Repository: flutter
Issue: PopScope is not working for LocalHistoryEntry navigation
Labels: framework,f: routes,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.24,found in release: 3.26

### Steps to reproduce

Run the code sample.
Go to second page.
Click show rectangle and then click go back.

### Expected results

onPopInvokedWithResult callback should be triggered on removing the LocalHistoryEntry.

### Actual results

onPopInvoked callback is not triggering on removing the LocalHistoryEntry. But this works fine with Flutter 3.22

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: <String, WidgetBuilder>{
        '/': (BuildContext context) => const HomePage(),
        '/second_page': (BuildContext context) => const SecondPage(),
      },
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const Text('HomePage'),
            // Press this button to open the SecondPage.
            ElevatedButton(
              child: const Text('Second Page >'),
              onPressed: () {
                Navigator.pushNamed(context, '/second_page');
              },
            ),
          ],
        ),
      ),
    );
  }
}

class SecondPage extends StatefulWidget {
  const SecondPage({super.key});

  @override
  State<SecondPage> createState() => _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  bool _showRectangle = false;

  Future<void> _navigateLocallyToShowRectangle() async {
    // This local history entry essentially represents the display of the red
    // rectangle. When this local history entry is removed, we hide the red
    // rectangle.
    setState(() => _showRectangle = true);
    ModalRoute.of(context)
        ?.addLocalHistoryEntry(LocalHistoryEntry(onRemove: () {
      // Hide the red rectangle.
      setState(() => _showRectangle = false);
    }));
  }

  @override
  Widget build(BuildContext context) {
    final Widget localNavContent = _showRectangle
        ? Container(
            width: 100.0,
            height: 100.0,
            color: Colors.red,
          )
        : ElevatedButton(
            onPressed: _navigateLocallyToShowRectangle,
            child: const Text('Show Rectangle'),
          );

    return PopScope(
      onPopInvoked: (didPop) {
        print('pop');
      },
      // onPopInvokedWithResult: (didPop, result) {
      //   print('pop with result');
      // },
      child: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              localNavContent,
              ElevatedButton(
                child: const Text('< Back'),
                onPressed: () {
                  // Pop a route. If this is pressed while the red rectangle is
                  // visible then it will pop our local history entry, which
                  // will hide the red rectangle. Otherwise, the SecondPage will
                  // navigate back to the HomePage.
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

```

</details>


### Screenshots or Video

-

### Logs

No logs for this issue.

### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.24.2, on Microsoft Windows [Version 10.0.22621.4169], locale en-US)
    • Flutter version 3.24.2 on channel stable at C:\sdk\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4cf269e36d (2 weeks ago), 2024-09-03 14:30:00 -0700
    • Engine revision a6bd3f1de1
    • Dart version 3.5.2
    • DevTools version 2.37.2

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\sdk\android-sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = C:\sdk\android-sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.11.3)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
    • Visual Studio Professional 2022 version 17.11.35303.130
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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.11+0--11852314)

[√] VS Code (version 1.93.1)
    • VS Code at C:\Users\\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.96.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22621.4169]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 128.0.6613.138
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 128.0.2739.79

[√] Network resources
    • All expected network resources are available.

```

</details>