Skip to content

Close CupertinoContextMenu overlay if the widget is disposed or a new route is pushed #170186

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

victorsanni
Copy link
Contributor

@victorsanni victorsanni commented Jun 6, 2025

Fixes CupertinoContextMenu potential unremoved overlay entry
Fixes CupertinoContextMenu onTap gesture interferes with child widget with onTap GestureRecognizer

Sample code
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

bool ctxMenuRemoved = false;

class ContextMenuApp extends StatelessWidget {
  const ContextMenuApp({super.key});

  @override
  Widget build(BuildContext context) {
    final colorScheme = ColorScheme.fromSeed(seedColor: Colors.orange);
    return MaterialApp(
      theme: ThemeData(
        colorScheme: colorScheme,
        appBarTheme:
            AppBarTheme(backgroundColor: colorScheme.secondaryContainer),
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text('Home'),
        ),
        body: Center(
          child: CupertinoContextMenu(
            actions: [
              CupertinoContextMenuAction(
                child: Text('Test'),
              ),
            ],
            child: GestureDetector(
              onTap: () {
                Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => _OtherPage()),
                );
              },
              child: Container(
                color: Colors.orange,
                height: 100,
                width: 100,
              ),
            ),
          ),
        ),
      );
}

class _OtherPage extends StatelessWidget {
  const _OtherPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Align(
        child: Builder(builder: (context) {
          return Listener(
            onPointerDown: (_) {
              Timer(const Duration(milliseconds: 480), () {
                ctxMenuRemoved = true;
                (context as Element).markNeedsBuild();
              });
            },
            child: ctxMenuRemoved
                ? const SizedBox()
                : CupertinoContextMenu(
                    actions: [
                      CupertinoContextMenuAction(
                        child: const Text('Action one'),
                        onPressed: () {},
                      ),
                    ],
                    child: Container(
                      height: 100,
                      width: 100,
                      color: Colors.black45,
                    ),
                  ),
          );
        }),
      ),
    );
  }
}

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@victorsanni victorsanni marked this pull request as draft June 6, 2025 23:13
@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: cupertino flutter/packages/flutter/cupertino repository labels Jun 6, 2025
@victorsanni victorsanni marked this pull request as ready for review June 11, 2025 01:34
@victorsanni victorsanni changed the title Close context menu if route is pushed mid-animation Close CupertinoContextMenu overlay if the widget is disposed or a new route is pushed Jun 11, 2025
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM 👍


// Kickstart the route transition.
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this duration dependent on the duration of the route transition? FYI I am working on figuring out a way to dynamically get this value in tests, see #165832 (comment). Probably no action to take on this PR, but just a heads up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, but 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: cupertino flutter/packages/flutter/cupertino repository framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CupertinoContextMenu onTap gesture interferes with child widget with onTap GestureRecognizer [CupertinoContextMenu] Potential unremoved overlay entry
2 participants