-
Notifications
You must be signed in to change notification settings - Fork 29.7k
fix PopupMenuButton unmounted exception when updating position #166412
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
Conversation
| await tester.tap(find.byKey(buttonKey)); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| // TODO(sstasi95): here we should trigger a layout change to test the fix |
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.
You can try pumping an unrelated widget with the tester (I.e. a SizedBox), after the button is tapped, to trigger the unmounting of the popup menu.
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've tried but it's not enough, we should also update the layout after the popup menu is unmounted but when is still visible (during the pop animation), as you can see in this screen recording. This way the _positionBuilder method will be called on an unmounted popup menu.
|
Hey @sstasi95 it looks like this never entered our review queue since this is a draft. Would you like to move forward with this change? |
|
Hi @Piinks, sure. I left it as a draft because I wasn't able to write a test for it, I'll mark it ready for review now |
|
Sorry for the late review and for drafting the PR! I've managed to rewrite your test in the following way, which breaks before your fix and passes after your fix. // Regression test for https://github.com/flutter/flutter/issues/163477
testWidgets("PopupMenu's overlay can be rebuilt even when the button is unmounted", (
WidgetTester tester,
) async {
final GlobalKey buttonKey = GlobalKey();
late StateSetter setState;
bool showButton = true;
Widget widget({required Size viewSize}) {
return Center(
child: SizedBox(
width: viewSize.width,
height: viewSize.height,
child: MaterialApp(
home: Material(
child: StatefulBuilder(builder: (BuildContext context, StateSetter innerSetState) {
setState = innerSetState;
return showButton
? PopupMenuButton<int>(
key: buttonKey,
popUpAnimationStyle: const AnimationStyle(
reverseDuration: Duration(milliseconds: 400),
),
itemBuilder: (BuildContext context) {
return <PopupMenuEntry<int>>[
PopupMenuItem<int>(
value: 1,
child: const Text('ACTION'),
onTap: () {},
),
];
},
)
: Container();
}),
),
),
),
);
}
// Pump a button
await tester.pumpWidget(widget(viewSize: const Size(500, 500)));
// Tap the button to show the menu
await tester.tap(find.byKey(buttonKey));
await tester.pumpAndSettle();
expect(find.text('ACTION'), findsOne);
expect(find.byKey(buttonKey), findsOne);
// Hide the button. The menu still shows since it's placed on a separate route.
setState(() {
showButton = false;
});
await tester.pump();
expect(find.text('ACTION'), findsOne);
expect(find.byKey(buttonKey), findsNothing);
// Resize the view, causing the menu to rebuild. Before the fix, this
// rebuild would lead to a crash, because it relies on context of the button,
// which has been unmounted.
await tester.pumpWidget(widget(viewSize: const Size(300, 300)));
expect(tester.takeException(), isNull);
}); |
dkwingsmt
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.
Everything else looks good to me! Thank you for the investigation.
Co-authored-by: Navaron Bracke <[email protected]>
chunhtai
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
| } | ||
|
|
||
| final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context); | ||
| final RenderBox button = context.findRenderObject()! as RenderBox; |
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.
Since this is already implemented this way, so not blocking the merge. but if the goal is to pin the popup to this button with a delta offset, we should use compositedtrasnformfollower and target/leaderlayer. Things will just be simpler with no mounted/unmounted problem
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.
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.
yes, that is right. although looking at the comment it mentioned some bug about using compsitedfollower, which is new to me.
…er#166412) <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This PR fixes an exception thrown when trying to update an unmounted PopupMenuButton's position, which can happen when a layout change is triggered during PopupMenuButton's pop animation (see issue's attached video). A workaround is to set `popUpAnimationStyle: AnimationStyle.noAnimation`. This PR fixes it by returning the last known position if the button is unmounted. Exception thrown:` FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct). Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)` Code that causes the exception: `final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);` Fixes flutter#163477 Tested both on stable (3.29.2) and master (a0b1b32) ## 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]. - [X] 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]. <!-- 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: Navaron Bracke <[email protected]> Co-authored-by: Tong Mu <[email protected]>
…er#166412) <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This PR fixes an exception thrown when trying to update an unmounted PopupMenuButton's position, which can happen when a layout change is triggered during PopupMenuButton's pop animation (see issue's attached video). A workaround is to set `popUpAnimationStyle: AnimationStyle.noAnimation`. This PR fixes it by returning the last known position if the button is unmounted. Exception thrown:` FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct). Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)` Code that causes the exception: `final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);` Fixes flutter#163477 Tested both on stable (3.29.2) and master (a0b1b32) ## 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]. - [X] 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]. <!-- 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: Navaron Bracke <[email protected]> Co-authored-by: Tong Mu <[email protected]>
This PR fixes an exception thrown when trying to update an unmounted PopupMenuButton's position, which can happen when a layout change is triggered during PopupMenuButton's pop animation (see issue's attached video).
A workaround is to set
popUpAnimationStyle: AnimationStyle.noAnimation.This PR fixes it by returning the last known position if the button is unmounted.
Exception thrown:
FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct). Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)Code that causes the exception:
final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);Fixes #163477
Tested both on stable (3.29.2) and master (a0b1b32)
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.