Skip to content

Commit 6c8cf3a

Browse files
authored
Migration guide for moving from BottomNavigationBar to NavigationBar (flutter#128263)
Fixes flutter#127213
1 parent d876565 commit 6c8cf3a

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

examples/api/lib/material/navigation_bar/navigation_bar.0.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ class _NavigationExampleState extends State<NavigationExample> {
3636
currentPageIndex = index;
3737
});
3838
},
39+
indicatorColor: Colors.amber[800],
3940
selectedIndex: currentPageIndex,
4041
destinations: const <Widget>[
4142
NavigationDestination(
42-
icon: Icon(Icons.explore),
43-
label: 'Explore',
43+
selectedIcon: Icon(Icons.home),
44+
icon: Icon(Icons.home_outlined),
45+
label: 'Home',
4446
),
4547
NavigationDestination(
46-
icon: Icon(Icons.commute),
47-
label: 'Commute',
48+
icon: Icon(Icons.business),
49+
label: 'Business',
4850
),
4951
NavigationDestination(
50-
selectedIcon: Icon(Icons.bookmark),
51-
icon: Icon(Icons.bookmark_border),
52-
label: 'Saved',
52+
selectedIcon: Icon(Icons.school),
53+
icon: Icon(Icons.school_outlined),
54+
label: 'School',
5355
),
5456
],
5557
),

examples/api/test/material/navigation_bar/navigation_bar.0_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ void main() {
1616
final NavigationBar navigationBarWidget = tester.firstWidget(find.byType(NavigationBar));
1717

1818
/// NavigationDestinations must be rendered
19-
expect(find.text('Explore'), findsOneWidget);
20-
expect(find.text('Commute'), findsOneWidget);
21-
expect(find.text('Saved'), findsOneWidget);
19+
expect(find.text('Home'), findsOneWidget);
20+
expect(find.text('Business'), findsOneWidget);
21+
expect(find.text('School'), findsOneWidget);
2222

2323
/// initial index must be zero
2424
expect(navigationBarWidget.selectedIndex, 0);
2525

2626
/// switch to second tab
27-
await tester.tap(find.text('Commute'));
27+
await tester.tap(find.text('Business'));
2828
await tester.pumpAndSettle();
2929
expect(find.text('Page 2'), findsOneWidget);
3030

3131
/// switch to third tab
32-
await tester.tap(find.text('Saved'));
32+
await tester.tap(find.text('School'));
3333
await tester.pumpAndSettle();
3434
expect(find.text('Page 3'), findsOneWidget);
3535
});

packages/flutter/lib/src/material/bottom_navigation_bar.dart

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,36 @@ enum BottomNavigationBarLandscapeLayout {
100100
///
101101
/// ## Updating to [NavigationBar]
102102
///
103-
/// There is an updated version of this component, [NavigationBar],
104-
/// that's preferred for new applications and applications that are
105-
/// configured for Material 3 (see [ThemeData.useMaterial3]). The
106-
/// [NavigationBar] widget's visuals are a little bit different, see
107-
/// the Material 3 spec at
103+
/// The [NavigationBar] widget's visuals
104+
/// are a little bit different, see the Material 3 spec at
108105
/// <https://m3.material.io/components/navigation-bar/overview> for
109-
/// more details. The API is similar, destinations are defined with
110-
/// [NavigationDestination]s and [NavigationBar.onDestinationSelected]
111-
/// is called when a destination is tapped.
106+
/// more details.
107+
///
108+
/// The [NavigationBar] widget's API is also slightly different.
109+
/// To update from [BottomNavigationBar] to [NavigationBar], you will
110+
/// need to make the following changes.
111+
///
112+
/// 1. Instead of using [BottomNavigationBar.items], which
113+
/// takes a list of [BottomNavigationBarItem]s, use
114+
/// [NavigationBar.destinations], which takes a list of widgets.
115+
/// Usually, you use a list of [NavigationDestination] widgets.
116+
/// Just like [BottomNavigationBarItem]s, [NavigationDestination]s
117+
/// have a label and icon field.
118+
///
119+
/// 2. Instead of using [BottomNavigationBar.onTap],
120+
/// use [NavigationBar.onDestinationSelected], which is also
121+
/// a callback that is called when the user taps on a
122+
/// navigation bar item.
123+
///
124+
/// 3. Instead of using [BottomNavigationBar.currentIndex],
125+
/// use [NavigationBar.selectedIndex], which is also an integer
126+
/// that represents the index of the selected destination.
127+
///
128+
/// 4. You may also need to make changes to the styling of the
129+
/// [NavigationBar], see the properties in the [NavigationBar]
130+
/// constructor for more details.
131+
///
132+
/// ## Using [BottomNavigationBar]
112133
///
113134
/// {@tool dartpad}
114135
/// This example shows a [BottomNavigationBar] as it is used within a [Scaffold]
@@ -122,6 +143,13 @@ enum BottomNavigationBarLandscapeLayout {
122143
/// {@end-tool}
123144
///
124145
/// {@tool dartpad}
146+
/// This example shows how you would migrate the above [BottomNavigationBar]
147+
/// to the new [NavigationBar].
148+
///
149+
/// ** See code in examples/api/lib/material/navigation_bar/navigation_bar.0.dart **
150+
/// {@end-tool}
151+
///
152+
/// {@tool dartpad}
125153
/// This example shows a [BottomNavigationBar] as it is used within a [Scaffold]
126154
/// widget. The [BottomNavigationBar] has four [BottomNavigationBarItem]
127155
/// widgets, which means it defaults to [BottomNavigationBarType.shifting], and

0 commit comments

Comments
 (0)