user9288692
user9288692

Reputation:

Is there any class in flutter which will work same like Handler?

Can anyone tell, what is equivalent to handler in flutter?. i want to implement a splash screen which will last for 5 seconds and after another screen will be shown.

Upvotes: 5

Views: 4298

Answers (3)

Amol Gangadhare
Amol Gangadhare

Reputation: 1105

new Future.delayed(new Duration(seconds: 5), () {
  Navigator.pushAndRemoveUntil(
      context,
      MaterialPageRoute(builder: (context) => NewScreen()),
      (Route route) => false);
});

where NewScreen is the screen you wish to show, it will show a new screen.

Upvotes: 0

CopsOnRoad
CopsOnRoad

Reputation: 267534

Handler.postDelayed() -- Used to do work after some specific time

We can either use Future.postDelayed (as answered by Günter) or we can use Timer class too.

Timer(Duration(seconds: 5), () {
 // 5 seconds have past, you can do your work
}

Handler.post() -- Used to keep on doing work after some specific interval

We can use Timer.periodic function for this like

Timer.periodic(Duration(seconds: 5), (_) {
  // this code runs after every 5 second. Good to use for Stopwatches
}); 

Upvotes: 3

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657268

I don't think there is something similar to a Handler class, but you can just use Future.delayed and in build() render a different UI depending on showSplash:

showSplash = true;

new Future.delayed(const Duration(seconds: 5), () {
  setState(() => showSplash = false);  
});

Upvotes: 7

Related Questions