From 18e7549bc8f2ae1d061c943fcf3f8de251f6a5fa Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Tue, 21 Aug 2018 17:10:31 -0700 Subject: [PATCH] Update BottomNavigationBar.didUpdateWidget() (#20890) BottomNavigationBar's state needs to update _backgroundColor when its configuration changes. --- .../src/material/bottom_navigation_bar.dart | 3 + .../material/bottom_navigation_bar_test.dart | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index 1885b9a88a..7601e95738 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -415,6 +415,9 @@ class _BottomNavigationBarState extends State with TickerPr _controllers[oldWidget.currentIndex].reverse(); _controllers[widget.currentIndex].forward(); } + + if (_backgroundColor != widget.items[widget.currentIndex].backgroundColor) + _backgroundColor = widget.items[widget.currentIndex].backgroundColor; } List _createTiles() { diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart index 84bedeee9a..0f4aca54b6 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -712,6 +712,64 @@ void main() { expect(find.text('item 2'), findsNothing); expect(find.text('item 3'), findsNothing); }); + + testWidgets('BottomNavigationBar change backgroundColor test', (WidgetTester tester) async { + // Regression test for: https://github.com/flutter/flutter/issues/19653 + + Color _backgroundColor = Colors.red; + + await tester.pumpWidget( + new MaterialApp( + home: new StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return new Scaffold( + body: new Center( + child: new RaisedButton( + child: const Text('green'), + onPressed: () { + setState(() { + _backgroundColor = Colors.green; + }); + }, + ), + ), + bottomNavigationBar: new BottomNavigationBar( + type: BottomNavigationBarType.shifting, + items: [ + new BottomNavigationBarItem( + title: const Text('Page 1'), + backgroundColor: _backgroundColor, + icon: const Icon(Icons.dashboard), + ), + new BottomNavigationBarItem( + title: const Text('Page 2'), + backgroundColor: _backgroundColor, + icon: const Icon(Icons.menu), + ), + ], + ), + ); + }, + ), + ), + ); + + final Finder backgroundMaterial = find.descendant( + of: find.byType(BottomNavigationBar), + matching: find.byWidgetPredicate((Widget w) { + if (w is Material) + return w.type == MaterialType.canvas; + return false; + }), + ); + + expect(_backgroundColor, Colors.red); + expect(tester.widget(backgroundMaterial).color, Colors.red); + await tester.tap(find.text('green')); + await tester.pumpAndSettle(); + expect(_backgroundColor, Colors.green); + expect(tester.widget(backgroundMaterial).color, Colors.green); + }); } Widget boilerplate({ Widget bottomNavigationBar, @required TextDirection textDirection }) {