Skip to content

Commit d18ad52

Browse files
[widgets/raw_menu_anchor.dart] Always call onClose and onCloseRequested on descendants before parent. (flutter#182357)
This PR corrects the order in which onClose() and onCloseRequested() is called by a RawMenuAnchor tree. Per @dkwingsmt: > A close is immediate and should close children immediately before closing itself, while a closeRequest [can be] async and should closeRequest children. Before, calling close() on a parent would call handleCloseRequest() on its children, leading to children (potentially) finishing their closure after their parents. Additionally, if a RawMenuAnchor is already closed, it will not trigger onCloseRequested. **Details:** The handleCloseRequest portion of the _RawMenuAnchorBaseMixin.closeChildren function has been moved into a requestChildrenClose function to be more explicit. Additionally, this PR makes handleCloseRequest call requestChildrenClose() after calling widget.onCloseRequested on _RawMenuAnchorState. This makes submenus begin closing after their parent menu begins closing. I also modified the menu_anchor.dart and raw_menu_anchor.3.dart code to remove a closeChildren call (now redundant). Last, this PR makes handleCloseRequest short-circuit if a menu isn't open. This prevents unncessary widget.onCloseRequest calls when a submenu is opened, since all sibling menus call handleCloseRequest before the submenu begins opening. This is a breaking change. I had to modify one of the DismissMenuAnchor tests since onCloseRequested is now called on all descendants when a dismissal occurs. I think it's best to introduce this change because it can lead to hard-to-track behavior when creating menus (that's how I discovered it). Migration guide: flutter/website#13145 Additional tests were added to check the opening and closing behavior. Fixes flutter#182355 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. 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](https://developers.google.com/gemini-code-assist/docs/review-github-code). 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. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Tong Mu <dkwingsmt@users.noreply.github.com>
1 parent d79a56b commit d18ad52

4 files changed

Lines changed: 340 additions & 66 deletions

File tree

examples/api/lib/widgets/raw_menu_anchor/raw_menu_anchor.3.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ class MenuState extends State<Menu> with SingleTickerProviderStateMixin {
181181
return;
182182
}
183183

184-
// Animate the menu's children out of view.
185-
menuController.closeChildren();
186-
187184
// Animate the menu out of view.
188185
animationController.reverse().whenComplete(hideOverlay);
189186
}

packages/flutter/lib/src/material/menu_anchor.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,6 @@ class _MenuAnchorState extends State<MenuAnchor> with SingleTickerProviderStateM
656656
return;
657657
}
658658

659-
_menuController.closeChildren();
660659
_animationController.reverse().whenComplete(hideOverlay);
661660
}
662661

packages/flutter/lib/src/widgets/raw_menu_anchor.dart

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,23 @@ class RawMenuAnchor extends StatefulWidget {
310310

311311
/// Called when a request is made to close the menu.
312312
///
313-
/// This callback is triggered every time [MenuController.close] is called,
314-
/// regardless of whether the overlay is already hidden. As a result, this
315-
/// callback can be used to add a delay or a closing animation before the menu
316-
/// is hidden.
313+
/// This callback can be used to add a delay or a closing animation before the
314+
/// menu is hidden.
315+
///
316+
/// This callback is triggered every time [MenuController.close] is called
317+
/// while this menu is open.
318+
///
319+
/// This callback is also triggered when a sibling [RawMenuAnchor] is opened
320+
/// while this menu is open. In this case, the callback can be used to add a
321+
/// delay or a closing animation while the sibling menu opens. When
322+
/// implementing this behavior, consider disabling interactions so that the
323+
/// closing menu does not interfere with the opening sibling menu. Also
324+
/// consider disabling semantics, focus, and hit testing for the closing menu
325+
/// for the duration of the closing animation.
326+
///
327+
/// Pending timers or animations started in a previous call to
328+
/// [onCloseRequested] should be canceled when this callback is triggered to
329+
/// prevent them from closing the menu at an unintended time.
317330
///
318331
/// If the menu is not closed, this callback will also be called when the root
319332
/// menu anchor is scrolled and when the screen is resized.
@@ -600,22 +613,40 @@ mixin _RawMenuAnchorBaseMixin<T extends StatefulWidget> on State<T> {
600613
@protected
601614
void handleCloseRequest();
602615

603-
/// Request that the submenus of this menu be closed.
616+
/// Close the open submenus of this menu.
604617
///
605-
/// By default, this method will call [handleCloseRequest] on each child of this
606-
/// menu, which will trigger the closing sequence of each child.
618+
/// This method will call [close] on each child of this menu, which will
619+
/// immediately close the child.
607620
///
608621
/// If `inDispose` is true, this method was triggered by the widget being
609622
/// unmounted.
623+
///
624+
/// See also:
625+
/// * [requestChildrenClose], which triggers the closing sequence of each
626+
/// child by calling [handleCloseRequest] on each child.
610627
@protected
611628
void closeChildren({bool inDispose = false}) {
612629
assert(_debugMenuInfo('Closing children of $this${inDispose ? ' (dispose)' : ''}'));
613-
for (final child in List<_RawMenuAnchorBaseMixin>.of(_anchorChildren)) {
614-
if (inDispose) {
615-
child.close(inDispose: inDispose);
616-
} else {
617-
child.handleCloseRequest();
618-
}
630+
final children = List<_RawMenuAnchorBaseMixin>.of(_anchorChildren);
631+
for (final child in children) {
632+
child.close(inDispose: inDispose);
633+
}
634+
}
635+
636+
/// Request that the open submenus of this menu be closed.
637+
///
638+
/// This method will call [handleCloseRequest] on each child of this menu,
639+
/// which will trigger the closing sequence of each child.
640+
///
641+
/// See also:
642+
/// * [closeChildren], which immediately closes each child without triggering
643+
/// the closing sequence.
644+
@protected
645+
void requestChildrenClose() {
646+
assert(_debugMenuInfo('Calling handleCloseRequest for children of $this'));
647+
final children = List<_RawMenuAnchorBaseMixin>.of(_anchorChildren);
648+
for (final child in children) {
649+
child.handleCloseRequest();
619650
}
620651
}
621652

@@ -626,7 +657,7 @@ mixin _RawMenuAnchorBaseMixin<T extends StatefulWidget> on State<T> {
626657
void handleOutsideTap(PointerDownEvent pointerDownEvent) {
627658
assert(_debugMenuInfo('Tapped Outside $menuController'));
628659
if (isOpen) {
629-
closeChildren();
660+
requestChildrenClose();
630661
}
631662
}
632663

@@ -708,7 +739,7 @@ class _RawMenuAnchorState extends State<RawMenuAnchor> with _RawMenuAnchorBaseMi
708739
assert(_debugMenuInfo('Opening $this at ${position ?? Offset.zero}'));
709740

710741
// Close all siblings.
711-
_parent?.closeChildren();
742+
_parent?.requestChildrenClose();
712743
assert(!_overlayController.isShowing);
713744
_menuPosition = position;
714745
_parent?._childChangedOpenState();
@@ -731,7 +762,9 @@ class _RawMenuAnchorState extends State<RawMenuAnchor> with _RawMenuAnchorBaseMi
731762
return;
732763
}
733764

765+
// Parents will close after children do.
734766
closeChildren(inDispose: inDispose);
767+
735768
// Don't hide if we're in the middle of a build.
736769
if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.persistentCallbacks) {
737770
_overlayController.hide();
@@ -764,6 +797,10 @@ class _RawMenuAnchorState extends State<RawMenuAnchor> with _RawMenuAnchorBaseMi
764797

765798
@override
766799
void handleCloseRequest() {
800+
if (!isOpen) {
801+
return;
802+
}
803+
767804
// Changes in MediaQuery.sizeOf(context) cause RawMenuAnchor to close during
768805
// didChangeDependencies. When this happens, calling setState during the
769806
// closing sequence (handleCloseRequest -> onCloseRequested -> hideOverlay)
@@ -779,6 +816,9 @@ class _RawMenuAnchorState extends State<RawMenuAnchor> with _RawMenuAnchorBaseMi
779816
}
780817
}, debugLabel: 'RawMenuAnchor.handleCloseRequest');
781818
}
819+
820+
// Parents will request close before children do.
821+
requestChildrenClose();
782822
}
783823

784824
Widget _buildOverlay(BuildContext context, OverlayChildLayoutInfo layoutInfo) {
@@ -939,7 +979,7 @@ class _RawMenuAnchorGroupState extends State<RawMenuAnchorGroup>
939979
@override
940980
void handleCloseRequest() {
941981
assert(_debugMenuInfo('Requesting close $this'));
942-
close();
982+
requestChildrenClose();
943983
}
944984

945985
@override
@@ -1018,7 +1058,7 @@ class MenuController {
10181058
/// without closing the menu itself.
10191059
void closeChildren() {
10201060
assert(_anchor != null);
1021-
_anchor!.closeChildren();
1061+
_anchor!.requestChildrenClose();
10221062
}
10231063

10241064
// ignore: use_setters_to_change_properties

0 commit comments

Comments
 (0)