显示留在屏幕上的简单持续时间计时器
Showing Simple Duration timer left on screen
有什么简单的方法可以在屏幕上显示 DURATION 的剩余时间吗?
void onTapDown(TapDownDetails details){
if(state == State.menu){
timer = Timer(duration = new Duration(seconds: 5), () {
state = State.playing;
});
print("STARTING IN [secondsremaing]");
}
或者我是否应该让它变得复杂并实施任何其他 class 来做到这一点?
它不会那样工作,因为Timer
只会在给定的Duration
之后调用给定的回调,但不会[=22] =]为你打勾。
如果你想显示一个更新的小部件来指示剩余时间,你将不得不使用一个自动收报机,这通常是通过设置一个 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}'),
);
}
}
有什么简单的方法可以在屏幕上显示 DURATION 的剩余时间吗?
void onTapDown(TapDownDetails details){
if(state == State.menu){
timer = Timer(duration = new Duration(seconds: 5), () {
state = State.playing;
});
print("STARTING IN [secondsremaing]");
}
或者我是否应该让它变得复杂并实施任何其他 class 来做到这一点?
它不会那样工作,因为Timer
只会在给定的Duration
之后调用给定的回调,但不会[=22] =]为你打勾。
如果你想显示一个更新的小部件来指示剩余时间,你将不得不使用一个自动收报机,这通常是通过设置一个 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}'),
);
}
}