From 6c8cf3a9d057319aedb0e058cf773b31372bd987 Mon Sep 17 00:00:00 2001 From: Leigha Jarett Date: Mon, 5 Jun 2023 23:52:07 -0400 Subject: [PATCH] Migration guide for moving from BottomNavigationBar to NavigationBar (#128263) Fixes https://github.com/flutter/flutter/issues/127213 --- .../navigation_bar/navigation_bar.0.dart | 16 ++++--- .../navigation_bar/navigation_bar.0_test.dart | 10 ++--- .../src/material/bottom_navigation_bar.dart | 44 +++++++++++++++---- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/examples/api/lib/material/navigation_bar/navigation_bar.0.dart b/examples/api/lib/material/navigation_bar/navigation_bar.0.dart index 689b97316b..b5ff01d313 100644 --- a/examples/api/lib/material/navigation_bar/navigation_bar.0.dart +++ b/examples/api/lib/material/navigation_bar/navigation_bar.0.dart @@ -36,20 +36,22 @@ class _NavigationExampleState extends State { currentPageIndex = index; }); }, + indicatorColor: Colors.amber[800], selectedIndex: currentPageIndex, destinations: const [ NavigationDestination( - icon: Icon(Icons.explore), - label: 'Explore', + selectedIcon: Icon(Icons.home), + icon: Icon(Icons.home_outlined), + label: 'Home', ), NavigationDestination( - icon: Icon(Icons.commute), - label: 'Commute', + icon: Icon(Icons.business), + label: 'Business', ), NavigationDestination( - selectedIcon: Icon(Icons.bookmark), - icon: Icon(Icons.bookmark_border), - label: 'Saved', + selectedIcon: Icon(Icons.school), + icon: Icon(Icons.school_outlined), + label: 'School', ), ], ), diff --git a/examples/api/test/material/navigation_bar/navigation_bar.0_test.dart b/examples/api/test/material/navigation_bar/navigation_bar.0_test.dart index b78688985b..f18598a397 100644 --- a/examples/api/test/material/navigation_bar/navigation_bar.0_test.dart +++ b/examples/api/test/material/navigation_bar/navigation_bar.0_test.dart @@ -16,20 +16,20 @@ void main() { final NavigationBar navigationBarWidget = tester.firstWidget(find.byType(NavigationBar)); /// NavigationDestinations must be rendered - expect(find.text('Explore'), findsOneWidget); - expect(find.text('Commute'), findsOneWidget); - expect(find.text('Saved'), findsOneWidget); + expect(find.text('Home'), findsOneWidget); + expect(find.text('Business'), findsOneWidget); + expect(find.text('School'), findsOneWidget); /// initial index must be zero expect(navigationBarWidget.selectedIndex, 0); /// switch to second tab - await tester.tap(find.text('Commute')); + await tester.tap(find.text('Business')); await tester.pumpAndSettle(); expect(find.text('Page 2'), findsOneWidget); /// switch to third tab - await tester.tap(find.text('Saved')); + await tester.tap(find.text('School')); await tester.pumpAndSettle(); expect(find.text('Page 3'), findsOneWidget); }); diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index ebcb53638a..f85a58e8e9 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -100,15 +100,36 @@ enum BottomNavigationBarLandscapeLayout { /// /// ## Updating to [NavigationBar] /// -/// There is an updated version of this component, [NavigationBar], -/// that's preferred for new applications and applications that are -/// configured for Material 3 (see [ThemeData.useMaterial3]). The -/// [NavigationBar] widget's visuals are a little bit different, see -/// the Material 3 spec at +/// The [NavigationBar] widget's visuals +/// are a little bit different, see the Material 3 spec at /// for -/// more details. The API is similar, destinations are defined with -/// [NavigationDestination]s and [NavigationBar.onDestinationSelected] -/// is called when a destination is tapped. +/// more details. +/// +/// The [NavigationBar] widget's API is also slightly different. +/// To update from [BottomNavigationBar] to [NavigationBar], you will +/// need to make the following changes. +/// +/// 1. Instead of using [BottomNavigationBar.items], which +/// takes a list of [BottomNavigationBarItem]s, use +/// [NavigationBar.destinations], which takes a list of widgets. +/// Usually, you use a list of [NavigationDestination] widgets. +/// Just like [BottomNavigationBarItem]s, [NavigationDestination]s +/// have a label and icon field. +/// +/// 2. Instead of using [BottomNavigationBar.onTap], +/// use [NavigationBar.onDestinationSelected], which is also +/// a callback that is called when the user taps on a +/// navigation bar item. +/// +/// 3. Instead of using [BottomNavigationBar.currentIndex], +/// use [NavigationBar.selectedIndex], which is also an integer +/// that represents the index of the selected destination. +/// +/// 4. You may also need to make changes to the styling of the +/// [NavigationBar], see the properties in the [NavigationBar] +/// constructor for more details. +/// +/// ## Using [BottomNavigationBar] /// /// {@tool dartpad} /// This example shows a [BottomNavigationBar] as it is used within a [Scaffold] @@ -122,6 +143,13 @@ enum BottomNavigationBarLandscapeLayout { /// {@end-tool} /// /// {@tool dartpad} +/// This example shows how you would migrate the above [BottomNavigationBar] +/// to the new [NavigationBar]. +/// +/// ** See code in examples/api/lib/material/navigation_bar/navigation_bar.0.dart ** +/// {@end-tool} +/// +/// {@tool dartpad} /// This example shows a [BottomNavigationBar] as it is used within a [Scaffold] /// widget. The [BottomNavigationBar] has four [BottomNavigationBarItem] /// widgets, which means it defaults to [BottomNavigationBarType.shifting], and