From 5bf0192f58ee1e5f262b78fd371df28e5e87eacc Mon Sep 17 00:00:00 2001 From: hangyu Date: Thu, 27 Jun 2024 10:52:48 -0700 Subject: [PATCH] [a11y] Reland [#149375 ] Update semantics in dropdown.dart (#150578) issue: https://github.com/flutter/flutter/issues/117997 This is a partial reland of https://github.com/flutter/flutter/pull/149375 with updated g3fix cl/642457545 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../flutter/lib/src/material/dropdown.dart | 13 ++++++----- .../flutter/test/material/dropdown_test.dart | 22 ++++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index c2abe0d859..1f4a5e35fa 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -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), + ), ); } } @@ -1583,9 +1586,9 @@ class _DropdownButtonState extends State> 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( diff --git a/packages/flutter/test/material/dropdown_test.dart b/packages/flutter/test/material/dropdown_test.dart index c04ae2b1aa..135ef2dda6 100644 --- a/packages/flutter/test/material/dropdown_test.dart +++ b/packages/flutter/test/material/dropdown_test.dart @@ -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, @@ -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, @@ -1358,6 +1358,7 @@ void main() { label: 'one', textDirection: TextDirection.ltr, flags: [ + SemanticsFlag.isButton, SemanticsFlag.isFocused, SemanticsFlag.isFocusable, ], @@ -1367,21 +1368,30 @@ void main() { TestSemantics( label: 'two', textDirection: TextDirection.ltr, - flags: [SemanticsFlag.isFocusable], + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.isFocusable, + ], tags: [const SemanticsTag('RenderViewport.twoPane')], actions: [SemanticsAction.tap, SemanticsAction.focus], ), TestSemantics( label: 'three', textDirection: TextDirection.ltr, - flags: [SemanticsFlag.isFocusable], + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.isFocusable, + ], tags: [const SemanticsTag('RenderViewport.twoPane')], actions: [SemanticsAction.tap, SemanticsAction.focus], ), TestSemantics( label: 'four', textDirection: TextDirection.ltr, - flags: [SemanticsFlag.isFocusable], + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.isFocusable, + ], tags: [const SemanticsTag('RenderViewport.twoPane')], actions: [SemanticsAction.tap, SemanticsAction.focus], ),