Skip to content

[MenuAnchor] fix: MenuAnchor submenu topStart alignment overlapping parent menu#187719

Open
lucas-goldner wants to merge 1 commit into
flutter:masterfrom
lucas-goldner:fix-submenu-topstart-position
Open

[MenuAnchor] fix: MenuAnchor submenu topStart alignment overlapping parent menu#187719
lucas-goldner wants to merge 1 commit into
flutter:masterfrom
lucas-goldner:fix-submenu-topstart-position

Conversation

@lucas-goldner

@lucas-goldner lucas-goldner commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Description

When a SubmenuButton inside a vertical menu uses AlignmentDirectional.topStart in its menuStyle, the submenu should open to the left of the parent menu (in LTR). Previously, it overlapped the parent menu because the x-position adjustment in _MenuLayout._positionChild() only considered RTL text direction, not the resolved alignment direction for same-axis submenus.

Before (bug)

The submenu's top-left corner was placed at the parent menu item's top-left corner, causing the submenu to overlap the parent menu

before.mov

After (fix)

The submenu's top-right edge aligns with the parent menu item's left edge, opening the submenu to the left as expected.

after.mov

What changed

In _positionChild(), the x-position adjustment now distinguishes between:

  • Same-axis submenus (e.g., vertical→vertical): Uses the resolved alignment's x component to determine growth direction. If resolvedAlignment.x < 0, the submenu grows leftward.
  • Cross-axis menus (e.g., MenuBar→dropdown): Keeps the existing RTL-based adjustment unchanged.

Fixes #181895

Pre-launch Checklist

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Jun 9, 2026
@google-cla

google-cla Bot commented Jun 9, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@lucas-goldner lucas-goldner changed the title Fix MenuAnchor submenu topStart alignment overlapping parent menu [MenuAnchor] fix: MenuAnchor submenu topStart alignment overlapping parent menu Jun 9, 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 the submenu positioning logic in MenuAnchor to correctly handle alignment for same-axis submenus by resolving the alignment against the text direction, and adds corresponding regression tests for both LTR and RTL layouts. The feedback suggests simplifying a switch statement on textDirection to an if statement to improve code readability.

Comment on lines +3466 to +3471
switch (textDirection) {
case TextDirection.rtl:
x -= childSize.width;
case TextDirection.ltr:
break;
}

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.

medium

Since textDirection is an enum with only two possible values (TextDirection.rtl and TextDirection.ltr), we can simplify this switch statement into a simple if statement. This improves readability and simplifies the code, adhering to the repository's general philosophy of optimizing for readability.

        if (textDirection == TextDirection.rtl) {
          x -= childSize.width;
        }
References
  1. Optimize for readability: Code is read more often than it is written. Suggest simplification and refactoring to enhance readability and maintainability. (link)

@lucas-goldner lucas-goldner force-pushed the fix-submenu-topstart-position branch from 64c0e47 to a463779 Compare June 9, 2026 04:30
@lucas-goldner

Copy link
Copy Markdown
Contributor Author

@ reviewer looking at this PR:

Hi! I noticed that the Material library in flutter/flutter has been frozen since April 7th as part of the decoupling effort. Since this PR modifies button_style_button.dart in the Material library, should I port this to the material_ui package in flutter/packages instead? Or should I keep this PR open here as-is until migration instructions are provided? Thanks!

@dkwingsmt

Copy link
Copy Markdown
Contributor

@lucas-goldner We can continue the review here and push as far as we can. When the freeze is lifted we will publish a guide on how to port the changes. :)

@dkwingsmt

Copy link
Copy Markdown
Contributor

@davidhicks980 Would you mind reviewing this PR?

@davidhicks980

Copy link
Copy Markdown
Contributor

Prior to this PR, the alignment specified the point on the anchor's surface that the top-left corner of the submenu was attached to: #181895 (comment)

This PR is flipping the horizontal attachment point for same-axis submenus. This is a clever fix, but I think it may lead to unpredictable code. For example (using the code from the PR):


New behavior:

With Alignment(0.1, -1):

image

With Alignment (-0.1, -1):

image

Previous behavior:

With Alignment(0.1, -1):

image

With Alignment (-0.1, -1):

image

If I was using MenuAnchor, I wouldn't expect a 0.2 change in horizontal alignment to flip my menu, especially when the menu isn't constrained by the screen. I think a better fix would be to add an additional alignment that lets users specify the attachment point on the menu itself. This is how most other menus and windowing systems behave:

https://material-web.dev/components/menu/#properties-2

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

Labels

f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MenuAnchor - SubmenuButton submenu pops at wrong position

3 participants