在屏幕上显示简单持续时间计时器



有什么简单的方法可以在屏幕上显示持续时间中的剩余时间吗?

void onTapDown(TapDownDetails details){
if(state == State.menu){
timer = Timer(duration = new Duration(seconds: 5), () {
state = State.playing;
});
print("STARTING IN [secondsremaing]");
}

还是我应该让它变得复杂并实现任何其他类来做到这一点?

不会那样工作,因为Timer只会在给定的Duration之后调用给定的回调,但它不会为你打勾
如果要显示指示剩余时间的更新小部件,则必须使用代码,这通常通过设置AnimationController来实现。

在您的情况下,它可能看起来像这样(假设您在StatefulWidget中(:

class _YourWidgetsState extends State<YourWidget> with SingleTickerProviderMixin {
AnimationController remainingTimeController;
@override
void initState() {
super.initState();
remainingTimeController = AnimationController(vsync: this, duration: const Duration(seconds: 5));
}
/// Used somewhere in your build method.
void onTapDown(TapDownDetails details) {
if(state == State.menu){
timer = Timer(duration = new Duration(seconds: 5), () {
state = State.playing;
});
print("STARTING IN [secondsremaing]");
}
}
@override
Widget build(BuildContext context) {
// Obviously return your other widgets in here as well.
return AnimatedBuilder(
animation: remainingTimeController,
builder: (context, _) => Text('${remainingTimeController.value * 60 * 5}'),
);
}
}

最新更新