Reputation: 289
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?
Upvotes: 2
Views: 8607
Reputation: 3622
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();
Upvotes: 16