Timer 颤振-如何在文本()中显示运行60秒的计时器
我正在构建一个应用程序,它需要在Text()中显示计时器,持续时间为60秒,当屏幕打开时,计时器会在Text()中自动从0秒到60秒启动 我试图使用,但我不知道如何获得放入文本()的时间(分和秒)Timer 颤振-如何在文本()中显示运行60秒的计时器,timer,flutter,Timer,Flutter,我正在构建一个应用程序,它需要在Text()中显示计时器,持续时间为60秒,当屏幕打开时,计时器会在Text()中自动从0秒到60秒启动 我试图使用,但我不知道如何获得放入文本()的时间(分和秒) 单击一个按钮,从0到60 Timer _timer; int _start = 0; void startTimer() { const oneSec = const Duration(seconds: 1); _timer = new Timer.periodic( oneSe
单击一个按钮,从0到60
Timer _timer;
int _start = 0;
void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) => setState(() {
if (_start > 60) {
timer.cancel();
} else {
_start = _start + 1;
}
}));
}
@override
void dispose() {
_timer.cancel();
super.dispose();
}
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title: Text("Timer test")),
body: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
startTimer();
},
child: Text("start"),
),
Text("$_start")
],
));
}
Timer\u定时器;
int _start=0;
void startTimer(){
const oneSec=const持续时间(秒:1);
_定时器=新定时器。周期性(
一秒,
(计时器)=>设置状态(){
如果(_start>60){
timer.cancel();
}否则{
_开始=_开始+1;
}
}));
}
@凌驾
无效处置(){
_timer.cancel();
super.dispose();
}
小部件构建(构建上下文){
归还新脚手架(
appBar:appBar(标题:文本(“计时器测试”)),
正文:专栏(
儿童:[
升起的按钮(
已按下:(){
startTimer();
},
子项:文本(“开始”),
),
文本($\u开始)
],
));
}
我不得不使用AnimatedBuilder来解决我的问题
String get timerString {
Duration duration = controller.duration * controller.value;
return '${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
}
AnimatedBuilder(
animation: controller,
builder: (context, child){
return Text(
timerString,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
) ,
);
}
),
我需要0到60。从0编辑到60。我更新了我的描述,因为我认为我没有很好地解释,我尝试了文本($\u start),但我只得到了“u Timer”的实例。尝试使用
返回文本(${DateTime.now())代码>但我只会得到当前的日期和时间,我需要一个自定义计时器,从00:00到00:60计数。它将显示从00到59,而不是60。我的分钟只有60秒,但是如果使用DateTime.now它将显示当前时间,如果它像1:20:40,我不想显示随机数,我需要显示00:00-00:59秒。
String get timerString {
Duration duration = controller.duration * controller.value;
return '${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
}
AnimatedBuilder(
animation: controller,
builder: (context, child){
return Text(
timerString,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
) ,
);
}
),