Update BottomNavigationBar.didUpdateWidget() (#20890)
BottomNavigationBar's state needs to update _backgroundColor when its configuration changes.
diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart
index 1885b9a..7601e95 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 @@
_controllers[oldWidget.currentIndex].reverse();
_controllers[widget.currentIndex].forward();
}
+
+ if (_backgroundColor != widget.items[widget.currentIndex].backgroundColor)
+ _backgroundColor = widget.items[widget.currentIndex].backgroundColor;
}
List<Widget> _createTiles() {
diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart
index 84bedee..0f4aca5 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 @@
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: <BottomNavigationBarItem>[
+ 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<Material>(backgroundMaterial).color, Colors.red);
+ await tester.tap(find.text('green'));
+ await tester.pumpAndSettle();
+ expect(_backgroundColor, Colors.green);
+ expect(tester.widget<Material>(backgroundMaterial).color, Colors.green);
+ });
}
Widget boilerplate({ Widget bottomNavigationBar, @required TextDirection textDirection }) {