Skip to content

Conversation

@QuncCccccc
Copy link
Contributor

@QuncCccccc QuncCccccc commented Feb 7, 2024

Fixes #142895

With the change of #143121, this PR is to add auto scroll to PopupMenuButton so when we open the menu, it will automatically scroll to the selected item.

auto_scroll_fix.mov

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Feb 7, 2024
@QuncCccccc QuncCccccc changed the title The selected item on popup menu is always visible even though the menu height is constrained The initial/selected item on popup menu should always be visible Feb 7, 2024
@QuncCccccc QuncCccccc marked this pull request as ready for review February 7, 2024 23:42
@QuncCccccc QuncCccccc requested a review from HansMuller February 8, 2024 00:03
Copy link
Contributor

@HansMuller HansMuller left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what the distinction between this and SchedulerBinding.instance.addPostFrameCallback is. Most of our code appears to use the SchedulerBinding version.

auto-submit bot pushed a commit that referenced this pull request Feb 8, 2024
Fixes #142896

The original code below is to always place the selected item above(overlap) the popup button so that the selected item can always be visible: https://github.com/flutter/flutter/blob/f8a77225f360556322934a9a831ffc9c9f3125da/packages/flutter/lib/src/material/popup_menu.dart#L723-L732

But when menu height is constrained and the menu itself is super long, the selected item still assumes there is enough space to push up all the items whose index is smaller than the selected index. As a result, every time when the menu is open, the calculation provides a different result to be the offset for the selected index,  and then with a constrained height, the menu looks jumping all over the place based on the different selected index.

https://github.com/flutter/flutter/assets/36861262/ad761f95-0ff5-4311-a81d-dac56df879c5

Even though the original calculation is to make the selected item visible when open the menu, the menu doesn't auto scroll and only expands itself as much as possible to show the selected one. In this case, if the screen it too small to show the selected item, we still cannot see it. This can be fixed by using `Scrollable.ensureVisible()`(#143118).

So we remove the calculation in this PR and the menu will always show up based on the top left of the anchor(button).

https://github.com/flutter/flutter/assets/36861262/03272f26-9440-4ac4-a701-9a0b41776ff9
@QuncCccccc QuncCccccc added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 8, 2024
Copy link
Contributor

@tvolkert tvolkert left a comment

Choose a reason for hiding this comment

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

Does this keep the behavior that the selected item will appear over the button when a PopupMenuButton is used?


final RelativeRect position;
final List<PopupMenuEntry<T>> items;
final List<GlobalKey>? itemKeys;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there any other users of _PopupMenuRoute? If not, this doesn't need to be nullable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. Thanks for catching this! Updating.

semanticLabel ??= MaterialLocalizations.of(context).popupMenuLabel;
}

final List<GlobalKey> menuItemKeys = itemKeys ?? List<GlobalKey>.generate(items.length, (int index) => GlobalKey());
Copy link
Contributor

Choose a reason for hiding this comment

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

If we create global keys here, don't we need to dispose them?

Copy link
Contributor

Choose a reason for hiding this comment

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

Weird - I thought you needed to dispose GlobalKeys, but it looks like I'm wrong - please disregard 🙂

required BuildContext context,
required RelativeRect position,
required List<PopupMenuEntry<T>> items,
List<GlobalKey>? itemKeys,
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the use case for exposing these in the API?

If we do expose them, please cover them in the dart docs above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry, I think we don't have to expose this. I used it to do some experiments but forgot to clean it up! Updating.

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Feb 12, 2024
@auto-submit
Copy link
Contributor

auto-submit bot commented Feb 12, 2024

auto label is removed for flutter/flutter/143118, due to This PR has not met approval requirements for merging. Changes were requested by {tvolkert}, please make the needed changes and resubmit this PR.
The PR author is a member of flutter-hackers and needs 0 more review(s) in order to merge this PR.

  • Merge guidelines: A PR needs at least one approved review if the author is already part of flutter-hackers or two member reviews if the author is not a flutter-hacker before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@QuncCccccc
Copy link
Contributor Author

QuncCccccc commented Feb 12, 2024

Does this keep the behavior that the selected item will appear over the button when a PopupMenuButton is used?

Yes, the selected item will still appear over the button but the menu position would be different. With this PR's change, the selected item will always on the top of the menu and the menu is aligned with the button. The difference is the top of the menu is always the same as the button top unless there is not enough space for menu, then the menu would try to show as much as contents like the original way.

I used the same demo👇 as the one shown in previous PR comment

video demo
Screen.Recording.2024-02-07.at.3.55.58.PM.mov

final _PopupMenuRoute<T> route;
final String? semanticLabel;
final BoxConstraints? constraints;
final List<GlobalKey>? itemKeys;
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this can be non-nullable now too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, just updated. Thanks!!

Copy link
Contributor

@tvolkert tvolkert left a comment

Choose a reason for hiding this comment

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

LGTM - thanks!

@tvolkert tvolkert added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 13, 2024
@auto-submit auto-submit bot merged commit 9bc8393 into flutter:master Feb 14, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 15, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 16, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 16, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Feb 16, 2024
Roll Flutter from a628814 to d7867ca (66 revisions)

flutter/flutter@a628814...d7867ca

2024-02-16 [email protected] Roll Packages from ef349be to c56c12d (5 revisions) (flutter/flutter#143581)
2024-02-16 [email protected] Update `MaterialStatesController` docs for calling `setState` in a listener  (flutter/flutter#143453)
2024-02-16 [email protected] Update `DataTable`  docs for  disabled `DataRow` ink well (flutter/flutter#143450)
2024-02-16 [email protected] Roll Flutter Engine from b7103bc8b374 to dd530f1556df (17 revisions) (flutter/flutter#143565)
2024-02-16 [email protected] Manual roll Flutter Engine from b7103bc8b374 to 7de84271eb65 (flutter/flutter#143564)
2024-02-16 [email protected] Manual roll Flutter Engine from d3c71d78f8ef to df0dc1fc06ca (flutter/flutter#143563)
2024-02-16 [email protected] Manual roll Flutter Engine from bc4dd534a0fa to d3c71d78f8ef (flutter/flutter#143561)
2024-02-16 [email protected] Manual roll Flutter Engine from edb2745e9834 to bc4dd534a0fa (flutter/flutter#143559)
2024-02-16 [email protected] Manual roll Flutter Engine from 15a358bbaf71 to edb2745e9834 (flutter/flutter#143555)
2024-02-16 [email protected] [devicelab] retain prior events for flutter gallery. (flutter/flutter#143554)
2024-02-16 [email protected] Reland "Disentangle and align flutter build web --wasm flags (#143517)" (flutter/flutter#143549)
2024-02-16 [email protected] Roll Flutter Engine from 3af336bfb2df to 15a358bbaf71 (1 revision) (flutter/flutter#143428)
2024-02-15 [email protected] Android Gradle file templates: make it easier to convert them to Kotlin DSL in the future (flutter/flutter#142146)
2024-02-15 [email protected] Remove bringup from win arm64 builds. (flutter/flutter#143548)
2024-02-15 [email protected] Fix minor spelling error (flutter/flutter#143541)
2024-02-15 [email protected] [devicelab] migrate new gallery benchmarks to local copy. (flutter/flutter#143545)
2024-02-15 49699333+dependabot[bot]@users.noreply.github.com Bump github/codeql-action from 3.24.1 to 3.24.3 (flutter/flutter#143546)
2024-02-15 [email protected] [flutter_tool] [dap] Forward Flutter progress events to DAP client (flutter/flutter#142524)
2024-02-15 [email protected] Swap the tasks that have been running fine for a while. (flutter/flutter#143544)
2024-02-15 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Disentangle and align `flutter build web --wasm` flags (#143517)" (flutter/flutter#143547)
2024-02-15 [email protected] Reland simulatedAccessibilityTraversal fix (flutter/flutter#143527)
2024-02-15 [email protected] Disentangle and align `flutter build web --wasm` flags (flutter/flutter#143517)
2024-02-15 [email protected] [devicelab] introduce new old gallery. (flutter/flutter#143486)
2024-02-15 [email protected] Remove certs dependency. (flutter/flutter#143495)
2024-02-15 98614782+auto-submit[bot]@users.noreply.github.com Reverts "Fix and test SemanticsController.simulatedAccessibilityTraversal (#143386)" (flutter/flutter#143523)
2024-02-15 [email protected] Roll Packages from a864254 to ef349be (9 revisions) (flutter/flutter#143521)
2024-02-15 [email protected] Modify `plugin_ffi` and `package_ffi` template (flutter/flutter#143376)
2024-02-15 [email protected] Remove certs installation from win_arm builds. (flutter/flutter#143487)
2024-02-14 98614782+auto-submit[bot]@users.noreply.github.com Reverts "[a11y] Add isEnabled semantics flag to  text field (#143334)" (flutter/flutter#143494)
2024-02-14 98614782+auto-submit[bot]@users.noreply.github.com Reverts "[a11y] Fix date picker cannot focus on the edit field (#143117)" (flutter/flutter#143493)
2024-02-14 [email protected] [a11y] Fix date picker cannot focus on the edit field (flutter/flutter#143117)
2024-02-14 [email protected] cleanup now-irrelevant ignores for `deprecated_member_use` (flutter/flutter#143403)
2024-02-14 [email protected] Fix and test SemanticsController.simulatedAccessibilityTraversal (flutter/flutter#143386)
2024-02-14 [email protected] Disable deprecation warnings for mega_gallery (flutter/flutter#143466)
2024-02-14 [email protected] The initial/selected item on popup menu should always be visible (flutter/flutter#143118)
2024-02-14 [email protected] Roll native_assets_builder to 0.5.0 (flutter/flutter#143472)
2024-02-14 [email protected] InputDecorator M3 test migration step2 (flutter/flutter#143369)
2024-02-14 [email protected] Add more documentation for TextEditingController default constructor (flutter/flutter#143452)
2024-02-14 [email protected] Format all kotlin according to ktlint (flutter/flutter#143390)
2024-02-14 [email protected] Marks Linux_pixel_7pro integration_ui_keyboard_resize to be unflaky (flutter/flutter#143440)
2024-02-14 [email protected] Roll Packages from 9385bbb to a864254 (6 revisions) (flutter/flutter#143454)
2024-02-14 [email protected] [tools] Add column header for emulators information (flutter/flutter#142853)
2024-02-14 [email protected] Use `dart compile wasm` for wasm compilations (flutter/flutter#143298)
2024-02-14 [email protected] [devicelab] retain first frame data in certain integration tests. (flutter/flutter#143419)
2024-02-14 [email protected] Roll Flutter Engine from 0849250a1419 to 3af336bfb2df (2 revisions) (flutter/flutter#143427)
2024-02-14 [email protected] Roll Flutter Engine from 215d55f4f82d to 0849250a1419 (2 revisions) (flutter/flutter#143425)
...
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 14, 2024
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 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.

[Material] showMenu() doesn't scroll initial item to visible

3 participants