Skip to content

Commit

Permalink
[a11y] Add semantics: button to bottom navigation bar items and dropd…
Browse files Browse the repository at this point in the history
…own menu items (#149375)

issue: flutter/flutter#117997
  • Loading branch information
hannah-hyj authored Jun 17, 2024
1 parent 64b99b5 commit 21a12f6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ class _BottomNavigationTile extends StatelessWidget {

result = Semantics(
selected: selected,
button: true,
container: true,
child: Stack(
children: <Widget>[
Expand Down
13 changes: 8 additions & 5 deletions packages/flutter/lib/src/material/dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,12 @@ class _DropdownMenuItemContainer extends StatelessWidget {

@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints: const BoxConstraints(minHeight: _kMenuItemHeight),
child: Align(alignment: alignment, child: child),
return Semantics(
button: true,
child: ConstrainedBox(
constraints: const BoxConstraints(minHeight: _kMenuItemHeight),
child: Align(alignment: alignment, child: child),
),
);
}
}
Expand Down Expand Up @@ -1583,9 +1586,9 @@ class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindi
child: result,
);
}

final bool childHasButtonSemantic = hintIndex != null || (_selectedIndex != null && widget.selectedItemBuilder == null);
return Semantics(
button: true,
button: !childHasButtonSemantic,
child: Actions(
actions: _actionMap,
child: InkWell(
Expand Down
16 changes: 15 additions & 1 deletion packages/flutter/test/material/bottom_navigation_bar_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,7 @@ void main() {
matchesSemantics(
label: 'AC\nTab 1 of 3',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
isSelected: true,
hasTapAction: true,
Expand All @@ -2119,6 +2120,7 @@ void main() {
matchesSemantics(
label: 'Alarm\nTab 2 of 3',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
Expand All @@ -2129,6 +2131,7 @@ void main() {
matchesSemantics(
label: 'Hot Tub\nTab 3 of 3',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
Expand Down Expand Up @@ -2165,6 +2168,7 @@ void main() {
matchesSemantics(
label: 'AC\nTab 1 of 3',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
isSelected: true,
hasTapAction: true,
Expand All @@ -2176,6 +2180,7 @@ void main() {
matchesSemantics(
label: 'Alarm\nTab 2 of 3',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
Expand All @@ -2186,6 +2191,7 @@ void main() {
matchesSemantics(
label: 'Hot Tub\nTab 3 of 3',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
Expand Down Expand Up @@ -2518,6 +2524,7 @@ void main() {
matchesSemantics(
label: 'Red\nTab 1 of 2',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
isSelected: true,
hasTapAction: true,
Expand All @@ -2529,6 +2536,7 @@ void main() {
matchesSemantics(
label: 'Green\nTab 2 of 2',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
Expand Down Expand Up @@ -2563,6 +2571,7 @@ void main() {
matchesSemantics(
label: 'Red\nTab 1 of 2',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
isSelected: true,
hasTapAction: true,
Expand All @@ -2574,6 +2583,7 @@ void main() {
matchesSemantics(
label: 'Green\nTab 2 of 2',
textDirection: TextDirection.ltr,
isButton: true,
isFocusable: true,
hasTapAction: true,
hasFocusAction: true,
Expand Down Expand Up @@ -2754,6 +2764,7 @@ void main() {
children: <TestSemantics>[
TestSemantics(
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.isSelected,
SemanticsFlag.isFocusable,
],
Expand All @@ -2762,7 +2773,10 @@ void main() {
textDirection: TextDirection.ltr,
),
TestSemantics(
flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.isFocusable,
],
actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.focus],
label: 'B\nTab 2 of 2',
textDirection: TextDirection.ltr,
Expand Down
22 changes: 16 additions & 6 deletions packages/flutter/test/material/dropdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ void main() {
));

// By default the hint contributes the label.
expect(tester.getSemantics(find.byKey(key)), matchesSemantics(
expect(tester.getSemantics(find.text('test')), matchesSemantics(
isButton: true,
label: 'test',
hasTapAction: true,
Expand All @@ -1311,8 +1311,8 @@ void main() {
hint: const Text('test'),
));

// Displays label of select item and is no longer tappable.
expect(tester.getSemantics(find.byKey(key)), matchesSemantics(
// Displays label of select item.
expect(tester.getSemantics(find.text('three')), matchesSemantics(
isButton: true,
label: 'three',
hasTapAction: true,
Expand Down Expand Up @@ -1358,6 +1358,7 @@ void main() {
label: 'one',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.isFocused,
SemanticsFlag.isFocusable,
],
Expand All @@ -1367,21 +1368,30 @@ void main() {
TestSemantics(
label: 'two',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.isFocusable,
],
tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.focus],
),
TestSemantics(
label: 'three',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.isFocusable,
],
tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.focus],
),
TestSemantics(
label: 'four',
textDirection: TextDirection.ltr,
flags: <SemanticsFlag>[SemanticsFlag.isFocusable],
flags: <SemanticsFlag>[
SemanticsFlag.isButton,
SemanticsFlag.isFocusable,
],
tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.focus],
),
Expand Down

0 comments on commit 21a12f6

Please sign in to comment.