Flutter-重新启动CountdownTimer

时间:2019-06-05 18:01:07

标签: flutter

我有一个倒数计时器(5分钟)。我需要一些东西来重新开始倒计时并从头开始。

我尝试更改倒计时变量的状态,但是无法正常工作,停止并从最后一个计数器编号重新开始。

我的代码:

import 'package:flutter/material.dart';
    import 'package:intl/intl.dart';
    import 'package:quiver/async.dart';

    class ProgressIndicatorDemo extends StatefulWidget {
      @override
      _ProgressIndicatorDemoState createState() =>
          new _ProgressIndicatorDemoState();
    }

    class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo>
        with SingleTickerProviderStateMixin {
      AnimationController controller;
      Animation<double> animation;
      var countdown;
      int actual;

      @override
      void initState() {
        super.initState();
        animationStart();
        startTimer();
      }

      @override
      void dispose() {
        controller.dispose();
        super.dispose();
      }

      void animationStart() {
        controller =
            AnimationController(duration: Duration(minutes: 5), vsync: this);
        animation = Tween(begin: 0.0, end: 1.0).animate(controller)
          ..addListener(() {
            setState(() {});
          });
        controller.forward();
      }

      void startTimer() {
        CountdownTimer countDownTimer = new CountdownTimer(
          new Duration(minutes: 5),
          new Duration(seconds: 1),
        );

        countdown = countDownTimer.listen(null);
        countdown.onData((duration) {
          setState(() {
            actual = 300 - duration.elapsed.inSeconds;
          });
        });

        countdown.onDone(() {
          countdown.cancel();
        });
      }

      @override
      Widget build(BuildContext context) {
        return new Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              animation.value != 1.0
                  ? CircularProgressIndicator(
                      semanticsLabel:
                          (animation.value * 100.0).toStringAsFixed(1).toString() +
                              '%',
                      semanticsValue:
                          (animation.value * 100.0).toStringAsFixed(1).toString() +
                              '%',
                      backgroundColor: Colors.black.withOpacity(0.25),
                      valueColor:
                          new AlwaysStoppedAnimation<Color>(Colors.green[700]),
                      value: animation.value)
                  : Icon(Icons.info_outline),
              actual != null
                  ? Text(
                      "Tiempo:" +
                          DateFormat.ms()
                              .format(DateTime.fromMillisecondsSinceEpoch(
                                  actual * 1000))
                              .toString(),
                      textAlign: TextAlign.center,
                    )
                  : Container(),
              FlatButton(
                  child: Text('Reset ' + actual.toString()), onPressed: () {}),
            ],
          ),
        );
      }
    }

问题是:如何从开始倒数计时重新开始?

3 个答案:

答案 0 :(得分:2)

您可以使用RestartableTimer

使用更干净的解决方案
import 'package:async/async.dart';

RestartableTimer _timer = new RestartableTimer(_timerDuration, _startNewPage);

然后,您只需调用_timer.reset();

即可重新开始倒计时。

希望有帮助。

答案 1 :(得分:1)

尝试先取消计时器,然后重新启动计时器:

  void restartTimer() {
    countDownTimer.cancel();
    startTimer();
  }

答案 2 :(得分:0)

尝试在顶部声明您的Timer,并在每次要重新启动它时调用此方法。

    void restartTimer() {
    countDownTimer = new CountdownTimer(
      new Duration(minutes: 5),
      new Duration(seconds: 1),
    );

别忘了声明变量