Ankit Shukla
Ankit Shukla

Reputation: 751

How is navigation stack and launch modes handled in Flutter?

I wish to know for how to navigate between screen in Flutter by meeting all requirements that may come during application development. For example normal navigation to next page is easy but I want to play with stack. 1.How to clear stack when login completed so that from home page when user does the back press he finishes the application. 2. How to sign out from my app that is when I press sign out all pages are cleared from the stack and login page appears, and if press back then the app finishes.

Right now if anyone can provide an example or explanation for only above two then that will be great. Thanks.

Upvotes: 1

Views: 875

Answers (1)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126894

For both of your cases you can use pushAndRemoveUntil.

Case 1, i.e. you remove all routes (login routes) and navigate to your normal screen:

Navigator.of(context).pushAndRemoveUntil(yourHomeScreenRoute, (Route<dynamic> route) => false)

The expression (Route<dynamic> route) => false will just always return false, i.e. remove all routes.

Case 2, i.e. you remove all routes again and navigate to your login screen.

Navigator.of(context).pushAndRemoveUntil(yourLoginScreenRoute, (Route<dynamic> route) => false)

Another method is just popUntil, although I think that my concept will satisfy you.

In case you do not know how you would create routes, check this link.

Upvotes: 1

Related Questions