[英]How to update a Text widget automatically - Flutter
I am trying to make a simple timer to know how much time has passed from a date to a current date我正在尝试制作一个简单的计时器来了解从某个日期到当前日期已经过去了多少时间
but the problem I have is that the Text widget is only painted once but it does not update, and I would like the text to update every 1 second.但我遇到的问题是 Text 小部件只绘制一次但不会更新,我希望文本每 1 秒更新一次。 And not have to refresh the page, is it possible to do that?
并且不必刷新页面,是否可以这样做?
import 'package:flutter/material.dart';
class TimerPage extends StatefulWidget {
@override
_TimerPageState createState() => _TimerPageState();
}
class _TimerPageState extends State<TimerPage> {
@override
Widget build(BuildContext context) {
final start = DateTime(2020, 8, 10, 16, 30, 0);
final currentdate= DateTime.now();
final comparation = currentdate.difference(start);
return Scaffold(
body: SafeArea(
child: Container(
padding: EdgeInsets.symmetric(vertical: 20),
child: Center(
child: Column(
children: <Widget>[
Text("Start time: " + start.toString()),
Divider(),
Text(
"time elapsed = " + comparation.toString().substring(0, 7),
style: TextStyle(color: Color(0xff5abd8c), fontSize: 26),
)
],
)),
),
),
);
}
}
I try this:我试试这个:
Timer timer;
final start = DateTime(2020, 8, 17, 21, 30, 0);
final dateActual = DateTime.now();
Duration comparation = new Duration(hours: 0, minutes: 0, seconds: 0);
@override
void initState() {
timer = Timer.periodic(Duration(seconds: 1), (_) {
setState(() {
comparation = dateActual.difference(start);
print(comparation);
});
});
}
@override
void dispose() {
timer?.cancel();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
padding: EdgeInsets.symmetric(vertical: 20),
child: Center(
child: Column(
children: <Widget>[
Text("Start time: " + start.toString()),
Divider(),
Text(
"time elapsed = " + comparation.toString().substring(0, 7),
style: TextStyle(color: Color(0xff5abd8c), fontSize: 26),
)
],
)),
),
),
);
}
}
To do this, you want a Timer
from the dart:async
library, specifically a Timer.periodic
.为此,您需要来自
dart:async
库的Timer
,特别是Timer.periodic
。
Instantiate one in initState
of your StatefulWidget
and cancel it in dispose
.在
StatefulWidget
的initState
中实例化一个并在dispose
中取消它。
Timer.periodic
takes a callback that fires after every duration that's passed. Timer.periodic
接受一个回调,该回调在每个持续时间过去后触发。 Since your DateTime
difference calculation is already done in build
, you just need to trigger a rebuild with setState
.由于您的
DateTime
差异计算已经在build
中完成,您只需要使用setState
触发重建。
Timer timer;
@override
void initState() {
timer = Timer.periodic(Duration(seconds: 1), (_) {
setState(() {});
});
}
@override
void dispose() {
timer?.cancel();
}
It's not possible to do this without rebuilding, at the very least, the Text
widget that's being changed.如果不至少重建正在更改的
Text
小部件,就不可能做到这一点。 I don't think it's necessary since there isn't much in this build
method, but if you wanted to do that, you would have to extract all of the necessary time calculations, timer, and single Text
widget to its own StatefulWidget
, so that only the one widget that's necessary to be rebuilt, is rebuilt.我认为没有必要,因为此
build
方法中没有太多内容,但是如果您想这样做,则必须将所有必要的时间计算、计时器和单个Text
小部件提取到它自己的StatefulWidget
中,所以只有一个需要重建的小部件被重建。
Ideally, you should also be doing the time calculations outside of the build
method, but it's not a huge deal in this case and you'll get something that works.理想情况下,您还应该在
build
方法之外进行时间计算,但在这种情况下这并不是什么大问题,您会得到一些有用的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.