Flutter Drawer Navigation - flutter

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
)

Related

How to switch the screen smoothly?

I have three screens: 1, 2, and 3.
I want to switch from screen 2 to screen 3, but when I pop screen 2 and push screen 3, then it shows screen 1 for a while, then it shows screen 3, so is there any way to switch screens using pop after push or not?
this code not expectations
Navigator.push(
context,
MaterialPageRoute<Widget>(
builder: (context) => widget,
),
);
Navigator.pop(context);
Using pushReplacement insted of push
Navigator.pushReplacement(
context,
MaterialPageRoute<Widget>(
builder: (context) => widget,
),
);
Using pushReplacement insted of push
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => widget,
),
);
The behavior you're describing is likely because when you call Navigator.pop(context), it removes the current screen (screen 2) from the navigation stack, but the framework hasn't yet had a chance to build and display the new screen (screen 3) that you pushed. So, for a brief moment, the previous screen (screen 1) is visible.
One way to switch screens without that flicker is to use Navigator.pushReplacement instead of Navigator.push followed by Navigator.pop. Navigator.pushReplacement removes the current screen from the navigation stack and replaces it with the new screen, all in one step.
Example:
Navigator.pushReplacement(
context,
MaterialPageRoute<Widget>(
builder: (context) => widget3,
),
);
This will directly switch from screen 2 to screen 3.
It is generally not recommended to use both Navigator.push() and Navigator.pop() together in the same function call, as this can cause unexpected behavior. Instead, you can use Navigator.replace() to replace the current screen with a new one, or use Navigator.pushNamedAndRemoveUntil() to push a new screen and remove all the screens that come before it.

Navigation in in flutter

I have a request from a client who wants to skip one page in opening under some conditions and immediately open another page. However the problem is that when it goes back it wants to always show 2 pages. So page 1 opens page 3 and when I go back from page 3 it goes to page 2 and then station 1. I'm new to flutter and I don't know how to do it.
Use pushReplacement for such Navigation
// In Screen 1
Navigator.pushReplacement(context, Screen3()) // Or Screen2()
// In Screen 3
Navigator.pushReplacement(context, Screen2())
// In Screen 2
Navigator.pushReplacement(context, Screen1())
From page 1 to page 3 your can use
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Page3()),
Then if you want to go to Page 2 from page 3 use WillPopScope in page 3
return WillPopScope(
onWillPop: (){},
child: Scaffold(
body: Column(
children: [],
),
),
);
Add the above code in page 3. Add condition to check if it should go to Page 1 or 2 in onWillPop and again use navigator.pop or navigator.pushReplacement

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

I want my button to navigate back to my home page not to the immediate previous page in 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?