From 9abce96e8ef5e3fed87ab8b20b207e05cb0d70c7 Mon Sep 17 00:00:00 2001 From: Michael Beckler Date: Wed, 7 Nov 2018 12:20:27 -0500 Subject: [PATCH] BottomNavigationBar: bug fix for dealing with animations with shifting tabs (#22264) Should fix #22226. Code introduced in #20890 caused a regression that broke color flooding animations in a BottomNavigationBar that has BottomNavigationBarType.shifting. The original issue (#19653) dealt with background color changes not occurring until another tab was selected. The result is that the background color instantly changes whenever the state changes and when the widget changes, instead of allowing a new widget to animate the background color change. --- bin/internal/goldens.version | 2 +- .../demo/material/bottom_navigation_demo.dart | 9 ---- .../src/material/bottom_navigation_bar.dart | 6 +-- .../material/bottom_navigation_bar_test.dart | 51 +++++++++++++++++++ 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/bin/internal/goldens.version b/bin/internal/goldens.version index 14d768d141c..d489cc13a90 100644 --- a/bin/internal/goldens.version +++ b/bin/internal/goldens.version @@ -1 +1 @@ -dd723e291a7997ce6b551b910447b64b3fe9ba7e +8c478bbaf27447f3d612959705b305e7d1293526 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 3046c74b08d..2d90b40407a 100644 --- a/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/bottom_navigation_demo.dart @@ -153,9 +153,6 @@ class _BottomNavigationDemoState extends State ) ]; - for (NavigationIconView view in _navigationViews) - view.controller.addListener(_rebuild); - _navigationViews[_currentIndex].controller.value = 1.0; } @@ -166,12 +163,6 @@ class _BottomNavigationDemoState extends State super.dispose(); } - void _rebuild() { - setState(() { - // Rebuild in order to animate views. - }); - } - Widget _buildTransitionsStack() { final List transitions = []; diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index 5513c0c24d7..724b6ac3953 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -473,10 +473,10 @@ class _BottomNavigationBarState extends State with TickerPr } _controllers[oldWidget.currentIndex].reverse(); _controllers[widget.currentIndex].forward(); + } else { + if (_backgroundColor != widget.items[widget.currentIndex].backgroundColor) + _backgroundColor = widget.items[widget.currentIndex].backgroundColor; } - - 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 d499dfc80dc..7f387426ba1 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:io'; import 'dart:ui'; import 'package:flutter/material.dart'; @@ -782,6 +783,56 @@ void main() { expect(tester.widget(backgroundMaterial).color, Colors.green); }); + testWidgets('BottomNavigationBar shifting backgroundColor with transition', (WidgetTester tester) async { + // Regression test for: https://github.com/flutter/flutter/issues/22226 + + int _currentIndex = 0; + await tester.pumpWidget( + MaterialApp( + home: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return Scaffold( + bottomNavigationBar: RepaintBoundary( + child: BottomNavigationBar( + type: BottomNavigationBarType.shifting, + currentIndex: _currentIndex, + onTap: (int index) { + setState(() { + _currentIndex = index; + }); + }, + items: const [ + BottomNavigationBarItem( + title: Text('Red'), + backgroundColor: Colors.red, + icon: Icon(Icons.dashboard), + ), + BottomNavigationBarItem( + title: Text('Green'), + backgroundColor: Colors.green, + icon: Icon(Icons.menu), + ), + ], + ), + ), + ); + }, + ), + ), + ); + + await tester.tap(find.text('Green')); + + for (int pump = 0; pump < 8; pump++) { + await tester.pump(const Duration(milliseconds: 30)); + await expectLater( + find.byType(BottomNavigationBar), + matchesGoldenFile('bottom_navigation_bar.shifting_transition.$pump.png'), + skip: !Platform.isLinux, + ); + } + }); + testWidgets('BottomNavigationBar item title should not be nullable', (WidgetTester tester) async { expect(() {