FlutterCountdownTimer
FlutterCountdownTimer copied to clipboard
Stopping timer after Navigate
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),
);
}
},
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();
},
),
);
}