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

发布于 2025-01-11 17:42:31 字数 2879 浏览 0 评论 0原文

我正在寻找一种在持续运行计时器的同时记录时间的方法,几乎​​就像圈功能一样。我有秒表设置,只需要一种创建圈功能的方法。我需要它能够在运行的同时记录一圈,直到它停止或开始另一圈。有人可以帮忙吗?如果需要,将提供代码。谢谢你! *编辑添加了我的秒表的代码

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

  }

I am looking for a way to record times while having a constant timer running, almost like a lap function. I have the stopwatch setup, just need a way to create a lap function. I need it be able to record a lap while still running till it is stopped or another lap is initiated. Can anyone help? Will provide code if needed. Thank you!
*edit added the code of my stopwatch

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

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2025-01-18 17:42:31

Dart 有一个 Stopwatch 。对于每一圈,您只需记录其当前的已用时间值。用法示例:

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

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

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

Dart has a Stopwatch class. For each lap, you can simply record its current elapsed value. Example usage:

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);
}

Note that the above would record the cumulative duration for the laps (i.e., the Durations will be monotonically increasing); if you instead want each individual lap's duration, then you would have to subtract each Duration from its previous one:

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

Alternatively you could easily implement Stopwatch yourself with DateTime.now() by recording a starting timestamp, recording timestamps for each lap, and then computing the differences between the current timestamp and the starting timestamp (or between the current timestamp and the previous timestamp if you want non-cumulative durations).

梦醒时光 2025-01-18 17:42:31

请发布您的代码以获取详细信息,但据我了解,我认为您可以创建另一个首先初始化为 0 的 lapCounter 变量。

int lapCounter = 0;

然后,在计时器计数器函数中,您结束计时器

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

,然后在函数中将进度重置为零。

setState(()=> lapCounter = 0);

Please post your code for details, but as far as I understood, I think you can create another lapCounter variable initialized as 0 at first.

int lapCounter = 0;

Then, in your timer counter function, where you end the timer

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

And then function where you reset the progress to zero.

setState(()=> lapCounter = 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文