Skip to content

[material_ui] Fix Escape key not closing MenuAnchor opened by tap or click - #13430

Merged
auto-submit[bot] merged 3 commits into
flutter:mainfrom
mmohankrishna-lab:fix-menu-anchor-escape-195059
Oct 30, 2026
Merged

auto-submit[bot] merged 3 commits into
flutter:mainfrom
mmohankrishna-lab:fix-menu-anchor-escape-195059

Conversation

@mmohankrishna-lab

Copy link
Copy Markdown
Contributor

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

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. 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.

Footnotes

  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. ↩ ↩2

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Oct 26, 2026
@github-actions github-actions Bot added p: material_ui triage-design Should be looked at in design triage labels Oct 26, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies MenuAnchor to manage focus when opened by a tap or click, ensuring that pressing Escape closes the menu. It also adjusts the widget hierarchy in _Submenu and adds corresponding regression tests. Review feedback suggests adding a mounted check in the close animation's completion callback to prevent potential crashes upon disposal, removing redundant detach logic in dispose, and simplifying the focus request logic by removing dead code.

Comment thread packages/material_ui/lib/src/menu_anchor.dart Outdated
Comment thread packages/material_ui/lib/src/menu_anchor.dart Outdated
Comment thread packages/material_ui/lib/src/menu_anchor.dart Outdated
@mmohankrishna-lab mmohankrishna-lab self-assigned this Oct 26, 2026

@navaronbracke navaronbracke left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nit

Comment thread packages/material_ui/lib/src/menu_anchor.dart Outdated
@mmohankrishna-lab mmohankrishna-lab added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit
auto-submit Bot merged commit e830a53 into flutter:main Oct 30, 2026
13 checks passed
flutter-zl pushed a commit to flutter-zl/flutter that referenced this pull request Oct 30, 2026
…er#195686)

flutter/packages@0b72e92...3742a4e

2026-10-30 stuart.morgan@gmail.com [pigeon] Allow event channel
unregistration (flutter/packages#13438)
2026-10-30 mmohankrishna@google.com [material_ui] Fix Escape key not
closing MenuAnchor opened by tap or click (flutter/packages#13430)
2026-10-30 tarrinneal@gmail.com manual roll and manual build and copy
(flutter/packages#13495)
2026-10-29 tarrinneal@gmail.com [pigeon] Separate native interop Dart
bindings into a conditionally imported companion file
(flutter/packages#13393)
2026-10-29 victor.orozco@cloudsufi.com [image_picker_ios] PR 4/5 Migrate
the view provider, PHPicker save operation, and image utilities to Swift
(flutter/packages#13394)
2026-10-29 fluttergithubbot@gmail.com Sync release-material_ui-1.9.0 to
main (flutter/packages#13479)
2026-10-29 1961493+harryterkelsen@users.noreply.github.com [material_ui]
Unskip web tests that pass on CanvasKit and Skwasm: time_picker_test
(flutter/packages#13471)
2026-10-29 48776784+mackings@users.noreply.github.com [vector_graphics]
Dispose pictures decoded after their VectorGraphic is removed
(flutter/packages#13435)
2026-10-29 50643541+Mairramer@users.noreply.github.com
[camera_android_x] implement CameraX RecordingStats and VideoRecordEvent
APIs (flutter/packages#13330)
2026-10-29 fluttergithubbot@gmail.com Sync release-go_router-18.1.2 to
main (flutter/packages#13480)
2026-10-29 fluttergithubbot@gmail.com Sync release-cupertino_ui-1.1.5 to
main (flutter/packages#13481)
2026-10-29 34390549+rohanjariwala03@users.noreply.github.com
[vector_graphics_compiler] Fix em units in text positions
(flutter/packages#13388)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: material_ui triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[material_ui] Escape doesn't close a MenuAnchor menu opened by a tap or click

2 participants