-
Notifications
You must be signed in to change notification settings - Fork 1
/
stopwatch_app_bar.dart
135 lines (114 loc) · 3.5 KB
/
stopwatch_app_bar.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
import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import '../bloc/note_page/note_page.dart';
class StopwatchAppBar extends StatelessWidget implements PreferredSize {
const StopwatchAppBar({Key? key, required this.notePageBloc}) : super(key: key);
final NotePageBloc notePageBloc;
@override
Widget build(BuildContext context) {
return child;
}
@override
Widget get child => StopwatchBar(notePageBloc: notePageBloc);
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
class StopwatchBar extends StatefulWidget {
const StopwatchBar({Key? key, required this.notePageBloc}) : super(key: key);
final NotePageBloc notePageBloc;
@override
State<StopwatchBar> createState() => _StopwatchBarState();
}
class _StopwatchBarState extends State<StopwatchBar> {
late Stopwatch stopwatch;
late Timer timer;
late int millisecondCount;
@override
void initState() {
super.initState();
millisecondCount = 0;
stopwatch = Stopwatch();
timer = Timer.periodic(const Duration(milliseconds: 10), _updateDisplay);
}
@override
void dispose() {
timer.cancel();
stopwatch.stop();
super.dispose();
}
void _updateDisplay(Timer timer) {
setState(() {
millisecondCount = stopwatch.elapsedMilliseconds;
});
}
void _toggleStopwatch() => stopwatch.isRunning ? stopwatch.stop() : stopwatch.start();
void _resetStopwatch() => stopwatch.reset();
String _getTimeString(int time) {
String str = time.toString();
if (str.length < 2) {
return "0$str";
}
return str;
}
int get _centiseconds {
int milliseconds = millisecondCount;
while (milliseconds >= 1000) {
milliseconds -= 1000;
}
return (milliseconds / 10).floor();
}
int get _seconds {
double seconds = millisecondCount / 1000;
while (seconds >= 60) {
seconds -= 60;
}
return (seconds).floor();
}
int get _minutes {
double minutes = millisecondCount / 60000;
while (minutes >= 60) {
minutes -= 60;
}
return (minutes).floor();
}
int get _hours => (_minutes / 60).floor();
String get _centisecondsString => _getTimeString(_centiseconds);
String get _secondsString => _getTimeString(_seconds);
String get _minutesString => _getTimeString(_minutes);
String get _hoursString => _getTimeString(_hours);
String get _stopwatchStringWithoutHour => "$_minutesString:$_secondsString:$_centisecondsString";
String get _stopwatchStringWithHour => "$_hoursString:$_minutesString:$_secondsString:$_centisecondsString";
String get _stopwatchString => _hours >= 1 ? _stopwatchStringWithHour : _stopwatchStringWithoutHour;
@override
Widget build(BuildContext context) {
return AppBar(
title: TextButton(
onPressed: () => _toggleStopwatch(),
onLongPress: () => _resetStopwatch(),
child: Text(
_stopwatchString,
style: const TextStyle(
fontSize: 22,
color: Colors.white,
fontFeatures: [
FontFeature.tabularFigures(),
],
),
),
),
actions: [
FocusScope.of(context).hasFocus
? TextButton(
child: const Text("Done", style: TextStyle(color: Colors.white)),
onPressed: () {
widget.notePageBloc.add(DoneTapped());
FocusScope.of(context).unfocus();
// widget.onDone();
},
)
: Container(),
],
);
}
}