Android Studio 如何在Flutter上记录秒表时间?

kq0g1dla  于 2023-01-26  发布在  Android
关注(0)|答案(2)|浏览(112)

我正在寻找一种方法来记录时间,同时有一个恒定的计时器运行,几乎像一圈功能。我有秒表设置,只是需要一种方法来创建一圈功能。我需要它能够记录一圈,而仍然运行,直到它停止或另一圈是启动。有人能帮忙吗?将提供代码,如果需要的话。谢谢!* 编辑添加了我的秒表的代码

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
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );

  }
xdnvmnnf

xdnvmnnf1#

dart 有一个Stopwatch class。对于每圈,您可以简单地记录其当前elapsed值。示例用法:

void main() async {
  var stopwatch = Stopwatch()..start();
  var lapTimes = <Duration>[];

  for (var i = 0; i < 5; i += 1) {
    var randomSeconds = random.nextInt(5) + 1;
    print(randomSeconds);
    await Future.delayed(Duration(seconds: randomSeconds));
    lapTimes.add(stopwatch.elapsed);
  }

  print(lapTimes);
}

注意,以上将记录圈的 * 累积 * 持续时间(即,Duration s将单调增加);如果您需要每一圈持续时间,那么您必须将每一个Duration从它的前一个中减去:

var lapDeltas = [
    lapTimes[0],
    for (var i = 1; i < lapTimes.length; i += 1)
      lapTimes[i] - lapTimes[i - 1],
  ];
  print(lapDeltas);

或者,您也可以使用DateTime.now()轻松实现Stopwatch,方法是记录起始时间戳、记录每圈的时间戳,然后计算当前时间戳和起始时间戳之间的差异(或者如果您想要非累积持续时间,则计算当前时间戳和前一个时间戳之间的差异)。

8hhllhi2

8hhllhi22#

请张贴您的代码的细节,但据我所知,我认为您可以创建另一个lapCounter变量初始化为0在第一。

int lapCounter = 0;

然后,在timer counter函数中,结束计时器的位置

....myTimer ends here before restarting the counter.
setState(()=> lapCounter += 1);

然后将进度重置为零。

setState(()=> lapCounter = 0);

相关问题