m2faridi / flutter_example

3 stars 1 forks source link

Test timer #1

Open mohammadlatifalikhah opened 2 months ago

mohammadlatifalikhah commented 2 months ago

import 'dart:async';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget { const MyApp({super.key});

// This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } }

class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title});

final String title;

@override State createState() => _MyHomePageState(); }

class _MyHomePageState extends State { bool isActive = false; int second = 0; Timer? timer;

void secondconter() { if (isActive) { setState(() { second += 1; }); } }

@override Widget build(BuildContext context) { timer = Timer.periodic( Duration(seconds: 1), (timer) { secondconter(); });

int sec = second % 60;
int min = ((second % 3600) / 60).toInt();
int hour = ( second / 3600).toInt();

return Scaffold(
  appBar: AppBar(
      backgroundColor: Colors.blue,
      title: Text(
        "Timer",
        style: TextStyle(color: Colors.black, fontSize: 40),
      )),
  body: Container(
      color: Colors.black,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "$hour",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                ":",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                "$min",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                ":",
                style: TextStyle(color: Colors.white, fontSize: 50),
              ),
              Text(
                "$sec",
                style: TextStyle(color: Colors.white, fontSize: 50),
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () {
                  setState(() {
                    isActive=true;
                  });
                },
                child: Text('Start'),
              ),
              SizedBox(
                width: 30,
              ),
              ElevatedButton(
                onPressed: () {
                 setState(() {
                   isActive=false;
                   second=0;
                 });
                },
                child: Text('Stop'),
              ),
            ],
          )
        ],
      )),
);

} }

mohammadz411 commented 2 months ago

`import 'dart:async';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget { const MyApp({super.key});

// This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: ""), ); } }

class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title});

final String title;

@override State createState() => _MyHomePageState(); }

class _MyHomePageState extends State { Timer? _timer; int _elapsedTime = 0; int _min = 0;

void startTimer() { _timer = Timer.periodic(Duration(seconds: 1), (timer) { setState(() { _elapsedTime++; if (_elapsedTime == 60) { _min++; _elapsedTime = 0; } }); }); }

void stopTimer() { _timer?.cancel(); }

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

@override Widget build(BuildContext context) { return Scaffold( body: Container( color: Colors.deepPurple, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "$_min : $_elapsedTime ", style: TextStyle(fontSize: 48), ), SizedBox(height: 20), ElevatedButton( onPressed: stopTimer, child: Text('Stop'), ), ElevatedButton( onPressed: startTimer, child: Text("Start"), ), ], ), ), ), ); } } `