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.
diff --git a/bin/internal/goldens.version b/bin/internal/goldens.version
index 14d768d..d489cc1 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 3046c74..2d90b40 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 @@
       )
     ];
 
-    for (NavigationIconView view in _navigationViews)
-      view.controller.addListener(_rebuild);
-
     _navigationViews[_currentIndex].controller.value = 1.0;
   }
 
@@ -166,12 +163,6 @@
     super.dispose();
   }
 
-  void _rebuild() {
-    setState(() {
-      // Rebuild in order to animate views.
-    });
-  }
-
   Widget _buildTransitionsStack() {
     final List<FadeTransition> transitions = <FadeTransition>[];
 
diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart
index 5513c0c..724b6ac 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 @@
       }
       _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<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 d499dfc..7f38742 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 @@
     expect(tester.widget<Material>(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>[
+                    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(() {