Repository: flutter
Issue: [Popscope, NavigatorPopHandler] Every PopScopes are triggered instead of the visible / active one
Labels: framework,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.22,found in release: 3.24

### Steps to reproduce

1. Run the code on Android (easier to reproduce)
2. Stay on Home page
3. Press device back button (or swipe to pop)
4. Look logs

### Expected results

```console
I/flutter (31191): [LabeledGlobalKey<NavigatorState>#fb408 home] onPop
```

### Actual results

```console
I/flutter (31191): Are you sure to leave the app ?
I/flutter (31191): [LabeledGlobalKey<NavigatorState>#fb408 home] onPop
I/flutter (31191): [LabeledGlobalKey<NavigatorState>#08a9b notification] onPop
I/flutter (31191): [LabeledGlobalKey<NavigatorState>#d0ebc message] onPop
```

Because of that, i'm unable to pop only the nested visible page.
I use an IndexedStack to keep in parallel and alive my pages.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final GlobalKey<NavigatorState> _homeKey = GlobalKey<NavigatorState>(debugLabel: "home");
  final GlobalKey<NavigatorState> _notificationKey = GlobalKey<NavigatorState>(debugLabel: "notification");
  final GlobalKey<NavigatorState> _messageKey = GlobalKey<NavigatorState>(debugLabel: "message");

  int index = 0;

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: (bool didPop) {
        if (didPop) return;
        debugPrint("Are you sure to leave the app ?");
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Home'),
        ),
        body: IndexedStack(
          index: index,
          children: <Widget>[
            Nested(
              color: Colors.orange,
              navigationKey: _homeKey,
            ),
            Nested(
              color: Colors.blue,
              navigationKey: _notificationKey,
            ),
            Nested(
              color: Colors.green,
              navigationKey: _messageKey,
            ),
          ],
        ),
        bottomNavigationBar: NavigationBar(
          selectedIndex: index,
          onDestinationSelected: (int index) {
            setState(() => this.index = index);
          },
          destinations: const <Widget>[
            NavigationDestination(
              selectedIcon: Icon(Icons.home),
              icon: Icon(Icons.home_outlined),
              label: 'Home',
            ),
            NavigationDestination(
              icon: Icon(Icons.notifications_sharp),
              label: 'Notifications',
            ),
            NavigationDestination(
              icon: Icon(Icons.messenger_sharp),
              label: 'Messages',
            ),
          ],
        ),
      ),
    );
  }
}

class Nested extends StatelessWidget {
  const Nested({
    required this.navigationKey,
    required this.color,
    super.key,
  });

  final GlobalKey<NavigatorState> navigationKey;
  final Color color;

  @override
  Widget build(BuildContext context) {
    return NavigatorPopHandler(
      onPop: () {
        print("$navigationKey onPop");
        navigationKey.currentState?.maybePop();
      },
      child: Navigator(
        key: navigationKey,
        initialRoute: '/',
        onGenerateRoute: (RouteSettings settings) {
          return switch (settings.name) {
            '/' => MaterialPageRoute(
                builder: (BuildContext context) {
                  return Container(
                    color: color,
                    child: const FakeFirstPage(),
                  );
                },
              ),
            '/increment' => MaterialPageRoute(
                builder: (BuildContext context) {
                  return Container(
                    color: color.withGreen(color.green + 50),
                    child: const FakeSecondPage(),
                  );
                },
              ),
            _ => null,
          };
        },
      ),
    );
  }
}

class FakeFirstPage extends StatelessWidget {
  const FakeFirstPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed("/increment");
            },
            child: const Text("Go to second page"),
          ),
        ],
      ),
    );
  }
}

class FakeSecondPage extends StatefulWidget {
  const FakeSecondPage({super.key});

  @override
  State<FakeSecondPage> createState() => _FakeSecondPageState();
}

class _FakeSecondPageState extends State<FakeSecondPage> {
  int nb = 0;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          ElevatedButton(
            onPressed: () {
              setState(() => nb++);
            },
            child: const Text("Increment"),
          ),
          Text(nb.toString()),
        ],
      ),
    );
  }
}

```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

https://github.com/user-attachments/assets/56190777-99a7-4fdb-a751-f47564590dbf

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.22.3, on macOS 14.5 23F79 darwin-arm64, locale fr-FR)
    • Flutter version 3.22.3 on channel stable at /Users/earminjon/fvm/versions/3.22.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b0850beeb2 (7 days ago), 2024-07-16 21:43:41 -0700
    • Engine revision 235db911ba
    • Dart version 3.4.4
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/earminjon/Library/Android/Sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/earminjon/Library/Android/Sdk
    • Java binary at: /Users/earminjon/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Users/earminjon/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] Android Studio (version 2023.2)
    • Android Studio at /Users/perso/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.7)
    • IntelliJ at /Users/earminjon/Applications/IntelliJ IDEA Ultimate 2023.3.7.app
    • Flutter plugin version 80.0.1
    • Dart plugin version 233.15325.11

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554         • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 126.0.6478.183
    
[✓] Network resources
    • All expected network resources are available.

• No issues found!

```

</details>