Repository: flutter
Issue: [go_router]: pushReplacement behavior inconsistency with GoRouter and native Navigator
Labels: package,has reproducible steps,P3,p: go_router,team-go_router,triaged-go_router,found in release: 3.24,found in release: 3.25

## Description

We've encountered an inconsistency in the behavior of `pushReplacement` when using GoRouter compared to the native Navigator. This issue affects the interaction with bottom sheets and navigation.

## Problem

1. When using `pushReplacement` with GoRouter:
   - The bottom sheet doesn't close when navigating to a new page.
   - When popping back from the new page, an error is thrown: "There is nothing to pop"

2. When using `pushReplacement` with the native Navigator:
   - The bottom sheet closes correctly when navigating to a new page.
   - Navigation works as expected without errors.

## Environment

- Flutter: 3.24.1
- go_router: 14.2.7
- Platform: macOS 14.6.1 23G93 darwin-arm64

## Steps to Reproduce

1. Use example with native Navigator: click "push b", then click "pop to home"
2. Use example with GoRouter: repeat steps, you should get the error.

## Expected Behavior

- The bottom sheet should close when navigating to Page B.
- Popping back from Page B should return to the Home page without errors.

## Actual Behavior

With GoRouter:
- The whole route is replaced instead of closing sheet when navigating to Page B.
- Attempting to pop back from Page B throws an error: "There is nothing to pop"

With native Navigator:
- The bottom sheet closes when navigating to Page B as expected.
- Popping back from Page B works as expected.

## Code Examples

See the attached code snippets for implementations using both GoRouter and native Navigator.

<details>
<summary>Example with native Navigator</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (context) => const Home(),
        '/B': (context) => const PageB(),
      },
    );
  }
}

class Home extends StatelessWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () {
                showModalBottomSheet(
                  context: context,
                  builder: (BuildContext context) {
                    return const BottomSheetContent();
                  },
                );
              },
              child: const Text('Show Bottom Sheet'),
            ),
          ],
        ),
      ),
    );
  }
}

class BottomSheetContent extends StatelessWidget {
  const BottomSheetContent({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text('This is the bottom sheet content'),
          const SizedBox(height: 20),
          TextButton(
            onPressed: () {
              Navigator.of(context).pushReplacementNamed('/B');
            },
            child: const Text('Push to Page B'),
          ),
        ],
      ),
    );
  }
}

class PageB extends StatelessWidget {
  const PageB({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Page B')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('Pop back to home'),
            ),
          ],
        ),
      ),
    );
  }
}

```

</details>

<details>
<summary>Example with GoRouter</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

final router = GoRouter(
  routes: [
    GoRoute(
      name: 'Home',
      path: '/',
      builder: (_, __) => const Home(),
    ),
    GoRoute(
      name: 'B',
      path: '/B',
      builder: (_, __) => const PageB(),
    ),
  ],
);

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: router,
    );
  }
}

class Home extends StatelessWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () {
                showModalBottomSheet(
                  context: context,
                  builder: (BuildContext context) {
                    return const BottomSheetContent();
                  },
                );
              },
              child: const Text('Show Bottom Sheet'),
            ),
          ],
        ),
      ),
    );
  }
}

class BottomSheetContent extends StatelessWidget {
  const BottomSheetContent({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text('This is the bottom sheet content'),
          const SizedBox(height: 20),
          TextButton(
            onPressed: () {
              router.pushReplacementNamed('B');
            },
            child: const Text('Push to Page B'),
          ),
        ],
      ),
    );
  }
}

class PageB extends StatelessWidget {
  const PageB({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Page B')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextButton(
              onPressed: () {
                router.pop();
              },
              child: const Text('Pop back to home'),
            ),
          ],
        ),
      ),
    );
  }
}
```

</details>

## Additional Notes

This inconsistency creates challenges in maintaining a consistent navigation experience across our app, especially when migrating from native Navigator to GoRouter.

## Possible Solutions

1. Investigate if there's a way to make GoRouter's `pushReplacement` behavior consistent with the native Navigator.
2. Consider adding a method to GoRouter that combines closing the bottom sheet and navigating to a new page.
3. Explore if this is an intended behavior difference and if so, update the documentation to clarify this.

### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.1, on macOS 14.6.1 23G93 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.92.2)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!
```
</details>