Victor Sanni be8cdeb84f
Close CupertinoContextMenu overlay if the widget is disposed or a new route is pushed (#170186)
Fixes [CupertinoContextMenu potential unremoved overlay
entry](https://github.com/flutter/flutter/issues/131471)
Fixes [CupertinoContextMenu onTap gesture interferes with child widget
with onTap
GestureRecognizer](https://github.com/flutter/flutter/issues/169911)

<details>
<summary>Sample code</summary>

```dart

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


```

</details>
2025-06-20 22:20:11 +00:00
..
2025-06-03 08:48:14 +00:00
2025-06-03 08:48:14 +00:00
2025-06-03 08:48:14 +00:00
2025-06-03 08:48:14 +00:00