From a4de77a239c4b6574350905f68419402569ccf3a Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Fri, 26 May 2023 13:23:45 -0700 Subject: [PATCH] Remove button announcement for `MenuItemButton` and `SubmenuButton` (#127620) --- .../lib/src/material/button_style_button.dart | 12 ++- .../flutter/lib/src/material/menu_anchor.dart | 2 + .../flutter/lib/src/material/text_button.dart | 1 + .../test/material/menu_anchor_test.dart | 81 +++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/button_style_button.dart b/packages/flutter/lib/src/material/button_style_button.dart index f35d10bf48..9155e49ef6 100644 --- a/packages/flutter/lib/src/material/button_style_button.dart +++ b/packages/flutter/lib/src/material/button_style_button.dart @@ -42,6 +42,7 @@ abstract class ButtonStyleButton extends StatefulWidget { required this.autofocus, required this.clipBehavior, this.statesController, + this.isSemanticButton = true, required this.child, }); @@ -100,6 +101,15 @@ abstract class ButtonStyleButton extends StatefulWidget { /// {@macro flutter.material.inkwell.statesController} final MaterialStatesController? statesController; + /// Determine whether this subtree represents a button. + /// + /// If this is null, the screen reader will not announce "button" when this + /// is focused. This is useful for [MenuItemButton] and [SubmenuButton] when we + /// traverse the menu system. + /// + /// Defaults to true. + final bool? isSemanticButton; + /// Typically the button's label. /// /// {@macro flutter.widgets.ProxyWidget.child} @@ -425,7 +435,7 @@ class _ButtonStyleState extends State with TickerProviderStat return Semantics( container: true, - button: true, + button: widget.isSemanticButton, enabled: widget.enabled, child: _InputPadding( minSize: minSize, diff --git a/packages/flutter/lib/src/material/menu_anchor.dart b/packages/flutter/lib/src/material/menu_anchor.dart index 856222f387..2c4dfae39f 100644 --- a/packages/flutter/lib/src/material/menu_anchor.dart +++ b/packages/flutter/lib/src/material/menu_anchor.dart @@ -1062,6 +1062,7 @@ class _MenuItemButtonState extends State { style: mergedStyle, statesController: widget.statesController, clipBehavior: widget.clipBehavior, + isSemanticButton: null, child: _MenuItemLabel( leadingIcon: widget.leadingIcon, shortcut: widget.shortcut, @@ -1903,6 +1904,7 @@ class _SubmenuButtonState extends State { focusNode: _buttonFocusNode, onHover: _enabled ? (bool hovering) => handleHover(hovering, context) : null, onPressed: _enabled ? () => toggleShowMenu(context) : null, + isSemanticButton: null, child: _MenuItemLabel( leadingIcon: widget.leadingIcon, trailingIcon: widget.trailingIcon, diff --git a/packages/flutter/lib/src/material/text_button.dart b/packages/flutter/lib/src/material/text_button.dart index 97b89254da..1c8da19188 100644 --- a/packages/flutter/lib/src/material/text_button.dart +++ b/packages/flutter/lib/src/material/text_button.dart @@ -87,6 +87,7 @@ class TextButton extends ButtonStyleButton { super.autofocus = false, super.clipBehavior = Clip.none, super.statesController, + super.isSemanticButton, required Widget super.child, }); diff --git a/packages/flutter/test/material/menu_anchor_test.dart b/packages/flutter/test/material/menu_anchor_test.dart index 0703202aae..5e523b05f8 100644 --- a/packages/flutter/test/material/menu_anchor_test.dart +++ b/packages/flutter/test/material/menu_anchor_test.dart @@ -10,6 +10,7 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; +import '../widgets/semantics_tester.dart'; void main() { late MenuController controller; @@ -3040,6 +3041,86 @@ void main() { expect(radioValue, 1); }); }); + + group('Semantics', () { + testWidgets('MenuItemButton is not a semantic button', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: MenuItemButton( + style: MenuItemButton.styleFrom(fixedSize: const Size(88.0, 36.0)), + onPressed: () { }, + child: const Text('ABC'), + ), + ), + ), + ); + + // The flags should not have SemanticsFlag.isButton + expect(semantics, hasSemantics( + TestSemantics.root( + children: [ + TestSemantics.rootChild( + actions: [ + SemanticsAction.tap, + ], + label: 'ABC', + rect: const Rect.fromLTRB(0.0, 0.0, 88.0, 48.0), + transform: Matrix4.translationValues(356.0, 276.0, 0.0), + flags: [ + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + ], + textDirection: TextDirection.ltr, + ), + ], + ), + ignoreId: true, + )); + + semantics.dispose(); + }); + + testWidgets('SubMenuButton is not a semantic button', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SubmenuButton( + onHover: (bool value) {}, + style: SubmenuButton.styleFrom(fixedSize: const Size(88.0, 36.0)), + menuChildren: const [], + child: const Text('ABC'), + ), + ), + ), + ); + + // The flags should not have SemanticsFlag.isButton + expect(semantics, hasSemantics( + TestSemantics.root( + children: [ + TestSemantics.rootChild( + label: 'ABC', + rect: const Rect.fromLTRB(0.0, 0.0, 88.0, 48.0), + transform: Matrix4.translationValues(356.0, 276.0, 0.0), + flags: [ + SemanticsFlag.hasEnabledState, + ], + textDirection: TextDirection.ltr, + ), + ], + ), + ignoreId: true, + )); + + semantics.dispose(); + }); + }); } List createTestMenus({