Home > other >  Flutter restart countdown timer
Flutter restart countdown timer

Time:02-15

I'm trying to restart the countdown timer when I click on the resend code. The package I'm using is 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?

CodePudding user response:

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.

This issue is in both cases caused by not having the CountdownController assigned to the Countdown widget.

You should add the following line of code to the Countdown widget: controller: _controller,

It should look like this:

return Countdown(
        controller: _controller,
        seconds: 40,

CodePudding user response:

Add a interval property

interval: Duration(milliseconds: 100),

and after try to restart

 onTap:: () {
   _controller.restart();
 },
  • Related