Navigation between Screens aka Routes¶
Key Points
- Flutter provides several widgets for navigating between routes.
NavigationBar
is the default implementation for a bottom navigation and should be used when the app consists of 3 and up to 5 main areas resp. screens.- A
NavigationBar
is added as argument to thebottomNavigationBar
parameter of theScaffold
widget. - Tabs are another common navigation/layout pattern in apps and follows the Material Design guidelines.
- Tabs are implemented using a
TabController
to keep the selected tabs and the screen contents in sync. - Flutter provides a default implementation of a tab controller called
DefaultTabController
. - When there is not enough space for tabs use the
Drawer
widget in combination with aScaffold
. - Different screens resp. routes should be put in individual Dart/Flutter classes to separate them from the main
.lib
-file.
Where do you find the relevant information
- How to create and use the
NavigationBar
: https://api.flutter.dev/flutter/material/NavigationBar-class.html - How to create and use tabs: https://docs.flutter.dev/cookbook/design/tabs
- How to use a
Drawer
: https://docs.flutter.dev/cookbook/design/drawer - General information about navigation and routing: https://docs.flutter.dev/ui/navigation
Background Information and Examples¶
NavigationBar¶
In most cases using a bottom navigation is recommended and sufficient.
With Material 3, the NavigationBar
-class is the recommended way for navigation between 3 and up to 5 screens.
Flutter provides the NavigationBar class therefore.
Full Example¶
A first simple example of an app that uses a navigation bar with 3 different screens.
A scaffold
as parent widget has a dedicated property for a navigation bar as well as a body property that can hold screens for all selectable menu items in form of an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
Tabs¶
Working with tabs is a common pattern in apps that follow the Material Design guidelines. Flutter includes a convenient way to create tab layouts as part of the material library.
Tabs can be added to an app using the following steps;
- Create a
TabController
- Create the tabs
- Create content for each tab
1. Create a TabController
¶
For tabs to work, you need to keep the selected tab and content sections in sync. This is the job of the TabController
.
Either create
- a
TabController
manually, or - automatically by using a
DefaultTabController
widget.
Using DefaultTabController
is the simplest option, since it creates a TabController
and makes it available to all descendant widgets.
2. Create the tabs¶
When a tab is selected, it needs to display content. You can create tabs using the TabBar
widget.
In the following example, a TabBar
with three Tab
widgets is created and placed within an AppBar
:
Note that the TabBar
is added to the bottom
parameter of the AppBar
.
Which TabController will be used ?
By default, the TabBar
looks up the widget tree for the nearest DefaultTabController
.
If you’re manually creating a TabController
, pass it to the TabBar
.
3. Create content for each tab¶
After creating the tabs, display the corresponding content when a tab is selected. For this purpose, use the TabBarView
widget.
The order is important
Order is important and must correspond to the order of the tabs in the TabBar
.
The full code example is listed below: