1

How to Build a Flutter Navigation Drawer

 1 year ago
source link: https://www.christianfindlay.com/blog/flutter-navigation-drawer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Build a Flutter Navigation Drawer

October 30, 2021
Flutter

Navigation drawer is a typical UI pattern for adaptive menus. The Material Design documentation formalizes the menu's behavior, but this pattern is not peculiar to Material Design. The pattern includes a basic Hamburger menu but isn't limited to small screens. The menu pattern suits all screen sizes. On larger width screens, you see the icon and text, smaller screens will display icons only, and on phones, the menu will disappear during regular use and slide in with the hamburger icon.

Follow me on Twitter for updates to this example.

Watch how the menu adapts to screen size changes on a desktop screen. It behaves the same way on phones and other form factors. Check out the complete example in the repo, or see the example code here. This example is a work in progress, and the long-term aim is to implement the material design pattern as closely as possible.

62baa67a497b63e2d4231177_navigationdrawer.png
Menu on desktop

If you're looking for a simple menu drawer example that isn't adaptive, please check out this article.

NavigationDrawerMenu

This is a widget that functions like a ListView. However, it exists to remove some of the boilerplate code necessary for constructing the menu and allows you to put arbitrary-sized spacers and headings inside the menu. The example uses this set of definitions. If you don't want to use this widget, you can use ListView instead. Add this package from pub dev, or see the code here.

How Does the Adaptive Layout Work?

On build, the widget checks the screen's width with a MediaQuery. It divides the screen into three width sizes: Drawer (only menu drawer), Thin menu, or Thick Menu. When we view on desktop landscape, we will see the full menu, but you can also toggle it to thin. If the user shrinks the width of the window, the menu will become thin. This is how to check:

In Drawer mode, toggleDrawer causes the drawer to open or close with an animation. Phones will always have a small width and use Drawer only mode.


Here is the Android menu

62baa94f23f82015f50dca6b_android.png
Menu on Android

Lastly, this is the code that dynamically builds the menu. It hides the text when the user is in Thin menu mode.

Wrap-Up

Flutter started out as a phone-only UI Toolkit, but adaptive layouts are becoming more important. When working with designers you should collect two or three versions of each screen for different form factors. If you are careful, you can usually make the code adaptive so that the screen matches each of the design form factors.

Christian Findlay

I am a Flutter, .NET and Cloud Developer living in Melbourne, Australia. I've been developing software and mentoring teams for over twenty years.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK