首页 > 解决方案 > 如何在 Flutter 中重置倒计时

问题描述

当我点击“重新发送”时,我正在制作一些可以重新发送一些 otp 的应用程序。我正在尝试刷新状态,但似乎没有用。有人可以帮助如何重置倒数计时器吗?

飞镖板代码

标签: flutterdart

解决方案


你需要通过function

首先你需要改变你restartTimer

   void restartTimer() {
    setState(() {
      _controller.reset();
       _controller.forward();
    });
  }

然后将使用的小部件更改Countdown为此

  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Countdown(
            restartTimer:restartTimer, // send the function
            animation: StepTween(
              begin: 1 * 60,
              end: 0,
            ).animate(_controller),
          ),
        ),
      ),
    );
  }

之后,您需要receive该功能

class Countdown extends AnimatedWidget {
  Countdown({Key key, this.animation,this.restartTimer}) : super(key: key, listenable: animation);
  Animation<int> animation;
  final Function restartTimer;
   ....

然后在里面Countdown改成OnTap这个

 onTap: () {
              this.restartTimer();
            },

推荐阅读