From d803f02d4f59bf06f4d3c26a6fa9669843908405 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 7 Jun 2018 15:42:54 -0700 Subject: [PATCH] Add activeIcon property to BottomNavigationBarItem (#18125) --- .../demo/material/bottom_navigation_demo.dart | 26 +++++++-- .../src/material/bottom_navigation_bar.dart | 2 +- .../widgets/bottom_navigation_bar_item.dart | 27 +++++++++- .../material/bottom_navigation_bar_test.dart | 53 +++++++++++++++++++ 4 files changed, 103 insertions(+), 5 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart b/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart index 79cca01600..2e0da39937 100644 --- a/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart @@ -7,6 +7,7 @@ import 'package:flutter/material.dart'; class NavigationIconView { NavigationIconView({ Widget icon, + Widget activeIcon, String title, Color color, TickerProvider vsync, @@ -15,6 +16,7 @@ class NavigationIconView { _title = title, item = new BottomNavigationBarItem( icon: icon, + activeIcon: activeIcon, title: new Text(title), backgroundColor: color, ), @@ -81,6 +83,21 @@ class CustomIcon extends StatelessWidget { } } +class CustomInactiveIcon extends StatelessWidget { + @override + Widget build(BuildContext context) { + final IconThemeData iconTheme = IconTheme.of(context); + return new Container( + margin: const EdgeInsets.all(4.0), + width: iconTheme.size - 8.0, + height: iconTheme.size - 8.0, + decoration: new BoxDecoration( + border: new Border.all(color: iconTheme.color, width: 2.0), + ) + ); + } +} + class BottomNavigationDemo extends StatefulWidget { static const String routeName = '/material/bottom_navigation'; @@ -105,19 +122,22 @@ class _BottomNavigationDemoState extends State vsync: this, ), new NavigationIconView( - icon: new CustomIcon(), + activeIcon: new CustomIcon(), + icon: new CustomInactiveIcon(), title: 'Box', color: Colors.deepOrange, vsync: this, ), new NavigationIconView( - icon: const Icon(Icons.cloud), + activeIcon: const Icon(Icons.cloud), + icon: const Icon(Icons.cloud_queue), title: 'Cloud', color: Colors.teal, vsync: this, ), new NavigationIconView( - icon: const Icon(Icons.favorite), + activeIcon: const Icon(Icons.favorite), + icon: const Icon(Icons.favorite_border), title: 'Favorites', color: Colors.indigo, vsync: this, diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index 101a49753a..3f7e1c9ec1 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -189,7 +189,7 @@ class _BottomNavigationTile extends StatelessWidget { color: iconColor, size: iconSize, ), - child: item.icon, + child: selected ? item.activeIcon : item.icon, ), ), ); diff --git a/packages/flutter/lib/src/widgets/bottom_navigation_bar_item.dart b/packages/flutter/lib/src/widgets/bottom_navigation_bar_item.dart index 8ccb2cc162..bc82034a68 100644 --- a/packages/flutter/lib/src/widgets/bottom_navigation_bar_item.dart +++ b/packages/flutter/lib/src/widgets/bottom_navigation_bar_item.dart @@ -25,8 +25,10 @@ class BottomNavigationBarItem { const BottomNavigationBarItem({ @required this.icon, @required this.title, + Widget activeIcon, this.backgroundColor, - }) : assert(icon != null), + }) : this.activeIcon = activeIcon ?? icon, + assert(icon != null), assert(title != null); /// The icon of the item. @@ -34,8 +36,31 @@ class BottomNavigationBarItem { /// Typically the icon is an [Icon] or an [ImageIcon] widget. If another type /// of widget is provided then it should configure itself to match the current /// [IconTheme] size and color. + /// + /// If [activeIcon] is provided, this will only be displayed when the item is + /// not selected. + /// + /// To make the bottom navigation bar more accessible, consider choosing an + /// icon with a stroked and filled version, such as [Icons.cloud] and + /// [Icons.cloud_queue]. [icon] should be set to the stroked version and + /// [activeIcon] to the filled version. + /// + /// If a particular icon doesn't have a stroked or filled version, then don't + /// pair unrelated icons. Instead, make sure to use a + /// [BottomNavigationBarType.shifting]. final Widget icon; + /// An alternative icon displayed when this bottom navigation item is + /// selected. + /// + /// If this icon is not provided, the bottom navigation bar will display + /// [icon] in either state. + /// + /// See also: + /// + /// * [BottomNavigationBarItem.icon], for a description of how to pair icons. + final Widget activeIcon; + /// The title of the item. final Widget title; diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart index c8799eed4f..a0422c1ec3 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -486,6 +486,59 @@ void main() { expect(box, paints..circle(x: 600.0)..circle(x: 200.0)..circle(x: 600.0)); }); + testWidgets('BottomNavigationBar inactiveIcon shown', (WidgetTester tester) async { + const Key filled = const Key('filled'); + const Key stroked = const Key('stroked'); + int selectedItem = 0; + + await tester.pumpWidget( + boilerplate( + textDirection: TextDirection.ltr, + bottomNavigationBar: new BottomNavigationBar( + currentIndex: selectedItem, + items: const [ + const BottomNavigationBarItem( + activeIcon: const Icon(Icons.favorite, key: filled), + icon: const Icon(Icons.favorite_border, key: stroked), + title: const Text('Favorite'), + ), + const BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm'), + ), + ], + ), + ), + ); + + expect(find.byKey(filled), findsOneWidget); + expect(find.byKey(stroked), findsNothing); + selectedItem = 1; + + await tester.pumpWidget( + boilerplate( + textDirection: TextDirection.ltr, + bottomNavigationBar: new BottomNavigationBar( + currentIndex: selectedItem, + items: const [ + const BottomNavigationBarItem( + activeIcon: const Icon(Icons.favorite, key: filled), + icon: const Icon(Icons.favorite_border, key: stroked), + title: const Text('Favorite'), + ), + const BottomNavigationBarItem( + icon: const Icon(Icons.access_alarm), + title: const Text('Alarm'), + ), + ], + ), + ), + ); + + expect(find.byKey(filled), findsNothing); + expect(find.byKey(stroked), findsOneWidget); + }); + testWidgets('BottomNavigationBar semantics', (WidgetTester tester) async { final SemanticsTester semantics = new SemanticsTester(tester);