-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Fix menu anchor state handling #157612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix menu anchor state handling #157612
Conversation
This commit refactors the `_MenuAnchorState` class in `menu_anchor.dart` to include a check for the mounted state and the scheduler phase before calling `setState()`. This ensures that UI updates are only performed when the widget is still mounted and not during the persistent callbacks phase. Additionally, a new test case is added in `menu_anchor_test.dart` to verify that the `isOpen` state of the `MenuAnchor` widget is updated correctly when the button is pressed. Fix: flutter#157606
nate-thegrate
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix, this looks awesome!
| if (mounted && SchedulerBinding.instance.schedulerPhase != SchedulerPhase.persistentCallbacks) { | ||
| setState(() { | ||
| // Mark dirty to ensure UI updates | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no idea that the SchedulerPhase could be used this way, thanks!
|
Actually, quick question before we land this one—there looks to be a flutter/packages/flutter/lib/src/material/menu_anchor.dart Lines 2533 to 2551 in add7e81
Currently it doesn't look like the inherited widget rebuilds when the menu is opened; maybe we should remove the |
|
The _MenuAnchorScope is indeed designed to notify its dependents when the isOpen state changes, but this notification doesn't directly trigger a rebuild of the builder. Instead, it triggers the didChangeDependencies method in the dependent widgets. |
True that! I think what I'm trying to get to is: this bug indicates that Perhaps it'd be good to remove the |
|
Consider a scenario where we're not using the builder pattern, but instead using a simple child widget. After this PR is merged, such a child widget would still be able to detect updates through didChangeDependencies. |
Perhaps so, though we'd need some way to let the child depend on My concern is: context.dependOnInheritedWidgetOfExactType<_MenuAnchorScope>()!.isOpen;In order for this value to be accurate, the inherited widget needs to be rebuilt when the controller opens/closes. And since rebuilding the Since it's a private class and |
|
I don't believe that While I understand that I think we may be straying too far from the original intent of this PR. Perhaps it would be better to open a separate issue to discuss this particular concern in more depth. |
Fair point, especially if we end up removing the If we decide to keep it, however, updating |
|
I see your point. However, I think there's an important aspect to consider here. Without this PR, the |
100% agree. In my mind I was thinking "if What didn't connect for me till just now: since this change calls |
bleroux
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for the contribution.
flutter/flutter@42132e8...fe71cad 2024-10-30 [email protected] Update CHANGELOG.md to correct ios vs macos issue (flutter/flutter#157822) 2024-10-30 [email protected] Add ability to customize the default `Slider` padding (flutter/flutter#156143) 2024-10-30 [email protected] Fix menu anchor state handling (flutter/flutter#157612) 2024-10-30 [email protected] Add test for `interactive_viewer.0.dart` (flutter/flutter#157773) 2024-10-30 [email protected] Add test for `scroll_metrics_notification.0.dart` (flutter/flutter#157768) 2024-10-30 [email protected] Add boundary feature to the drag gesture. (flutter/flutter#147521) 2024-10-30 [email protected] Fix `ResizeImage` documentation (flutter/flutter#157619) 2024-10-29 [email protected] Roll Flutter Engine from 795b5492f1b9 to 999797a2f690 (1 revision) (flutter/flutter#157825) 2024-10-29 [email protected] Avoid labeling all PRs as 'text-input' (flutter/flutter#157805) 2024-10-29 [email protected] Roll Packages from e0c4f55 to 028027e (8 revisions) (flutter/flutter#157813) 2024-10-29 [email protected] Roll Flutter Engine from 725c8e4bc379 to 795b5492f1b9 (5 revisions) (flutter/flutter#157820) 2024-10-29 [email protected] Fix and remove a few `no-shuffle` tags in `flutter_tools`. (flutter/flutter#157656) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: 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
This commit refactors the
_MenuAnchorStateclass inmenu_anchor.dartto include a check for the mounted state and the scheduler phase before callingsetState(). This ensures that UI updates are only performed when the widget is still mounted and not during the persistent callbacks phase. Additionally, a new test case is added inmenu_anchor_test.dartto verify that theisOpenstate of theMenuAnchorwidget is updated correctly when the button is pressed.Fix: #157606
Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.
List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.
If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.