Reputation: 101
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
Reputation: 1469
Please try this below code
It pops payments screen from the navigation stack and only wallet screen remains in Navigation stack
Navigator.popAndPushNamed(context, '/addCard');
//named
Navigator.of(context)
.pushNamedAndRemoveUntil('\wallets', (Route<dynamic> route) => false);
//not named
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>
Wallets()), (Route<dynamic> route) => false),
Navigator.popUntil(context, ModalRoute.withName('/wallets'));
Upvotes: 0
Reputation: 189
Navigator.removeRoute(context, MaterialPageRoute(builder: (context) => payments()));
use this code when you remove your payments screen
Upvotes: 0
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