Skip to content

Conversation

@sstasi95
Copy link
Contributor

@sstasi95 sstasi95 commented Apr 2, 2025

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.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Apr 2, 2025
await tester.tap(find.byKey(buttonKey));
await tester.pumpAndSettle();

// TODO(sstasi95): here we should trigger a layout change to test the fix
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@Piinks
Copy link
Contributor

Piinks commented Jun 4, 2025

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?

@sstasi95
Copy link
Contributor Author

sstasi95 commented Jun 5, 2025

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

@sstasi95 sstasi95 marked this pull request as ready for review June 5, 2025 09:45
@dkwingsmt dkwingsmt self-requested a review June 11, 2025 18:24
@dkwingsmt
Copy link
Contributor

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);
  });

Copy link
Contributor

@dkwingsmt dkwingsmt left a 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.

Copy link
Contributor

@chunhtai chunhtai left a 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;
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@chunhtai Are you talking about #169814 ?

Copy link
Contributor

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.

@dkwingsmt dkwingsmt added the autosubmit Merge PR when tree becomes green via auto submit App label Jun 26, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Jun 26, 2025
Merged via the queue into flutter:master with commit 777bfea Jun 26, 2025
71 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jun 26, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jun 27, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Jul 7, 2025
mboetger pushed a commit to mboetger/flutter that referenced this pull request Jul 21, 2025
…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]>
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 16, 2025
lucaantonelli pushed a commit to lucaantonelli/flutter that referenced this pull request Nov 21, 2025
…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]>
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.

PopupMenuRoute crashes sometimes on drawFrame

5 participants