Skip to content

[material_ui] Fix positioned MenuAnchor menus opening away from the position near screen edges - #13772

Open
muralikrishmt wants to merge 1 commit into
flutter:mainfrom
muralikrishmt:fix-menu-anchor-position-146086
Open

muralikrishmt wants to merge 1 commit into
flutter:mainfrom
muralikrishmt:fix-menu-anchor-position-146086

Conversation

@muralikrishmt

@muralikrishmt muralikrishmt commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

When a menu is opened at a position with MenuController.open(position: ...), such as a context menu opened at the pointer, and it doesn't fit below that position, it is moved above the top of the whole MenuAnchor child instead of above the position. The larger the anchor (for example, a list item, or a whole page used as a context menu region), the further the menu ends up from the pointer. If there is no room above the anchor, the menu is shifted to fit and covers the pointer instead.

This happens because _MenuLayout._positionChild always computes the fallback positions from the bounds of the anchor. That is right for menus attached to a button, but not for menus attached to a position.

As suggested in flutter/flutter#146086, this PR treats the position as an empty anchor rectangle for positioned menus:

  • A menu that doesn't fit below the position opens just above it. The other fallbacks also use the position. For example, a positioned menu inside another menu that doesn't fit to the right of the position opens to the left of it.
  • alignmentOffset isn't applied when the menu is moved to the other side of the position, since it isn't applied to positioned menus otherwise.
  • On foldable devices, the display area that the menu is kept in is chosen based on the position instead of the center of the anchor.
  • When animated is true, a menu that opens above the position grows upward from the position, so it still starts animating at the pointer, as intended by flutter/flutter%23182932.

Menus that aren't opened at a position are not affected. A top-level positioned menu that doesn't fit to the right of the position is still shifted to fit, as before, so the existing menu position in LTR and menu position in RTL tests pass unchanged.

Six tests are added. Without the fix, five of them fail. The sixth covers the existing behavior when the menu fits neither above nor below the position.

Before:

pr_146086_before

After:

pr_146086_after

Fixes flutter/flutter#146086

%23%23 Pre-Review Checklist

If you need help, consider asking for advice on the %23hackers-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

…osition near screen edges

When a menu opened with `MenuController.open(position: ...)` doesn't fit,
place it relative to the position instead of the bounds of the anchor, so
that, for example, a context menu near the bottom of the screen opens just
above the pointer instead of above the whole anchor.

Fixes flutter/flutter#146086
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Nov 24, 2026
@github-actions github-actions Bot added p: material_ui triage-design Should be looked at in design triage P2 Important issues not at the top of the work list labels Nov 24, 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 updates MenuAnchor to ensure that menus opened at a specific position via MenuController.open are positioned and flipped relative to that position rather than the entire anchor. It introduces attachmentRect and attachmentOffset in _MenuLayout._positionChild to handle these calculations and ensures that positioned menus grow upward from their position during animation if they open above it. Comprehensive tests have been added to verify these behaviors under various constraints. There are no review comments, and I have no feedback to provide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD p: material_ui P2 Important issues not at the top of the work list triage-design Should be looked at in design triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MenuAnchor controller open method bugs at the edge of the screen

1 participant