FlutterCountdownTimer icon indicating copy to clipboard operation
FlutterCountdownTimer copied to clipboard

Stopping timer after Navigate

Open andy-t-wang opened this issue 5 years ago • 1 comments

I have a 5 minute timer, if the person finishes before time is up they navigate to another page. I want the timer to be destroyed because when they fill it out again and go to the 5 minute timer the timer has the leftover time from the previous time they were on the page. Then it executes the OnEnd wrongly using the leftover time rather than the current time.

How to dispose of the current timer after navigating away from the screen. I want to have a new timer each time I come to the screen.

CountdownTimer(
                        endTime: endTime,
                        onEnd: () {
                          print("end");
                          if (widget.isSeer) {
                            seerFinish();
                          } else {
                            lockinAnswer();
                            // This is still executing even after navigating 
                            Navigator.of(context).pushReplacement(
                              MaterialPageRoute(
                                  builder: (context) => RevealScreen(
                                      colors[widget.color + selection - 1],
                                      selection,
                                      win,
                                      widget.roomCode,
                                      widget.myName),
                                  fullscreenDialog: true),
                            );
                          }
                        },

andy-t-wang avatar Dec 15 '20 22:12 andy-t-wang

Hello,I released version 1.6.0.You can use disposetimer to destroy it.

CountdownTimerController controller;
  @override
  void initState() {
    super.initState();
    int endTime = DateTime.now().millisecondsSinceEpoch + 1000 * 30;
    controller = CountdownTimerController(endTime: endTime, onEnd: onEnd);
  }

  void onEnd() {
    print('onEnd');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          CountdownTimer(
            controller: controller,
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.stop),
        onPressed: () {
          onEnd();
          /// destroy it. <<<<<<<<<<<<<<
          controller.disposeTimer();
        },
      ),
    );
  }

wuweijian1997 avatar Dec 16 '20 08:12 wuweijian1997