Unverified Commit f4cb3d21 authored by Jack Gibbons's avatar Jack Gibbons Committed by GitHub

Add key to BottomNavigationBarItem (#139617)

Pass key into _BottomNavigationTile

Adding a optional key parameter to BottomNavigationBarItem to be passed through to _BottomNavigationTile.
This will allow easier testing in some scenarios, and fix the splash appearing on the wrong tile.

https://github.com/flutter/flutter/issues/139615
https://github.com/flutter/flutter/issues/34833
parent 70386a61
...@@ -449,6 +449,7 @@ class _BottomNavigationTile extends StatelessWidget { ...@@ -449,6 +449,7 @@ class _BottomNavigationTile extends StatelessWidget {
this.item, this.item,
this.animation, this.animation,
this.iconSize, { this.iconSize, {
super.key,
this.onTap, this.onTap,
this.labelColorTween, this.labelColorTween,
this.iconColorTween, this.iconColorTween,
...@@ -1101,6 +1102,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr ...@@ -1101,6 +1102,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
widget.items[i], widget.items[i],
_animations[i], _animations[i],
widget.iconSize, widget.iconSize,
key: widget.items[i].key,
selectedIconTheme: widget.useLegacyColorScheme ? widget.selectedIconTheme ?? bottomTheme.selectedIconTheme : effectiveSelectedIconTheme, selectedIconTheme: widget.useLegacyColorScheme ? widget.selectedIconTheme ?? bottomTheme.selectedIconTheme : effectiveSelectedIconTheme,
unselectedIconTheme: widget.useLegacyColorScheme ? widget.unselectedIconTheme ?? bottomTheme.unselectedIconTheme : effectiveUnselectedIconTheme, unselectedIconTheme: widget.useLegacyColorScheme ? widget.unselectedIconTheme ?? bottomTheme.unselectedIconTheme : effectiveUnselectedIconTheme,
selectedLabelStyle: effectiveSelectedLabelStyle, selectedLabelStyle: effectiveSelectedLabelStyle,
......
...@@ -23,6 +23,7 @@ class BottomNavigationBarItem { ...@@ -23,6 +23,7 @@ class BottomNavigationBarItem {
/// ///
/// The argument [icon] should not be null and the argument [label] should not be null when used in a Material Design's [BottomNavigationBar]. /// The argument [icon] should not be null and the argument [label] should not be null when used in a Material Design's [BottomNavigationBar].
const BottomNavigationBarItem({ const BottomNavigationBarItem({
this.key,
required this.icon, required this.icon,
this.label, this.label,
Widget? activeIcon, Widget? activeIcon,
...@@ -30,6 +31,14 @@ class BottomNavigationBarItem { ...@@ -30,6 +31,14 @@ class BottomNavigationBarItem {
this.tooltip, this.tooltip,
}) : activeIcon = activeIcon ?? icon; }) : activeIcon = activeIcon ?? icon;
/// A key to be passed through to the resultant widget.
///
/// This allows the identification of different [BottomNavigationBarItem]s through their keys.
///
/// When changing the number of bar items in response to a bar item being tapped, giving
/// each item a key will allow the inkwell / splash animation to be correctly positioned.
final Key? key;
/// The icon of the item. /// The icon of the item.
/// ///
/// Typically the icon is an [Icon] or an [ImageIcon] widget. If another type /// Typically the icon is an [Icon] or an [ImageIcon] widget. If another type
......
...@@ -3174,6 +3174,34 @@ void main() { ...@@ -3174,6 +3174,34 @@ void main() {
addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetPhysicalSize);
}); });
testWidgets('BottomNavigationBar keys passed through', (WidgetTester tester) async {
const Key key1 = Key('key1');
const Key key2 = Key('key2');
await tester.pumpWidget(
boilerplate(
textDirection: TextDirection.ltr,
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
key: key1,
icon: Icon(Icons.favorite_border),
label: 'Favorite',
),
BottomNavigationBarItem(
key: key2,
icon: Icon(Icons.access_alarm),
label: 'Alarm',
),
],
),
),
);
expect(find.byKey(key1), findsOneWidget);
expect(find.byKey(key2), findsOneWidget);
});
} }
Widget boilerplate({ Widget? bottomNavigationBar, required TextDirection textDirection, bool? useMaterial3 }) { Widget boilerplate({ Widget? bottomNavigationBar, required TextDirection textDirection, bool? useMaterial3 }) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment