Crafting Navigation Drawers in Flutter
Crafting Navigation Drawers in Flutter
Navigation drawers are a commonly used UI element, often found in apps like Gmail, YouTube, and many more. They allow users to quickly navigate around the app without having to dive deep into the navigation hierarchy. The Flutter framework makes it easy to create a custom navigation drawer. Here’s everything you need to know about crafting navigation drawers in Flutter.
Understanding Drawer Widgets
At the core of creating a navigation drawer is the Drawer widget. A simple example of a Drawer widget is shown below:
Drawer(
child: ListView(
children: [
ListTile(
title: Text(‘Home’),
onTap: () { /* Navigate to Home page */ },
),
ListTile(
title: Text(‘Settings’),
onTap: () { /* Navigate to Settings page */ },
),
],
)
)
The Drawer widget contains a ListView of ListTiles which represent the various pages that can be navigated to. When a ListTile is tapped, the appropriate navigation method can be called. This is how navigation drawers are set up within Flutter.
Customizing Drawers
Drawers can be further customized by adding properties to the Drawer widget. The drawer’s background color, its width, and its elevation can all be changed to better suit the design. A customized draw can be seen below:
Drawer(
backgroundColor: Colors.blue[200],
elevation: 10.0,
width: 200.0,
child: ListView(
children: [
ListTile(
title: Text(‘Home’),
onTap: () { /* Navigate to Home page */ },
),
ListTile(
title: Text(‘Settings’),
onTap: () { /* Navigate to Settings page */ },
),
],
)
)
It’s important to note that the Drawer widget should be a child of the Scaffold widget in order for the Drawer to appear. The final piece of the puzzle is to add an IconButton to the AppBar which will open and close the navigation drawer.
Adding an IconButton
AppBar(
title: Text(‘My App’),
actions: [
IconButton(
icon: Icon(Icons.menu),
onPressed: () {
// Show the navigation drawer
}
)
]
)
The popular hamburger menu icon will appear in the AppBar, allowing the user to open and close the navigation drawer. All that’s left to do is add some logic to show/hide the Drawer when the icon is pressed.
Finishing Touches
The last step is to add logic to show/hide the Drawer when the icon is pressed. This logic can either be written in the stateful or stateless widget class. First, declare a boolean variable which will track the state of the Drawer. Then create a method which will toggle this value when the IconButton is pressed. This method should also call the Drawer.open() method when it is run.
bool _isDrawerOpen = false;
void _toggleDrawerState() {
setState(() {
_isDrawerOpen = !_isDrawerOpen;
});
if (_isDrawerOpen)
_drawerKey.currentState.open();
else
_drawerKey.currentState.close();
}
Finally, wrap the Drawer widget in an AnimatedContainer and pass it the _isDrawerOpen boolean variable as a parameter. This will automatically animate the Drawer as it is opened/closed.
AnimatedContainer(
duration: Duration(milliseconds: 500),
color: _isDrawerOpen ? Colors.blue[200] : Colors.transparent,
child: Drawer(
...
)
)
And there you have it, a custom navigation drawer built with Flutter! With just a few lines of code, you can create a seamless navigation experience for your users.