Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flutter/lib/src/cupertino/bottom_tab_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class CupertinoTabBar extends StatelessWidget implements PreferredSizeWidget {
List<Widget> _buildSingleTabItem(BottomNavigationBarItem item, bool active) {
return <Widget>[
Expanded(child: Center(child: active ? item.activeIcon : item.icon)),
if (item.label != null) Text(item.label!),
if (item.label != null) Text(item.label!, semanticsLabel: item.semanticsLabel),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ class _Label extends StatelessWidget {
),
),
alignment: Alignment.bottomCenter,
child: Text(item.label!),
child: Text(item.label!, semanticsLabel: item.semanticsLabel),
),
);

Expand Down
13 changes: 13 additions & 0 deletions packages/flutter/lib/src/widgets/bottom_navigation_bar_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class BottomNavigationBarItem {
Widget? activeIcon,
this.backgroundColor,
this.tooltip,
this.semanticsLabel,
}) : activeIcon = activeIcon ?? icon;

/// A key to be passed through to the resultant widget.
Expand Down Expand Up @@ -104,4 +105,16 @@ class BottomNavigationBarItem {
///
/// Defaults to null, in which case the tooltip is not shown.
final String? tooltip;

/// The semantic label for this [BottomNavigationBarItem].
///
/// This is used by accessibility tools to describe the item. When provided,
/// it overrides the default [label] String when read by accessibility tools.
///
/// This is useful when the visual label does not fully describe the action
/// or destination, or when you want to provide additional context for
/// screen reader users.
///
/// If null, the default semantic description is used.
final String? semanticsLabel;
}
45 changes: 45 additions & 0 deletions packages/flutter/test/cupertino/bottom_tab_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,49 @@ Future<void> main() async {
);
expect(tester.getSize(find.byType(CupertinoTabBar)), Size.zero);
});

testWidgets('CupertinoTabBar item semanticsLabel overrides label for accessibility', (
WidgetTester tester,
) async {
final semantics = SemanticsTester(tester);

await pumpWidgetWithBoilerplate(
tester,
MediaQuery(
data: const MediaQueryData(),
child: CupertinoTabBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
label: 'A',
semanticsLabel: 'Custom A label',
),
BottomNavigationBarItem(
icon: ImageIcon(MemoryImage(Uint8List.fromList(kTransparentImage))),
label: 'B',
),
],
),
),
);

// Tab A should use the custom semanticsLabel
expect(
semantics,
includesNodeWith(
label: 'Custom A label',
hint: 'Tab 1 of 2',
flags: <SemanticsFlag>[SemanticsFlag.hasSelectedState, SemanticsFlag.isSelected],
textDirection: TextDirection.ltr,
),
);

// Tab B should use the default label since no semanticsLabel is provided
expect(
semantics,
includesNodeWith(label: 'B', hint: 'Tab 2 of 2', textDirection: TextDirection.ltr),
);

semantics.dispose();
});
}
24 changes: 24 additions & 0 deletions packages/flutter/test/material/bottom_navigation_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3102,6 +3102,30 @@ void main() {
final Finder xText = find.text('X');
expect(tester.getSize(xText).isEmpty, isTrue);
});

testWidgets('BottomNavigationBarItem.semanticsLabel overrides Text semantics', (
WidgetTester tester,
) async {
await tester.pumpWidget(
boilerplate(
textDirection: TextDirection.ltr,
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.ac_unit),
label: 'A',
semanticsLabel: 'Custom A label',
),
BottomNavigationBarItem(icon: Icon(Icons.access_alarm), label: 'B'),
],
),
),
);

expect(tester.getSemantics(find.text('A')), isSemantics(label: 'Custom A label\nTab 1 of 2'));

expect(tester.getSemantics(find.text('B')), isSemantics(label: 'B\nTab 2 of 2'));
});
}

Widget boilerplate({
Expand Down