Reset countdown time
Closed this issue · 9 comments
Hello, After the SlideCountdown component is added, set the countdown time. When the countdown time expires, the device will not move until a new countdown time is set.
Hi @yj229201093 , when the countdown is done it will restart to the initialization duration?
Hi @yj229201093 , when the countdown is done it will restart to the initialization duration?
Yes, it is a continuous countdown, for example, after the end of a countdown, the background will return a new countdown time. It should be back to the initialization state. At present, it is found that after the end of a countdown, the next countdown will not be followed, and I do not know how to deal with it.
是的 ,就是连续倒计时,如,一个倒计时结束后,后台会重新返回一个新的倒计时时间。应该就是回到初始化状态。目前发现一个倒计时结束后,不会接着下一个倒计时,不知道如何处理。
have you tried using onDone
property? this example will restart to initial initiation when countdown is completed
SlideCountdownSeparated(
key: UniqueKey(),
duration: const Duration(seconds: 10),
onDone: () {
setState(() {});
},
),
onDone
The new timer does not work. I guess the internal timer has been cancelled.
尝试过了,就是在 onDone回调后到然后赋值进去的,然后新的倒计时不生效,我猜应该是内部的定时器取消了。
return SlideCountdown(
key: UniqueKey(),
duration:
TimeUtils.getCountdownTime(widget.nowAt ?? 0, widget.endAt ?? 0),
padding: const EdgeInsets.only(left: 0),
slideDirection: SlideDirection.none,
separatorType: SeparatorType.title,
showZeroValue: true,
withDays: true,
durationTitle: const DurationTitle(
days: '天',
hours: '小时',
minutes: '分',
seconds: '秒',
),
textStyle: YWTextStyle.setTextStyle(YWColor.white, fontSize: 12),
onDone: () {
Log.d('完成'); // There's a reassignment after that, and then the new data doesn't count down
if (widget.onDone != null) {
widget.onDone!();
}
},
onChanged: (value) {
Log.d('value = $value');
},
decoration: const BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.all(Radius.circular(0)),
));
add UniqueKey(), ok, sorry for my question. Thanks for your guidance
您好 大神,问下,我用,倒计时结束后,我开启新的倒计时,不会更新。我添加 key: UniqueKey()可以更新,但是在倒计时期间我刷新页面的话,会闪缩,一闪一闪。这个有解决防范。
Hello God, ask, I use, after the end of the countdown, I open a new countdown, will not update. I add key: UniqueKey() to update, but when I refresh the page during the countdown, it will blink, blink, blink. This has a solution guard.
SlideCountdown(
// key: UniqueKey(),
duration:
TimeUtils.getCountdownTime(nowAt ?? 0, tempSaleStartedAt ?? 0),
padding: const EdgeInsets.only(left: 10),
// fade: true,
slideDirection: SlideDirection.none,
separatorStyle: YWTextStyle.setTextStyle(YWColor.white,
fontSize: 11, fontWeight: FontWeight.normal, height: 1.1),
separatorType: SeparatorType.title,
separatorPadding: const EdgeInsets.all(0),
showZeroValue: true,
withDays: false,
durationTitle: const DurationTitle(
days: ':',
hours: ':',
minutes: ':',
seconds: '',
),
textStyle: YWTextStyle.setTextStyle(YWColor.white, fontSize: 14),
onDone: () {
Log.d('完成');
if (onDone != null) {
onDone!();
}
},
onChanged: (value) {
Log.d('value = $value');
},
decoration: const BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.all(Radius.circular(0)),
));
@yj229201093 You can change UniqueKey
to ValueKey
here an example
onDone
important for called setState or you can use StateManagement for triger build
make sure value inValueKey(value)
is diffrent from oldValueKey(oldValue)
const kInitialDuration = Duration(seconds: 10);
const kNextDuration = Duration(seconds: 15);
class SlideCountdownPage extends StatefulWidget {
const SlideCountdownPage({Key? key}) : super(key: key);
@override
State<SlideCountdownPage> createState() => _SlideCountdownPageState();
}
class _SlideCountdownPageState extends State<SlideCountdownPage> {
int _completed = 0;
@override
Widget build(BuildContext context) {
final duration = _completed % 2 == 0 ? kInitialDuration : kNextDuration;
return Scaffold(
body: Center(
child: SlideCountdown(
key: ValueKey(_completed),
duration: duration,
showZeroValue: true,
withDays: false,
onDone: () {
setState(() {
_completed++;
});
},
),
),
);
}
}
感谢,感谢,这个问题可以解决,但是又有了一个新的问题,就是我app在后台或者在切换到其他页面的时候,倒计时就不会走,在到这个页面才会走,会导致倒计时时间不准确。 这个好解决吗?