I have a simple Scaffold
with a drawer and a body. I want to select an item in the drawer and have the scaffold's body navigate to a new view.
Most approaches that i found (like this) just use a stateful widget and change its state when the drawer item is tapped.
However this completely ignores the navigation stack of the app, and pressing back does not return to the previous view as expected.
On the other hand, using Navigator.of(context).push...
when an item is clicked uses the navigation stack, but changes the whole screen, which is also not what i want.
I tried to create a new custom navigator for the scaffold's body, but had issues accessing the NavigatorState
from the Drawer.
This seems like a common problem to me (for instance, all google apps work that way) and i am a bit confused on how to implement this correctly. Is a custom navigator the correct approach? Are there some examples available?
Example of how to use the Navigation Drawer. 1. Create a New Flutter Project Go ahead and create a new Flutter app. If you don’t know how to create a project you can refer to the “ Hello World App in Flutter ” tutorial. For this tutorial, we will create two screens, one will be the Home Screen, and another one which will be Sample Screen.
DrawerHeader widget is used to define the header content of the drawer whereas ListTile is used to create drawer items. We also defined routes so that we can navigate to specific screens when the drawer items are clicked. Go through the complete Flutter navigation drawer example for better understanding.
To add a basic navigation drawer in Flutter, you must first use MaterialApp in your project. Then, the Drawer widget can be added to the Scaffold widget. Inside the Scaffold, add the Drawer property and assign the Drawer widget Inside the ListView, add the DrawerHeader widget. This will create a material design drawer header
In Android, a route is equivalent to an Activity. In iOS, a route is equivalent to a ViewController. In Flutter, a route is just a widget. This recipe uses the Navigator to navigate to a new route. The next few sections show how to navigate between two routes, using these steps: Create two routes.
It seems that you have to create a Widget only for the drawer, and include it in the Scaffold of each screen of your app. This way you can use the Navigator preserving the navigation stack, and include the drawer only in the screens you decide.
This is the article where I found an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With