Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions packages/material_ui/lib/src/menu_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,24 @@ class _MenuAnchorState extends State<MenuAnchor> with SingleTickerProviderStateM
}

_animationController.forward();

if (isSubmenu || widget.childFocusNode != null) {
return;
}

final FocusScopeNode scope = FocusScope.of(context);
final BuildContext? focusedContext = scope.focusedChild?.context;
final bool isAnchorFocused = focusedContext != null && _maybeOf(focusedContext)?._root == this;
if (isAnchorFocused) {
return;
}

final FocusNode? initialFocus = FocusManager.instance.primaryFocus;
SchedulerBinding.instance.addPostFrameCallback((Duration _) {
if (mounted && !isClosingOrClosed && FocusManager.instance.primaryFocus == initialFocus) {
scope.setFirstFocus(_menuScopeNode);
}
});
}

void _handleMenuCloseRequest(VoidCallback hideOverlay) {
Expand Down Expand Up @@ -3934,15 +3952,15 @@ class _Submenu extends StatelessWidget {
child: MouseRegion(
cursor: mouseCursor,
hitTestBehavior: HitTestBehavior.deferToChild,
child: FocusScope(
node: anchor._menuScopeNode,
skipTraversal: true,
child: Actions(
actions: <Type, Action<Intent>>{
DismissIntent: DismissMenuAction(controller: anchor._menuController),
},
child: Shortcuts(
shortcuts: _kMenuTraversalShortcuts,
child: Actions(
actions: <Type, Action<Intent>>{
DismissIntent: DismissMenuAction(controller: anchor._menuController),
},
child: Shortcuts(
shortcuts: _kMenuTraversalShortcuts,
child: FocusScope(
node: anchor._menuScopeNode,
skipTraversal: true,
child: FadeTransition(
opacity: fadeAnimation,
alwaysIncludeSemantics: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Fixes an issue where pressing Escape does not close a MenuAnchor opened by a tap or click.
version: patch
131 changes: 131 additions & 0 deletions packages/material_ui/test/menu_anchor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7077,6 +7077,137 @@ void main() {

handle.dispose();
});

// Regression test for https://github.com/flutter/flutter/issues/195059.
testWidgets('Escape closes MenuAnchor opened by tap without childFocusNode', (
WidgetTester tester,
) async {
final outsideFocusNode = FocusNode(debugLabel: 'Outside Focus');
addTearDown(outsideFocusNode.dispose);

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Column(
children: <Widget>[
TextButton(
focusNode: outsideFocusNode,
autofocus: true,
onPressed: () {},
child: const Text('Outside'),
),
MenuAnchor(
menuChildren: <Widget>[
MenuItemButton(onPressed: () {}, child: const Text('Item 1')),
MenuItemButton(onPressed: () {}, child: const Text('Item 2')),
],
builder: (BuildContext context, MenuController controller, Widget? child) {
return TextButton(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text('Open menu'),
);
},
),
],
),
),
),
);

await tester.pump();
expect(outsideFocusNode.hasPrimaryFocus, isTrue);

await tester.tap(find.text('Open menu'));
await tester.pumpAndSettle();
expect(find.text('Item 1'), findsOneWidget);

await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.text('Item 1'), findsNothing);
expect(outsideFocusNode.hasPrimaryFocus, isTrue);

// Reopen by tap, traverse with arrow key, and close with Escape.
await tester.tap(find.text('Open menu'));
await tester.pumpAndSettle();
expect(find.text('Item 1'), findsOneWidget);

listenForFocusChanges();
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(focusedMenu, equals('MenuItemButton(Text("Item 1"))'));

await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.text('Item 1'), findsNothing);
expect(outsideFocusNode.hasPrimaryFocus, isTrue);
});

// Regression test for https://github.com/flutter/flutter/issues/195059.
testWidgets(
'Escape closes MenuAnchor opened by tap inside a Dialog before dismissing the Dialog',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
return TextButton(
onPressed: () {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Dialog Title'),
content: MenuAnchor(
menuChildren: <Widget>[
MenuItemButton(onPressed: () {}, child: const Text('Menu Item 1')),
],
builder:
(BuildContext context, MenuController controller, Widget? child) {
return TextButton(
onPressed: controller.open,
child: const Text('Open menu'),
);
},
),
);
},
);
},
child: const Text('Show Dialog'),
);
},
),
),
),
);

await tester.tap(find.text('Show Dialog'));
await tester.pumpAndSettle();
expect(find.text('Dialog Title'), findsOneWidget);

await tester.tap(find.text('Open menu'));
await tester.pumpAndSettle();
expect(find.text('Menu Item 1'), findsOneWidget);

// First Escape closes only the MenuAnchor menu, keeping the Dialog open.
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.text('Menu Item 1'), findsNothing);
expect(find.text('Dialog Title'), findsOneWidget);

// Second Escape dismisses the Dialog.
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.text('Dialog Title'), findsNothing);
},
);
}

List<Widget> createTestMenus({
Expand Down
Loading