简体   繁体   English

Flutter - 重启倒数计时器

[英]Flutter - Restart countdown timer

I'm trying to restart the countdown timer when I click on the resend code.当我点击重新发送代码时,我试图重新启动倒数计时器。 The package I'm using is timer_count_down .我使用的 package 是timer_count_down

I've tried adding:我试过添加:

 onTap: () {
     _controller.restart();
     _submit();
 },

But it can't seem to work.但这似乎行不通。 here is my code:这是我的代码:

final CountdownController _controller =
  new CountdownController(autoStart: true);

@override
  Widget build(BuildContext context) {
    return Countdown(
            seconds: 40,
            build: (_, double time) =>
                Column(
                  children: [
                    Text(time.toString(),
                      style: TextStyle(
                        color: Theme.of(context).primaryColor,
                      ),
                    ),
                    SizedBox(height: 30,),
                    Visibility(
                      visible: time == 0,
                      child: Column(
                        children: [
                          Text('Haven\'t recieved the code?',
                            style: TextStyle(
                              color: Colors.grey
                            ),
                          ),
                          SizedBox(height: 10,),
                          InkWell(
                            onTap: () {
                              _controller.restart();
                              _submit();
                            },
                            child: Text('Resend code',
                            style: TextStyle(
                              color: Theme.of(context).primaryColor,
                              fontWeight: FontWeight.bold,
                              decoration: TextDecoration.underline,
                            ),),
                          ),
                        ],
                      ))
                  ],
                ),
          ),

Is there any solutions to this?有什么解决办法吗?

I've been able to reproduce your issue both by using your provided code and the example code of the package (provided by the project) github.com/DizoftTeam/simple_count_down/blob/master/example/lib/main.dart .通过使用您提供的代码和 package(由项目提供) github.com/DizoftTeam/simple_count_down/blob/master/example/lib/main.dart的示例代码,我已经能够重现您的问题。

This issue is in both cases caused by not having the CountdownController assigned to the Countdown widget.在这两种情况下,这个问题都是由于没有将 CountdownController 分配给 Countdown 小部件引起的。

You should add the following line of code to the Countdown widget: controller: _controller,您应该将以下代码行添加到倒计时小部件: controller: _controller,

It should look like this:它应该是这样的:

return Countdown(
        controller: _controller,
        seconds: 40,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM