如何在Flutter中进行倒数计时器

时间:2019-04-09 23:06:12

标签: flutter

我正在寻找在Flutter中制作倒数计时器的最佳方法。 我是这样做的,但我不确定这样做的有效方法,这是我的代码:

class CountDown extends StatefulWidget {
  final int secondsNum;
  CountDown({Key key, @required this.secondsNum}) : super(key: key);
  @override
  _CountDownState createState() => _CountDownState(secondsNum);
}
class _CountDownState extends State<CountDown> {
  Timer _timer;
  int _start;
  _CountDownState(int start) {
    this._start = start;
    startTimer();
  }
  void startTimer() {
    const oneSec = const Duration(seconds: 1);
    _timer = new Timer.periodic(
        oneSec,
            (Timer timer) => setState(() {
          if (_start < 1) {
            timer.cancel();
          } else {
            _start = _start - 1;
          }
        })
    );
  }
  @override
  Widget build(BuildContext context) {
    return Text("$_start");
  }
  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }
}

然后导入该文件并以这种方式调用它:CountDown(secondsNum: 50)。 问题是我要在同一屏幕上像打10次电话CountDown(secondsNum: 50)

有很多Javascript库可以对Web执行相同的操作,但是到目前为止,我还没有找到任何针对Flutter的库。如果有人对执行此操作的最佳方法有任何想法,请在下面发布。提前致谢。

0 个答案:

没有答案