有什么简单的方法可以在屏幕上显示DURATION中的剩余时间?
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}'),
);
}
}
关于flutter - 在屏幕上显示“简单持续时间”计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589826/