Skip to content

Commit 028cec9

Browse files
committed
fix: prefect count_down.dart
1 parent 46f27ab commit 028cec9

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

lib/page/count_down.dart

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class _CountDownPageState extends State<CountDownPage> {
3838
double strokeWidth = ScreenUtil().setHeight(12);
3939
// 进度条进度
4040
double progressValue = 0;
41-
// 倒计时是否结束
42-
bool success = false;
4341
// 当前时间
4442
int? hour;
4543
int? minutes;
@@ -56,8 +54,10 @@ class _CountDownPageState extends State<CountDownPage> {
5654
bool enable = false;
5755
// 是否开启声音
5856
bool enableAudio = true;
57+
58+
final num _threshold = 5;
5959
void handleStartClick() {
60-
if (pickerTime.inSeconds < 10) return;
60+
if (pickerTime.inSeconds <= _threshold - 1) return;
6161
setState(() => show = true);
6262
const timeout = Duration(seconds: 1);
6363
progressValue = 0;
@@ -82,7 +82,6 @@ class _CountDownPageState extends State<CountDownPage> {
8282
startSecond++;
8383
int diffSecond = totalSecond - startSecond;
8484
timerId = timer;
85-
success = false;
8685
if (enableAudio) {
8786
_myPlayer!.startPlayer(
8887
fromDataBuffer:
@@ -109,16 +108,8 @@ class _CountDownPageState extends State<CountDownPage> {
109108
// 倒计时完成
110109
if (diffSecond == 0) {
111110
timer.cancel();
112-
await Future.delayed(const Duration(milliseconds: 300));
113-
setState(() {
114-
show = false;
115-
hour = null;
116-
minutes = null;
117-
second = null;
118-
pickerTime = const Duration(seconds: 0);
119-
progressValue = 0;
120-
});
121-
success = true;
111+
await Future.delayed(const Duration(milliseconds: 800));
112+
countdownEnd();
122113
if (await Vibration.hasCustomVibrationsSupport() != null) {
123114
Vibration.vibrate(duration: 2500, amplitude: 128);
124115
}
@@ -127,18 +118,31 @@ class _CountDownPageState extends State<CountDownPage> {
127118
});
128119
}
129120

121+
void countdownEnd() {
122+
setState(() {
123+
show = false;
124+
hour = null;
125+
minutes = null;
126+
second = null;
127+
pickerTime = const Duration(seconds: 0);
128+
progressValue = 0;
129+
});
130+
}
131+
130132
Future<bool> handleOnWillPop() async {
131133
// 判断是否结束
132-
if (success) {
133-
return true;
134-
}
135-
// 判断是否退出
136-
if (await showConfirmDialog(context,
137-
message: S.of(context).cancelCountDownMessage) !=
138-
null) {
134+
if (progressValue == 0) {
139135
return true;
136+
} else {
137+
// 判断是否退出
138+
if (await showConfirmDialog(context,
139+
message: S.of(context).cancelCountDownMessage, showTips: false) !=
140+
null) {
141+
timerId?.cancel();
142+
countdownEnd();
143+
}
144+
return false;
140145
}
141-
return false;
142146
}
143147

144148
TextStyle textStyle = TextStyle(
@@ -285,8 +289,6 @@ class _CountDownPageState extends State<CountDownPage> {
285289
backgroundColor: isNightMode
286290
? Colors.grey.shade800
287291
: Colors.grey.shade300,
288-
circularStrokeCap:
289-
CircularStrokeCap.round,
290292
linearGradient:
291293
LinearGradient(colors: colors),
292294
percent: progressValue,
@@ -415,7 +417,7 @@ class _CountDownPageState extends State<CountDownPage> {
415417
S.of(context).startTiming,
416418
textAlign: TextAlign.center,
417419
style: TextStyle(
418-
color: pickerTime.inSeconds >= 10
420+
color: pickerTime.inSeconds >= _threshold
419421
? context
420422
.read<CurrentTheme>()
421423
.isNightMode

0 commit comments

Comments
 (0)