Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test timer #1

Open
mohammadlatifalikhah opened this issue Aug 19, 2024 · 1 comment
Open

Test timer #1

mohammadlatifalikhah opened this issue Aug 19, 2024 · 1 comment

Comments

@mohammadlatifalikhah
Copy link

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
Copy link

`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"),
),
],
),
),
),
);
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants