From 186c6210554f725f53745be81d5a184dca560fcd Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Tue, 28 Jul 2020 10:26:10 -0700 Subject: [PATCH] makes PopupMenuitem merge the semantics of its child (#62062) --- .../flutter/lib/src/material/popup_menu.dart | 20 +++-- .../test/material/popup_menu_test.dart | 84 +++++++++++++++++++ 2 files changed, 95 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 845c16247401..92338a3a781e 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -342,15 +342,17 @@ class PopupMenuItemState> extends State { }, ); - return Semantics( - enabled: widget.enabled, - button: true, - child: InkWell( - onTap: widget.enabled ? handleTap : null, - canRequestFocus: widget.enabled, - mouseCursor: effectiveMouseCursor, - child: item, - ), + return MergeSemantics( + child: Semantics( + enabled: widget.enabled, + button: true, + child: InkWell( + onTap: widget.enabled ? handleTap : null, + canRequestFocus: widget.enabled, + mouseCursor: effectiveMouseCursor, + child: item, + ), + ) ); } } diff --git a/packages/flutter/test/material/popup_menu_test.dart b/packages/flutter/test/material/popup_menu_test.dart index a17845c1cf9f..30e1bb717407 100644 --- a/packages/flutter/test/material/popup_menu_test.dart +++ b/packages/flutter/test/material/popup_menu_test.dart @@ -836,6 +836,90 @@ void main() { semantics.dispose(); }); + testWidgets('PopupMenuItem merges the semantics of its descendants', (WidgetTester tester) async { + final SemanticsTester semantics = SemanticsTester(tester); + await tester.pumpWidget( + MaterialApp( + home: Material( + child: PopupMenuButton( + itemBuilder: (BuildContext context) { + return >[ + PopupMenuItem( + value: 1, + child: Row( + children: [ + Semantics( + child: const Text('test1'), + ), + Semantics( + child: const Text('test2'), + ), + ], + ), + ), + ]; + }, + child: const SizedBox( + height: 100.0, + width: 100.0, + child: Text('XXX'), + ), + ), + ), + ), + ); + await tester.tap(find.text('XXX')); + await tester.pumpAndSettle(); + + expect(semantics, hasSemantics( + TestSemantics.root( + children: [ + TestSemantics( + textDirection: TextDirection.ltr, + children: [ + TestSemantics( + children: [ + TestSemantics( + flags: [ + SemanticsFlag.scopesRoute, + SemanticsFlag.namesRoute, + ], + label: 'Popup menu', + textDirection: TextDirection.ltr, + children: [ + TestSemantics( + flags: [ + SemanticsFlag.hasImplicitScrolling, + ], + children: [ + TestSemantics( + flags: [ + SemanticsFlag.isButton, + SemanticsFlag.hasEnabledState, + SemanticsFlag.isEnabled, + SemanticsFlag.isFocusable, + ], + actions: [SemanticsAction.tap], + label: 'test1\ntest2', + textDirection: TextDirection.ltr, + ), + ], + ), + ], + ), + ], + ), + TestSemantics(), + ], + ), + ], + ), + ignoreId: true, ignoreTransform: true, ignoreRect: true, + )); + + semantics.dispose(); + }); + testWidgets('disabled PopupMenuItem has correct semantics', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/45044. final SemanticsTester semantics = SemanticsTester(tester);