我在Flutter中有一个简单的计时器应用程序,它显示剩余秒数的倒计时.我有:
new Timer.periodic(new Duration(seconds: 1), _decrementCounter);
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常,直到我的手机显示屏关闭(即使我切换到另一个应用程序)并进入睡眠状态.然后,计时器暂停.是否有建议的方法来创建即使在屏幕关闭时在后台运行的服务?
我正在尝试构建一个倒计时小部件.目前,我得到了结构工作.我只与倒计时本身斗争.我使用倒计时插件尝试了这种方法:
class _Countdown extends State<Countdown> {
int val = 3;
void countdown(){
CountDown cd = new CountDown(new Duration(seconds: 4));
cd.stream.listen((Duration d) {
setState((){
val = d.inSeconds;
});
});
}
@override
build(BuildContext context){
countdown();
return new Scaffold(
body: new Container(
child: new Center(
child: new Text(val.toString(), style: new TextStyle(fontSize: 150.0)),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,价值变化非常奇怪而且根本不光滑.它开始抽搐.任何其他方法或修复?