Flutter - Dart

Таймер

main.dart

				
					import 'package:flutter/material.dart';
import 'dart:async';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int minutes = 5;
  int seconds = 0;
  late Timer timer;
  bool isTimerRunning = false;

  void startTimer() {
    if (!isTimerRunning) {
      timer = Timer.periodic(const Duration(seconds: 1), (timer) {
        setState(() {
          if (seconds > 0) {
            seconds--;
          } else {
            if (minutes > 0) {
              minutes--;
              seconds = 59;
            } else {
              timer.cancel();
              isTimerRunning = false;
            }
          }
        });
      });
      isTimerRunning = true;
    }
  }

  String formatTime(int time) {
    return time.toString().padLeft(2, '0');
  }

  void resetTimer() {
    setState(() {
      minutes = 5;
      seconds = 0;
      isTimerRunning = false;
      timer.cancel();
    });
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Таймер'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
                '${formatTime(minutes)}:${formatTime(seconds)}',
                 style: TextStyle(fontSize: 50,color: Colors.blue,fontWeight: FontWeight.w700), // Задайте нужный размер шрифта
                ),
            SizedBox(height: 20),
            ElevatedButton(
                          onPressed: () {
                          startTimer();
                          },
                          child: Text('Старт'),
                          style: ButtonStyle(
                          minimumSize: MaterialStateProperty.all(Size(150, 35)), 
                          ),
                        ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                resetTimer();
              },
              child: Text('Қалпына келтіру'),
              
            ),
          ],
        ),
      ),
    );
  }
}
console.log( 'Code is Poetry' );