无法更改 CountDownTimer 中的结束时间
Can't change endTime in CountDownTimer
我想在我的 flutter 应用程序中创建一个倒数计时器,我使用了 flutter_countdown_timer 来实现它。在我的应用程序中,我使用了以下代码;
CountdownTimer(
endTime: 1594829147719,
textStyle: TextStyle(
fontFamily: 'Montserrat',
fontSize: 12,
color: Colors.white),
defaultDays: "==",
defaultHours: "--",
defaultMin: "**",
defaultSec: "++",
daysSymbol: "Days ",
hoursSymbol: "Hrs ",
minSymbol: "Mins ",
secSymbol: "Secs",
),
在上面,如果我将 endTime 从 1594829147719 更改为 到某个其他值,此计时器停止工作并且不提供任何输出。
这可能是什么原因造成的,我该如何解决?
您可以从头开始创建计时器。
int timerSeconds;
DateTime startedAt;
StartTimer(){
_timer= new Timer.periodic(oneSec, (Timer t) => {
setState(() {
startedAt=DateTime.now();
seconds--;
})
});
}
//to format the timer text
String _printDuration(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
String getTimerText(){
int timePassed= DateTime.now().difference(startedAt).inSeconds;
timerTextString=_printDuration(Duration(seconds: timerText-timePassed));
}
然后在处理时停止定时器。
@override
void dispose() {
_timer.cancel();
super.dispose();
}
然后像这样调用文本小部件中的文本字符串
Text(getTimerText());
我想在我的 flutter 应用程序中创建一个倒数计时器,我使用了 flutter_countdown_timer 来实现它。在我的应用程序中,我使用了以下代码;
CountdownTimer(
endTime: 1594829147719,
textStyle: TextStyle(
fontFamily: 'Montserrat',
fontSize: 12,
color: Colors.white),
defaultDays: "==",
defaultHours: "--",
defaultMin: "**",
defaultSec: "++",
daysSymbol: "Days ",
hoursSymbol: "Hrs ",
minSymbol: "Mins ",
secSymbol: "Secs",
),
在上面,如果我将 endTime 从 1594829147719 更改为 到某个其他值,此计时器停止工作并且不提供任何输出。
这可能是什么原因造成的,我该如何解决?
您可以从头开始创建计时器。
int timerSeconds;
DateTime startedAt;
StartTimer(){
_timer= new Timer.periodic(oneSec, (Timer t) => {
setState(() {
startedAt=DateTime.now();
seconds--;
})
});
}
//to format the timer text
String _printDuration(Duration duration) {
String twoDigits(int n) {
if (n >= 10) return "$n";
return "0$n";
}
String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
return "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
}
String getTimerText(){
int timePassed= DateTime.now().difference(startedAt).inSeconds;
timerTextString=_printDuration(Duration(seconds: timerText-timePassed));
}
然后在处理时停止定时器。
@override
void dispose() {
_timer.cancel();
super.dispose();
}
然后像这样调用文本小部件中的文本字符串
Text(getTimerText());