How to make a countdown in flutter?
6,581
Flutter has a class called RestartableTimer
which extends from Timer
. It get's a Duration
element and a callback method when the timer is set.
When you want to restart it, you can simply reset it. Here is the code snippet to go through all of this. You can just put the code to the related place.
//You need to import this
import 'package:async/async.dart';
// Duration is 5 seconds
Duration _timerDuration = new Duration(seconds: 5);
// Creating a new timer element.
RestartableTimer _timer = new RestartableTimer(_timerDuration, _startNewPage);
fun _startNewPage() {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
}
// Restarting the timer
_timer.reset();
Author by
jericho
Updated on December 06, 2022Comments
-
jericho over 1 year
How can I achieve a Timer Widget that navigates to another screen after the time expired or it restarts itself after, lets say a swiping gesture?
-
EngineSense over 3 yearsfun cannot be called inside a widget!
-
salihgueler over 3 yearsThe answer does not state that it is called inside a widget :smh: