sarakhan
sarakhan

Reputation: 101

Flutter: How to pop last visited screen from stack in flutter

Let say i have three screens: wallet, payments, add card. I want to pop payments screen from stack when i'm on add card screen and navigate it to wallet screen without using navigator.push on click on icon plus the back icon of device.

here is the flow of screen:

wallet screen has a button which navigate to the payments screen, then payments screen has a button which navigate to the add card screen . So, now i want when i am on add card screen and click on back icon of android or the icon which is on add card screen both should navigate to wallet screen.

Please help how to do this.

Upvotes: 0

Views: 3769

Answers (4)

Tejaswini Dev
Tejaswini Dev

Reputation: 1469

Please try this below code

  1. PopAndPushNamed -> Pop the current route off the navigator that most tightly encloses the given context and push a named route in its place.

It pops payments screen from the navigation stack and only wallet screen remains in Navigation stack

Navigator.popAndPushNamed(context, '/addCard');
  1. PushNamedAndRemoveUntil -> It pushes the route with the given name (Wallets) onto the navigator, and then remove all the previous routes until the predicate returns true. Here it is using a RoutePredicate that always returns false (Route route) => false. In this situation it removes all of the routes except for the wallets route pushed.
//named
Navigator.of(context)
    .pushNamedAndRemoveUntil('\wallets', (Route<dynamic> route) => false);
    
//not named
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
    Wallets()), (Route<dynamic> route) => false),

  1. PopUntil -> Calls pop repeatedly on the navigator that most tightly encloses the given context until the predicate returns true. The predicate may be applied to the same route more than once if Route.willHandlePopInternally is true. To pop until a route with a certain name, use the RoutePredicate returned from ModalRoute.withName.
Navigator.popUntil(context, ModalRoute.withName('/wallets'));

Upvotes: 0

Jay Limbani
Jay Limbani

Reputation: 189

Navigator.removeRoute(context, MaterialPageRoute(builder: (context) => payments()));

use this code when you remove your payments screen

Upvotes: 0

AlishanMuhammadAmin
AlishanMuhammadAmin

Reputation: 310

try this:

Navigator.pushReplacement()

Upvotes: -1

fravolt
fravolt

Reputation: 3001

It's not very clear from your question how your navigation stack should look: Is payments first in the stack, and can you go from there to add card and wallet? Or should only one exist?

I think you can use pushReplacement: https://api.flutter.dev/flutter/widgets/Navigator/pushReplacement.html

That way, it'll automatically replace the current route with the new one, and your back button will simply close the app (or go back to the screen before it). If you want some custom behavior when the back button is pressed, you can always use a WillPopScope: https://api.flutter.dev/flutter/widgets/WillPopScope-class.html

Upvotes: 0

Related Questions