Skip to content

Using CupertinoContextMenu on a widget with onTap can trigger both simultaneously #52226

Description

@MrAlek

When using CupertinoContextMenu on a widget which also has a GestureDetector with onTap, both the context menu and the other tap action (such as navigating) can be triggered at the same time. See gif:

76107550-2f0acb80-5fd9-11ea-9b90-447cba25d3a7

Code example

CupertinoContextMenu(
  child: GestureDetector(
    onTap: () => Navigator.of(context).pushNamed('/photo'),
    child: Image(image: imageProvider),
  ),
),

Underlying issue

https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/cupertino/context_menu.dart#L344-L348

void _onTap() {
    if (_openController.isAnimating && _openController.value < 0.5) {
      _openController.reverse();
    }
  }

If the controller already is opened more than 50%, it doesn't close. Since there's no way of detecting this from the other gesture detector, both are triggered.

Proposed solution

Always reverse the context menu onTap.

Activity

  1. added
    p: cupertino_uicupertino_ui package in flutter/packages
    f: gesturesflutter/packages/flutter/gestures repository.
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: proposalA detailed proposal for a change to Flutter
    on Mar 9, 2020
  2. TahaTesser commented on Mar 26, 2020

    @TahaTesser
    Contributor

    Code Sample

    import 'package:flutter/cupertino.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoApp(
          debugShowCheckedModeBanner: false,
          title: 'Cupertino App',
          home: Home(),
        );
      }
    }
    
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text('Cupertino App Bar'),
          ),
          child: Center(
            child: CupertinoContextMenu(
              actions: <Widget>[
                CupertinoContextMenuAction(
                  child: const Text('Action one'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                CupertinoContextMenuAction(
                  child: const Text('Action two'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              ],
              child: GestureDetector(
                onTap: () => print('onTap'),
                child:
                    Image(image: NetworkImage('https://i.imgur.com/QCNbOAo.png')),
              ),
            ),
          ),
        );
      }
    }
    
  3. TahaTesser commented on Jul 31, 2020

    @TahaTesser
    Contributor

    Hi @MrAlek
    Not able to reproduce this issue

    Code Sample
    import 'package:flutter/cupertino.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoApp(
          debugShowCheckedModeBanner: false,
          title: 'Cupertino App',
          home: Home(),
        );
      }
    }
    
    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text('Cupertino App Bar'),
          ),
          child: Center(
            child: CupertinoContextMenu(
              actions: <Widget>[
                CupertinoContextMenuAction(
                  child: const Text('Action one'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
                CupertinoContextMenuAction(
                  child: const Text('Action two'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              ],
              child: GestureDetector(
                onTap: () => print('onTap'),
                child:
                    Image(image: NetworkImage('https://i.imgur.com/QCNbOAo.png')),
              ),
            ),
          ),
        );
      }
    }
    
    
    flutter doctor -v
    [✓] Flutter (Channel dev, 1.21.0-1.0.pre, on Mac OS X 10.15.6 19G73, locale
        en-GB)
        • Flutter version 1.21.0-1.0.pre at /Users/tahatesser/Code/flutter_dev
        • Framework revision f25bd9c55c (2 weeks ago), 2020-07-14 20:26:01 -0400
        • Engine revision 99c2b3a245
        • Dart version 2.9.0 (build 2.9.0-21.0.dev 20bf2fcf56)
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
        • Android SDK at /Users/tahatesser/Code/sdk
        • Platform android-30, build-tools 30.0.1
        • ANDROID_HOME = /Users/tahatesser/Code/sdk
        • Java binary at: /Applications/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.6)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.6, Build version 11E708
        • CocoaPods version 1.9.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 47.1.2
        • Dart plugin version 193.7361
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
    
    [✓] VS Code (version 1.47.3)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.13.0
    
    [✓] Connected device (4 available)
        • iPhone 11 (mobile) • 7863E29F-EEE8-499E-BB28-6B111B76E3EE • ios
          • com.apple.CoreSimulator.SimRuntime.iOS-13-6 (simulator)
        • macOS (desktop)    • macos                                • darwin-x64
          • Mac OS X 10.15.6 19G73
        • Web Server (web)   • web-server                           • web-javascript
          • Flutter Tools
        • Chrome (web)       • chrome                               • web-javascript
          • Google Chrome 84.0.4147.105
    
    • No issues found!
    
    
  4. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    and removed
    c: proposalA detailed proposal for a change to Flutter
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Jul 31, 2020
  5. markusaksli-nc commented on Aug 28, 2020

    @markusaksli-nc
    Contributor

    Without additional information, we are unfortunately not sure how to resolve this issue.
    We are therefore reluctantly going to close this bug for now.
    Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away!
    Thanks for your contribution.

  6. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 28, 2020
  7. github-actions commented on Aug 13, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  8. locked as resolved and limited conversation to collaborators on Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: gesturesflutter/packages/flutter/gestures repository.frameworkflutter/packages/flutter repository. See also f: labels.p: cupertino_uicupertino_ui package in flutter/packages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions