forked from md-siam/widget_of_the_day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.dart
53 lines (48 loc) · 1.22 KB
/
timer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'dart:async';
import 'package:flutter/material.dart';
class MyTimer extends StatefulWidget {
const MyTimer({Key? key}) : super(key: key);
@override
State<MyTimer> createState() => _MyTimerState();
}
class _MyTimerState extends State<MyTimer> {
// variable
int timeLeft = 5;
// timer method
void _startCountDown() {
Timer.periodic(const Duration(seconds: 1), (timer) {
if (timeLeft > 0) {
setState(() {
timeLeft--;
});
} else {
timer.cancel();
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Timer')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
timeLeft == 0 ? 'DONE' : timeLeft.toString(),
style: const TextStyle(fontSize: 70),
),
MaterialButton(
onPressed: _startCountDown,
color: Colors.purple,
child: const Text(
'S T A R T',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
],
),
),
);
}
}