我已使用以下方法(以下说明)使用动画控制器创建了一个倒数计时器。我知道定时器完成后会执行代码,但我想知道是否有一种方法可以在倒计时定时器仍在运行的情况下经过一定时间后执行函数。在这种情况下,我想在计时器完成前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",
);
}
答案 0 :(得分:0)
您需要设置一个计时器并设置回调以播放声音:
Timer(Duration(<Your duration minus 10 sec>), () {
// play the sound here
});
要播放声音,请看一下这篇文章:https://fluttercentral.com/Articles/Post/1234/How_to_play_an_audio_file_in_flutter