【发布时间】:2020-08-23 15:14:21
【问题描述】:
我使用动画控制器创建了一个倒数计时器,方法如下(发布在下面)。我知道在计时器完成时执行代码,但我想知道是否有办法在倒数计时器仍在运行时在一定时间后执行函数。在这种情况下,我想在计时器完成前 10 秒发出警报声,让用户知道他们的时间快到了。任何帮助将不胜感激。
class Timer extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _TimerState();
}
}
class _TimerState extends State<Timer> with SingleTickerProviderStateMixin {
AnimationController _controller;
int _timerDuration = 2;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this, duration: Duration(minutes: _timerDuration));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Countdown(
animation: StepTween(
begin: _timerDuration * 60,
end: 0,
).animate(_controller),
),
),
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
class Countdown extends AnimatedWidget {
Countdown({Key key, this.animation}) : super(key: key, listenable: animation);
Animation<int> animation;
@override
build(BuildContext context) {
Duration clockTimer = Duration(seconds: animation.value);
String timerText =
'${clockTimer.inMinutes.remainder(60).toString()}:${(clockTimer.inSeconds.remainder(60) % 60).toString().padLeft(2, '0')}';
return Text(
"$timerText",
);
}
【问题讨论】:
标签: flutter dart timer countdown flutter-animation