-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomodoro_screen.dart
155 lines (145 loc) · 4.11 KB
/
pomodoro_screen.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import 'dart:async';
import 'package:flutter/material.dart';
class PomodoroHomeScreen extends StatefulWidget {
const PomodoroHomeScreen({super.key});
@override
State<PomodoroHomeScreen> createState() => Pomodoro_HomeScreenState();
}
class Pomodoro_HomeScreenState extends State<PomodoroHomeScreen> {
static const initSecond = 1500;
int totalSeconds = initSecond;
int totalPomodoros = 0;
bool isRunning = false;
late Timer timer;
void onTick(Timer timer) {
if (totalSeconds == 0) {
setState(() {
totalSeconds = initSecond;
totalPomodoros += 1;
isRunning = false;
});
timer.cancel();
} else {
setState(() {
totalSeconds -= 1;
});
}
}
void onStartPressed() {
timer = Timer.periodic(
const Duration(seconds: 1),
onTick,
);
setState(() {
isRunning = true;
});
}
void onPausePressed() {
timer.cancel();
setState(() {
isRunning = false;
});
}
void onResetPressed() {
timer.cancel();
setState(() {
isRunning = false;
totalSeconds = initSecond;
});
}
String formatSec(int seconds) {
var min = (seconds / 60).floor();
var sec = seconds % 60;
if (sec < 10) {
return '$min:0$sec';
} else {
return '$min:$sec';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: Column(
children: [
Flexible(
flex: 1,
child: Container(
alignment: Alignment.bottomCenter,
child: Text(
formatSec(totalSeconds),
style: TextStyle(
color: Theme.of(context).cardColor,
fontSize: 89,
fontWeight: FontWeight.w600),
),
),
),
Flexible(
flex: 2,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
iconSize: 120,
color: Theme.of(context).cardColor,
icon: Icon(isRunning
? Icons.pause_circle_outline
: Icons.play_circle_outline),
onPressed: isRunning ? onPausePressed : onStartPressed,
),
const SizedBox(
width: 10,
),
IconButton(
iconSize: 120,
color: Theme.of(context).cardColor,
icon: const Icon(Icons.stop_circle_outlined),
onPressed: onResetPressed,
),
],
),
),
),
Flexible(
flex: 1,
child: Row(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
color: Theme.of(context).cardColor,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Pomodoros',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Theme.of(context).textTheme.headline1!.color,
),
),
Text(
'$totalPomodoros',
style: TextStyle(
fontSize: 60,
fontWeight: FontWeight.w600,
color: Theme.of(context).textTheme.headline1!.color,
),
),
],
),
),
),
],
),
),
],
),
);
}
}