Skip to content

Commit e830a53

Browse files
[material_ui] Fix Escape key not closing MenuAnchor opened by tap or click (flutter#13430)
Fixes an issue where pressing the `Escape` key does not close a `MenuAnchor` menu that was opened by a tap or click when `childFocusNode` is not provided. ### Root Cause 1. When a `MenuAnchor` is opened via a tap or click (e.g. from a `TextButton` or `IconButton`) without a `childFocusNode`, the button does not request focus on tap and `RawMenuAnchorState.open` only requests focus on `widget.childFocusNode`. As a result, `FocusManager.instance.primaryFocus` remains outside both `RawMenuAnchor` and `_Submenu`, so key events never route through their `Shortcuts` or `Actions` widgets. 2. In `_Submenu.build`, `FocusScope(node: anchor._menuScopeNode)` was placed above `Actions` and `Shortcuts`, so even when `_menuScopeNode` itself had primary focus, key events walked up past `FocusScope` without passing through `_Submenu`'s `Shortcuts` and `Actions`. ### Fix 1. Wrap `FocusScope(node: anchor._menuScopeNode)` inside `Actions` and `Shortcuts` in `_Submenu.build` so key events dispatched to `_menuScopeNode` walk up through `_kMenuTraversalShortcuts` and `DismissMenuAction`. 2. In `_MenuAnchorState._handleMenuOpenRequest`, when a root menu opens without a `childFocusNode` and focus is not already inside the `MenuAnchor` subtree, move focus to `_menuScopeNode` via `FocusScope.of(context).setFirstFocus(_menuScopeNode)` (and detach `_menuScopeNode` cleanly if closed/disposed before `FocusScope` mounts). Fixes flutter/flutter#195059 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent b9975c9 commit e830a53

3 files changed

Lines changed: 161 additions & 9 deletions

File tree

‎packages/material_ui/lib/src/menu_anchor.dart‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,24 @@ class _MenuAnchorState extends State<MenuAnchor> with SingleTickerProviderStateM
687687
}
688688

689689
_animationController.forward();
690+
691+
if (isSubmenu || widget.childFocusNode != null) {
692+
return;
693+
}
694+
695+
final FocusScopeNode scope = FocusScope.of(context);
696+
final BuildContext? focusedContext = scope.focusedChild?.context;
697+
final bool isAnchorFocused = focusedContext != null && _maybeOf(focusedContext)?._root == this;
698+
if (isAnchorFocused) {
699+
return;
700+
}
701+
702+
final FocusNode? initialFocus = FocusManager.instance.primaryFocus;
703+
SchedulerBinding.instance.addPostFrameCallback((Duration _) {
704+
if (mounted && !isClosingOrClosed && FocusManager.instance.primaryFocus == initialFocus) {
705+
scope.setFirstFocus(_menuScopeNode);
706+
}
707+
});
690708
}
691709

692710
void _handleMenuCloseRequest(VoidCallback hideOverlay) {
@@ -3943,15 +3961,15 @@ class _Submenu extends StatelessWidget {
39433961
child: MouseRegion(
39443962
cursor: mouseCursor,
39453963
hitTestBehavior: HitTestBehavior.deferToChild,
3946-
child: FocusScope(
3947-
node: anchor._menuScopeNode,
3948-
skipTraversal: true,
3949-
child: Actions(
3950-
actions: <Type, Action<Intent>>{
3951-
DismissIntent: DismissMenuAction(controller: anchor._menuController),
3952-
},
3953-
child: Shortcuts(
3954-
shortcuts: _kMenuTraversalShortcuts,
3964+
child: Actions(
3965+
actions: <Type, Action<Intent>>{
3966+
DismissIntent: DismissMenuAction(controller: anchor._menuController),
3967+
},
3968+
child: Shortcuts(
3969+
shortcuts: _kMenuTraversalShortcuts,
3970+
child: FocusScope(
3971+
node: anchor._menuScopeNode,
3972+
skipTraversal: true,
39553973
child: FadeTransition(
39563974
opacity: fadeAnimation,
39573975
alwaysIncludeSemantics: true,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes an issue where pressing Escape does not close a MenuAnchor opened by a tap or click.
3+
version: patch

‎packages/material_ui/test/menu_anchor_test.dart‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7202,6 +7202,137 @@ void main() {
72027202

72037203
handle.dispose();
72047204
});
7205+
7206+
// Regression test for https://github.com/flutter/flutter/issues/195059.
7207+
testWidgets('Escape closes MenuAnchor opened by tap without childFocusNode', (
7208+
WidgetTester tester,
7209+
) async {
7210+
final outsideFocusNode = FocusNode(debugLabel: 'Outside Focus');
7211+
addTearDown(outsideFocusNode.dispose);
7212+
7213+
await tester.pumpWidget(
7214+
MaterialApp(
7215+
home: Scaffold(
7216+
body: Column(
7217+
children: <Widget>[
7218+
TextButton(
7219+
focusNode: outsideFocusNode,
7220+
autofocus: true,
7221+
onPressed: () {},
7222+
child: const Text('Outside'),
7223+
),
7224+
MenuAnchor(
7225+
menuChildren: <Widget>[
7226+
MenuItemButton(onPressed: () {}, child: const Text('Item 1')),
7227+
MenuItemButton(onPressed: () {}, child: const Text('Item 2')),
7228+
],
7229+
builder: (BuildContext context, MenuController controller, Widget? child) {
7230+
return TextButton(
7231+
onPressed: () {
7232+
if (controller.isOpen) {
7233+
controller.close();
7234+
} else {
7235+
controller.open();
7236+
}
7237+
},
7238+
child: const Text('Open menu'),
7239+
);
7240+
},
7241+
),
7242+
],
7243+
),
7244+
),
7245+
),
7246+
);
7247+
7248+
await tester.pump();
7249+
expect(outsideFocusNode.hasPrimaryFocus, isTrue);
7250+
7251+
await tester.tap(find.text('Open menu'));
7252+
await tester.pumpAndSettle();
7253+
expect(find.text('Item 1'), findsOneWidget);
7254+
7255+
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
7256+
await tester.pumpAndSettle();
7257+
expect(find.text('Item 1'), findsNothing);
7258+
expect(outsideFocusNode.hasPrimaryFocus, isTrue);
7259+
7260+
// Reopen by tap, traverse with arrow key, and close with Escape.
7261+
await tester.tap(find.text('Open menu'));
7262+
await tester.pumpAndSettle();
7263+
expect(find.text('Item 1'), findsOneWidget);
7264+
7265+
listenForFocusChanges();
7266+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
7267+
await tester.pump();
7268+
expect(focusedMenu, equals('MenuItemButton(Text("Item 1"))'));
7269+
7270+
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
7271+
await tester.pumpAndSettle();
7272+
expect(find.text('Item 1'), findsNothing);
7273+
expect(outsideFocusNode.hasPrimaryFocus, isTrue);
7274+
});
7275+
7276+
// Regression test for https://github.com/flutter/flutter/issues/195059.
7277+
testWidgets(
7278+
'Escape closes MenuAnchor opened by tap inside a Dialog before dismissing the Dialog',
7279+
(WidgetTester tester) async {
7280+
await tester.pumpWidget(
7281+
MaterialApp(
7282+
home: Scaffold(
7283+
body: Builder(
7284+
builder: (BuildContext context) {
7285+
return TextButton(
7286+
onPressed: () {
7287+
showDialog<void>(
7288+
context: context,
7289+
builder: (BuildContext context) {
7290+
return AlertDialog(
7291+
title: const Text('Dialog Title'),
7292+
content: MenuAnchor(
7293+
menuChildren: <Widget>[
7294+
MenuItemButton(onPressed: () {}, child: const Text('Menu Item 1')),
7295+
],
7296+
builder:
7297+
(BuildContext context, MenuController controller, Widget? child) {
7298+
return TextButton(
7299+
onPressed: controller.open,
7300+
child: const Text('Open menu'),
7301+
);
7302+
},
7303+
),
7304+
);
7305+
},
7306+
);
7307+
},
7308+
child: const Text('Show Dialog'),
7309+
);
7310+
},
7311+
),
7312+
),
7313+
),
7314+
);
7315+
7316+
await tester.tap(find.text('Show Dialog'));
7317+
await tester.pumpAndSettle();
7318+
expect(find.text('Dialog Title'), findsOneWidget);
7319+
7320+
await tester.tap(find.text('Open menu'));
7321+
await tester.pumpAndSettle();
7322+
expect(find.text('Menu Item 1'), findsOneWidget);
7323+
7324+
// First Escape closes only the MenuAnchor menu, keeping the Dialog open.
7325+
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
7326+
await tester.pumpAndSettle();
7327+
expect(find.text('Menu Item 1'), findsNothing);
7328+
expect(find.text('Dialog Title'), findsOneWidget);
7329+
7330+
// Second Escape dismisses the Dialog.
7331+
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
7332+
await tester.pumpAndSettle();
7333+
expect(find.text('Dialog Title'), findsNothing);
7334+
},
7335+
);
72057336
}
72067337

72077338
List<Widget> createTestMenus({

0 commit comments

Comments
 (0)