Skip to content

Close context menu if route is pushed mid-animation #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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

victorsanni
Copy link
Contributor

Fixes #131471
Fixes #169911

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
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
1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy