我正在寻找一种方法来记录时间,同时有一个恒定的计时器运行,几乎像一圈功能。我有秒表设置,只是需要一种方法来创建一圈功能。我需要它能够记录一圈,而仍然运行,直到它停止或另一圈是启动。有人能帮忙吗?将提供代码,如果需要的话。谢谢!* 编辑添加了我的秒表的代码
Widget stopwatch(){
return Container(
child: Column(
children: <Widget>[
Expanded(
flex: 6,
child: Container(
alignment: Alignment.center,
child: Text(
stoptimedisplay,
style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.bold,
),
),
),
),
Expanded(
flex: 4,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
onPressed: stoppressed ? null : stopstopwatch,
color: Colors.red,
padding: EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 12.0,
),
child: Text(
"Stop",
style: TextStyle(
fontSize: 20.0,
color: Colors.white
),
),
),
RaisedButton(
onPressed: resetpressed ? null : resetstopwatch,
color: Colors.blueGrey,
padding: EdgeInsets.symmetric(
horizontal: 40.0,
vertical: 12.0,
),
child: Text(
"Reset",
style: TextStyle(
fontSize: 20.0,
color: Colors.white
),
),
),
],
),
RaisedButton(
onPressed: startpressed ? startstopwatch : null,
color: Colors.green,
padding: EdgeInsets.symmetric(
horizontal: 80.0,
vertical: 20.0,
),
child: Text(
"Start",
style: TextStyle(
fontSize: 24.0,
color: Colors.white
),
),
),
],
),
),
),
],
),
);
}
2条答案
按热度按时间xdnvmnnf1#
dart 有一个
Stopwatch
class。对于每圈,您可以简单地记录其当前elapsed
值。示例用法:注意,以上将记录圈的 * 累积 * 持续时间(即,
Duration
s将单调增加);如果您需要每一圈持续时间,那么您必须将每一个Duration
从它的前一个中减去:或者,您也可以使用
DateTime.now()
轻松实现Stopwatch
,方法是记录起始时间戳、记录每圈的时间戳,然后计算当前时间戳和起始时间戳之间的差异(或者如果您想要非累积持续时间,则计算当前时间戳和前一个时间戳之间的差异)。8hhllhi22#
请张贴您的代码的细节,但据我所知,我认为您可以创建另一个lapCounter变量初始化为0在第一。
然后,在timer counter函数中,结束计时器的位置
然后将进度重置为零。