mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
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
This commit is contained in:
parent
70386a613b
commit
f4cb3d215d
@ -449,6 +449,7 @@ class _BottomNavigationTile extends StatelessWidget {
|
||||
this.item,
|
||||
this.animation,
|
||||
this.iconSize, {
|
||||
super.key,
|
||||
this.onTap,
|
||||
this.labelColorTween,
|
||||
this.iconColorTween,
|
||||
@ -1101,6 +1102,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
|
||||
widget.items[i],
|
||||
_animations[i],
|
||||
widget.iconSize,
|
||||
key: widget.items[i].key,
|
||||
selectedIconTheme: widget.useLegacyColorScheme ? widget.selectedIconTheme ?? bottomTheme.selectedIconTheme : effectiveSelectedIconTheme,
|
||||
unselectedIconTheme: widget.useLegacyColorScheme ? widget.unselectedIconTheme ?? bottomTheme.unselectedIconTheme : effectiveUnselectedIconTheme,
|
||||
selectedLabelStyle: effectiveSelectedLabelStyle,
|
||||
|
||||
@ -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].
|
||||
const BottomNavigationBarItem({
|
||||
this.key,
|
||||
required this.icon,
|
||||
this.label,
|
||||
Widget? activeIcon,
|
||||
@ -30,6 +31,14 @@ class BottomNavigationBarItem {
|
||||
this.tooltip,
|
||||
}) : 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.
|
||||
///
|
||||
/// Typically the icon is an [Icon] or an [ImageIcon] widget. If another type
|
||||
|
||||
@ -3174,6 +3174,34 @@ void main() {
|
||||
|
||||
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 }) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user