Repository: flutter
Issue: [go_router] Closes the root route when MaterialApp.router changes
Labels: package,has reproducible steps,P2,p: go_router,found in release: 3.3,found in release: 3.6,team-go_router,triaged-go_router

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. First problem is changing values in **MaterialApp.router.showPerformanceOverlay** or **MaterialApp.router.showSemanticsDebugger** at runtime.
2. The second problem is changing the **MaterialApp.router.builder** widget at runtime. For example, when a new widget is added to the tree.

**Expected results:** <!-- what did you want to see? -->
The order of the routes does not change

**Actual results:** <!-- what did you see? -->
The current route remains, but the root route is closed. Both problems lead to the same result.

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
  final router = GoRouter(
    routes: <GoRoute>[
      GoRoute(path: '/', builder: (_, __) => const HomeScreen()),
      GoRoute(path: '/settings', builder: (_, __) => const SettingsScreen()),
    ],
  );
  final controller = SettingsController();
  return runApp(App(router, controller: controller));
}

class App extends StatelessWidget {
  const App(
    this.router, {
    Key? key,
    required this.controller,
  }) : super(key: key);

  final GoRouter router;
  final SettingsController controller;

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (context, child) => MaterialApp.router(
        debugShowCheckedModeBanner: false,
        showPerformanceOverlay: controller.performanceOverlay,
        routerDelegate: router.routerDelegate,
        routeInformationParser: router.routeInformationParser,
        routeInformationProvider: router.routeInformationProvider,
        builder: (context, child) {
          if (controller.needUpdateMaterialBuilder) {
            child = Banner(
              message: 'go_router',
              location: BannerLocation.topStart,
              child: child,
            );
          }
          return child!;
        },
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () => context.push('/settings'),
              child: const Text('Go to setting'),
            ),
          ],
        ),
      ),
    );
  }
}

class SettingsScreen extends StatefulWidget {
  const SettingsScreen({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _SettingState();
}

class _SettingState extends State<SettingsScreen> {
  late final SettingsController _controller;

  @override
  void initState() {
    super.initState();
    _controller = SettingsController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Settings')),
      body: AnimatedBuilder(
        animation: _controller,
        builder: (context, child) => Column(
          children: [
            SwitchListTile(
              title: const Text('Show performance overlay'),
              value: _controller.performanceOverlay,
              onChanged: (_) => _controller.updatePerformanceOverlay(),
            ),
            SwitchListTile(
              title: const Text('Update material builder'),
              value: _controller.needUpdateMaterialBuilder,
              onChanged: (_) => _controller.updateMaterialBuilder(),
            ),
          ],
        ),
      ),
    );
  }
}

class SettingsController with ChangeNotifier {
  static final SettingsController _instance = SettingsController._internal();

  factory SettingsController() => _instance;

  SettingsController._internal()
      : _performanceOverlay = false,
        _needUpdateMaterialBuilder = false;

  late bool _performanceOverlay;
  late bool _needUpdateMaterialBuilder;

  bool get performanceOverlay => _performanceOverlay;

  bool get needUpdateMaterialBuilder => _needUpdateMaterialBuilder;

  void updatePerformanceOverlay() {
    _performanceOverlay = !_performanceOverlay;
    notifyListeners();
  }

  void updateMaterialBuilder() {
    _needUpdateMaterialBuilder = !_needUpdateMaterialBuilder;
    notifyListeners();
  }
}
```
</details>

The problem is reproduced on Android & iOS.

```console
[✓] Flutter (Channel stable, 3.3.6, on macOS 13.0 22A380 darwin-x64, locale ru-RU)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.3)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
```

```console
go_router: ^5.1.9
```