Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter navigation, reopen page instead of pushing it again

I'm new to flutter and I'm working on an App that have multiple screens.

I would like to know to stop flutter from creating multiple screens of the same route, for example I have Page 1 and Page 2, if I click on the button to navigate to Page 2 and clicked again on the same button the application will push a new screen of Page 2 and i will have it twice and if I click on the return button it will send me back to the first created Page 2

is there a way to create every page only once and then reopen it if it's already pushed to the stack ?

I'm using Navigator.push for all my navigation

like image 346
Mohammed Bouali Avatar asked Dec 23 '19 14:12

Mohammed Bouali


1 Answers

These answers are not correct. PushReplacement will still create a new instance of the widget. That's very simple to identify if you have a scrollable list in RouteA and scroll it before navigating to a RouteB. When you pushReplacement(RouteA) , you'll see the scrolling position have changed to its initial state. That is because a new widget was instantiated, and that is not the behaviour you want.

You should use popUntil, as previously answered by JoaoSoares

like image 97
Maurício Fontana de Vargas Avatar answered Oct 19 '22 20:10

Maurício Fontana de Vargas