Repository: flutter
Issue: Android 11 gesture navigation when using CupertinoPageTransitionsBuilder, causes double "pop" of Flutter navigation stack
Labels: platform-android,framework,f: cupertino,f: routes,e: OS-version specific,has reproducible steps,P2,found in release: 2.2,team-design,triaged-design

Pixel 5 / Android 11 uses gesture navigation. This means you can "go back" by swiping from the edges of the screen. This gesture is occasionally picked up by Flutter; thus duplicating the "back" intention.

https://youtu.be/1PinCPtCQB0 - the bug in our production app
https://youtu.be/mkDj4Ym9nWU - the bug in the example code below

Possibly related:

* #62744
* #74228

## Steps to Reproduce

The following _should_ display the same behaviour.

Please see the youtube video above.

Using a physical device running Android 11 with navigation gestures, _sometimes_ swiping from the edges **sometimes** causes navigator.pop to trigger twice. The Hero widget will disappear.

My hypothesis is that:

1. The `Hero` widget is attached to `Page3`
2. `pop` is triggered twice in a single event - _the bug_
3. The `Hero` widget is now attached to `Page2`, which is immediately disposed
4. The user ends up on `Page1`, with no `Hero` widget - _a side effect_

_Please note that I've not ran this code on a physical device as I'm not at my usual workstation. Please DM me if you cannot replicate and I will see if I can flesh out my code / testing._

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        pageTransitionsTheme: PageTransitionsTheme(
          builders: {
            TargetPlatform.android: CupertinoPageTransitionsBuilder(),
          },
        ),
      ),
      home: Page1(),
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 1'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Logo(),
          TextButton(
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => Page2(),
                ),
              );
            },
            child: Text('Go To Page 2'),
          ),
        ],
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 2'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Logo(),
          TextButton(
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => Page3(),
                ),
              );
            },
            child: Text('Go To Page 3'),
          ),
        ],
      ),
    );
  }
}

class Page3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page 3'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Logo(),
        ],
      ),
    );
  }
}

class Logo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Hero(
      tag: 'hero',
      child: Card(
        child: Padding(
          padding: EdgeInsets.all(8.0),
          child: Text('Hero Widget'),
        ),
      ),
    );
  }
}

```

**Expected results:** <!-- what did you want to see? -->

I expected swiping from the edges to trigger a single "back" event

**Actual results:** <!-- what did you see? -->

Swiping from the edges causes **two** back events. Sometimes.

<details>
  <summary>Logs</summary>

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
No issues found! (ran in 1.8s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 2.0.4, on Mac OS X 10.15.7 19H15 darwin-x64, locale en-GB)
    • Flutter version 2.0.4 at /Users/david/Development/flutter
    • Framework revision b1395592de (7 weeks ago), 2021-04-01 14:25:01 -0700
    • Engine revision 2dce47073a
    • Dart version 2.12.2

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/david/Library/Android/sdk
    • Platform android-30, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.4, Build version 12D4e
    • CocoaPods version 1.10.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 40.2.2
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.56.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.22.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.212

• No issues found!
```

</details>