Chapter 4
Visual, Behavioral, and Motion-Rich Widgets
Implementing Material Design Guidelines
Contents
• Introduction
• BottomNavigationBar Widget
• ListTile Widget
• DefaultTabController, TabBar, and TabBarView
Widgets
• ListView Widget
• Drawer Widget
Introduction
In this lesson, you will learn more about Flutter widgets especially
those related to navigating the app screens in different ways of
navigations and configurations techniques. You may add more than one
navigation method to your app that depends on your app purpose and
your customers’ targets. Remember, you should make your app as easy
to navigate and to find information as possible.
ButtomNavigationBar Widget
Bottom navigation bar is a material widget that is displayed at the
bottom of an app for selecting among a small number of views,
typically between three and five.
The bottom navigation bar consists of multiple items in the form of text
labels, icons, or both, laid out on top of a piece of material. It provides
quick navigation between the top-level views of an app.
ButtomNavigationBar Widget
BottomNavigationBar widget depends on its work on a list of
BottomNavigationBarItem widgets where each BottomNavigationBarItem
represents one item of the bottom navigation bar (icon and text) as
illustrated in the following figure:
ButtomNavigationBar Widget (Example)
ListTile Widget
In some app layout designs, you have a list of items that you really want
them to follow the material design specifications. You could spend
hours working out the perfect item layout using rows, columns,
containers, and various amounts of padding. Or, you could just use a
ListTile widget.
ListTile widget implements the material design list spec for you with full
and easy control on its content. The following figure displays how the
ListTile widget looks like and some ListTile() widget properties:
ListTile Widget
ListTile Widget (Example)
DefaultTabController, TabBar, and
TabBarView Widgets
The DefaultTabController, TabBar, and TabBarView widgets are used to
organize your widgets or app contents into tabs. As illustrated in the
below figure, to use tabs, you will first need a tab controller
(DefaultTabController) to keep the selected tab and the visible content
in sync.
The easiest way to do this is using the DefaultTabController widget.
Once you have that, you will need a widget to show the tabs that the
app user would use to switch between the different interfaces of your
app. This is where TabBar comes in. The TabBar takes a list of tab
widgets. You can have the tab show text, an icon, or a child widget.
DefaultTabController, TabBar, and
TabBarView Widgets
DefaultTabController, TabBar, and
TabBarView Widgets (Example)
ListView Widget
If you want to set your app items to appear in a scrollable list, the
ListView widget is the perfect choice for your app layout. The default
scroll direction of your app items is vertical, however, you can change
the scroll direction to horizontal. The following code displays the main
component of the ListView widget:
ListView Widget (Example)
This example uses the default constructor for ListView which takes an
explicit List<Widget> of children. This ListView's children are made up
of Containers with Text.
ListView Widget (Example)
Drawer Widget
In apps that use Material Design, there are two primary options for navigation: tabs
and drawers. When there is insufficient space to support tabs, drawers provide a
handy alternative.
In Flutter, use the Drawer widget in combination with a Scaffold to create a layout
with a Material Design drawer. This recipe uses the following steps:
1) Create a Scaffold.
2) Add a drawer.
3) Populate the drawer with items.
4) Close the drawer programmatically.
Drawer Widget (Create Scaffold)
To add a drawer to the app, wrap it in a Scaffold widget. The Scaffold
widget provides a consistent visual structure to apps that follow the
Material Design Guidelines. It also supports special Material Design
components, such as Drawers, AppBars, and SnackBars.
In this example, create a Scaffold with a drawer:
Drawer Widget (Add a drawer)
Now add a drawer to the Scaffold. A drawer can be any widget, but it’s
often best to use the Drawer widget from the material library, which
adheres to the Material Design spec.
Drawer Widget (Populate the drawer with
items)
Now that you have a Drawer in place, add content to it. For this
example, use a ListView. While you could use a Column widget,
ListView is handy because it allows users to scroll through the drawer if
the content takes more space than the screen supports.
Populate the ListView with a DrawerHeader and two ListTile widgets.
For more information on working with Lists, see the list recipes.
Drawer Widget (Populate the drawer with
items)
Drawer Widget (Close the drawer
Programmatically)
After a user taps an item, you might want to close the drawer. You can
do this by using the Navigator.
When a user opens the drawer, Flutter adds the drawer to the
navigation stack. Therefore, to close the drawer, call
Navigator.pop(context).
End