I want my button to navigate back to my home page not to the immediate previous page in flutter - flutter

I want my flutter app to navigate from my last screen back to the home page. Currently, it navigates back to the previous page which I do not want.
onPressed: () {
Navigator.pop(context, MaterialPageRoute(builder: (context) => MyHomePage()));
},
I expect the button to return me back to the Home Page

If you don't use named routes you can simply replace your code with this:
onPressed: () {
Navigator.of(context).popUntil((route) => route.isFirst);
},
This will pop the navigation stack until it reaches the first item which might be your home page.

Can't you just use Navigator.pushAndReplace(MaterialPageRoute(builder: (context) => MyHomePage())); for navigation back to the home page?

Related

flutter pushReplacement page

This is my second page button, go to page 3.
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder:
(context) => MapsPage(index: widget.index))
);
},
This is the page 3 button that will send information back to page 2, but when the second page is pressed to go back to page 1, it has to be pressed twice. It's like replacing a new face now.
void saveAddress() {
var lat = centerMap.latitude;
var lng = centerMap.longitude;
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (_) => EditAddressPage(
index: widget.index,
lat: lat.toString(),
lng: lng.toString(),
),
));
print("Saving address: Latitude: $lat, Longitude: $lng");
}
The pushReplacement method replaces the current route on the navigation stack with a new route, rather than pushing a new route on top of the stack. This means that when you press the back button, you are not going back to the previous route (Page 2), but instead going back to the route that was replaced (Page 1).
To fix this issue, you can use the push method instead of pushReplacement when navigating from Page 3 to Page 2. This will add a new route to the navigation stack, rather than replacing the current route, and allow you to navigate back to Page 2 when the back button is pressed.

Flutter Drawer Navigation

I have a list of Screens in a custom drawer.
Screen 1,
Screen 2,
Screen 3
What is the proper way of navigating from Screen 1 to Screen 2 on push of a button. Currently, I am losing the hamburger (the drawer button) option when I push or push replace.
The code I have was not written by me and I do not have access to the person who wrote the code.
Any thoughts?
Just use the standard navigation:
TextButton(
onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => Screen2()));
}
child:
//child goes here
)

How to navigate to previous page using device back button - Flutter

When I press the device back button, it navigate to the homepage but not to the previous page.
I would like to know how to navigate to the previous page using the the device back button.
Thank you
You might have used Navigator.pushReplacement which destroys the previous page and creates the new page.However if you use Navigator.push pressing the back button will navigate you to the previous screen.
Example:
Container(
child: Center(
child: TextButton(child: Text("Next page"),onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context) => Page2(),)); // back button will navigate to previous screen
//Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Page2(),)); // back button will not navigate to previous page
},),),),
It was a Materialapp widget problem.
I solved the problem by deleting the Materialapp widget that was in one of the pages.
This occurs because the previous navigation was not saved in the route stack.
What you should do is this:
Widget A
Navigator.push(context, WidgetB());
Inside Widget B, press the device button and you will return to widget A.
It is important that in widget A you are not using replacement or another that removes widget A from the stack.
You can use:
Navigator.pop(context);
or
Navigator.pushNamed(context, '/routeName');

Navigation in Flutter App - does not work as intended

I try to build a flutter app and want to navigate trough the pages.
I build a drawer with a menu with several items to open new pages - that works fine.
onTap: () {
Navigator.of(context).pop();
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) => new Page1(),
),
);
},
By pressing on the Page1 menuitem page1 opens fine.
Inside page1 I have a slidable list with "edit" and "details". I open this with
onTap: () {
Navigator.of(context).pop();
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => new DetailsPage(),
),
);
},
In the details Page I see all the things i wanna see, but when I press the Back button in the appBar i am back at home screen with closed drawer.
I tried to put onPressed: () => Navigator.pop(context); inside the appBar section but this doesn´t work either.
Now I got what I want by using always Navigator.push but I am sure that this is not right, so that navigator stack gets bigger and bigger.
What I do wrong? Why .pop does not bring me to the last page?
Any ideas?
Thx in advance
Patrick
Just remove the line with the Navigator.of(context).pop();in the Page1 onTap.
you can use pushReplacement in any cases you need to remove current page from stack and route to new page.
Replace the current route of the navigator that most tightly encloses
the given context by pushing the given route and then disposing the
previous route once the new route has finished animating in.
here is the documentation

Getting a black screen when navigating between two pages in flutter

This is my home page code:
routes: {
'/second' : (context) => addExpence(),
},
my second-page code is:
FlatButton(
child: Text("Done".toUpperCase()),
onPressed: (){
Navigator.pop(context);
},
)
please note that both pages are in different files. Now the problem is that I am getting a black screen when popping from the First Page.
It's a natural thing to get a black screen when you pop from the first page because the Navigator will be empty. The only reason you're popping the first page is probably to close your app, for which you should use this method.
#Lewis Weng's answer is the correct one that works for me too.
if(Navigator.canPop(context)){
Navigator.of(context).pop();
}else{
SystemNavigator.pop();
}