Flutter Change Size of Drawer Header
When comes to the navigation in-app, Navigation drawer can be considered of the one primary method of Navigation
InFlutter, Navigation Drawer comes with the Scaffold widget. Therefore before adding a navigation drawer we need to define aScaffold.
When we are creating a sample flutter app, it comes with the default scaffold. There is an option to set a drawer like below
Scaffold( drawer: ) Create a basic navigation drawer
Now we already have a skeleton with the Scaffold widget. For that, we need to setDrawer widget.
To properly structuring the Drawer you need to set Header part and the below you can add some menu items. For that, you have to create aListview and add the first child as aDrawerHeader and after that, you can addListTile widget to show other menu items.
drawer: Drawer( child: ListView( padding: EdgeInsets.zero, children: [ DrawerHeader( child: Text("Header"), ), ListTile( title: Text("Home"), ) ], ), ), Navigation Drawer header image
Before adding an image you have to make an assets folder and create an images folder inside that. Then add your image to that folder.
Next inside thepubspec.yaml you need to import that asset. For that under the flutter add aassets section andimage like below
flutter: assets: - images/header.jpeg The next thing needs to do is provide decoration toDrawerHeader. In there you can use set image usingDecorationImage widget. Please remember to set fit type aBoxFit.cover
drawer: Drawer( child: ListView( padding: EdgeInsets.zero, children: [ DrawerHeader( decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/header.jpeg"), fit: BoxFit.cover ) ), child: Text("Header"), ), ListTile( title: Text("Home"), ) ], ), ) Navigation Drawer Sticky header
Drawer items inside the ListView will scroll all the child content when scroll. If you want to change that to keep the header part sticky and move only the below items you have to do some changes to the widget structure
Drawer( child: Column( children: [ Expanded( flex: 1, child: Container( width: MediaQuery.of(context).size.width * 0.85, child: DrawerHeader( decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/header.jpeg"), fit: BoxFit.cover)), child: Text("Header"), ), ), ), Expanded( flex: 2, child: ListView(children: [ ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ) ]), ) ], ), )
Close Navigation Drawer programmatically
It pretty easy to close the drawer programmatically. You just need only use the pop method inNavigation class for the current context.
ListTile( title: Text("Home"), onTap: (){ Navigator.of(context).pop(); }, ) Open Navigation Drawer programmatically
First, you must set a key to the scaffold to identify. You can create a new object of GlobalKey class and set that to the scaffold key. Then using that you can access the current state of the Scaffold and open the drawer by callingopenDrawer method.
You can call this method from any button and in here I added a FlatButton to the body and from there I am going to open the drawer. You can also add a custom button to AppBar and trigger like this to open a drawer.
final GlobalKey _scaffoldKey = new GlobalKey(); return Scaffold( key: _scaffoldKey ) // set key FlatButton( child: Text("Open Navigation"), onPressed: (){ _scaffoldKey.currentState.openDrawer(); }, ), Navigation Drawer hamburger icon change and color change
If you are using Appbar you can addleading Icon to replace the hamburger Icon. You can set IconButton for that and setting the colour will also change the Icon colour of that button.
But when you add a custom button it will be lost the drawer open capability. But you can use the same old method to open the drawer programmatically by callingopenDrawer method.
appBar: AppBar( leading: IconButton( icon: Icon(Icons.gamepad,color: Colors.yellow,), onPressed: (){ _scaffoldKey.currentState.openDrawer(); }, ), title: Text(widget.title), ), Navigation Drawer in right side
It pretty easy to move drawer from left to right. When you are creating a drawer in the first place, it usesdrawer option in Scaffold. You just need only to change that toendDrawer and the drawer will be open from the right. Also,hamburger menu will be a move to the right.
return Scaffold( key: _scaffoldKey, endDrawer: Drawer( child: ListView( padding: EdgeInsets.zero, children: [ DrawerHeader( decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/header.jpeg"), fit: BoxFit.cover ) ), child: Text("Header"), ), ListTile( title: Text("Home"), onTap: (){ Navigator.of(context).pop(); }, ) ], ), ),
Custom Navigation drawer
Drawer inside the Scaffold accepts Widget as a parameter. Therefore we can add custom widget without using Drawer widget inside the navigation.
In here I use Container as a main widget and wrap the other content inside that. Now when you test you will be able to see a full-screen drawer.
drawer: Container( width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).size.height, color: Colors.white, child: ListView( padding: EdgeInsets.zero, children: [ DrawerHeader( decoration: BoxDecoration( image: DecorationImage( image: AssetImage("images/header.jpeg"), fit: BoxFit.cover)), child: Text("Header"), ), ListTile( title: Text("Home"), onTap: () { Navigator.of(context).pop(); }, ) ], ), ),
Conclusion
I hope you get a better idea about navigation drawer implementation in Flutter and how to customize drawer as you need. If you have any question feel free to ask.
Flutter Change Size of Drawer Header
Source: https://mightytechno.com/flutter-navigation-drawer/
0 Response to "Flutter Change Size of Drawer Header"
Post a Comment