Skip to content

[GoRouter] ShellRoute state does not take pushed route into account. #114901

@cgestes

Description

@cgestes
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() => runApp(App());

final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    ShellRoute(
        builder: (context, state, child) {
          print("Shell: ${state.location}");
          return child;
        },
        routes: <GoRoute>[
          GoRoute(
            path: '/',
            builder: (BuildContext context, GoRouterState state) {
              return MyHomePage();
            },
          ),
          GoRoute(
            path: '/b',
            builder: (BuildContext context, GoRouterState state) {
              return MyHomePageB();
            },
          ),
        ])
  ],
);

class App extends StatelessWidget {
  App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
      title: 'GoRouter Example',
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final st = GoRouterState.of(context);
    print("locA: ${st.location}");
    return Scaffold(
        appBar: AppBar(
          title: const Text("A"),
        ),
        body: Column(children: [
          TextButton(child: Text("go A"), onPressed: () => _router.go('/')),
          TextButton(child: Text("push A"), onPressed: () => _router.push('/')),
          TextButton(child: Text("go B"), onPressed: () => _router.go('/b')),
          TextButton(
              child: Text("push B"), onPressed: () => _router.push('/b')),
        ]));
  }
}

class MyHomePageB extends StatelessWidget {
  const MyHomePageB({super.key});

  @override
  Widget build(BuildContext context) {
    final st = GoRouterState.of(context);
    print("locB: ${st.location}");
    return Scaffold(
        appBar: AppBar(
          title: const Text("B"),
        ),
        body: Column(children: [
          TextButton(child: Text("go A"), onPressed: () => _router.go('/')),
          TextButton(child: Text("push A"), onPressed: () => _router.push('/')),
          TextButton(child: Text("go B"), onPressed: () => _router.go('/b')),
          TextButton(
              child: Text("push B"), onPressed: () => _router.push('/b')),
        ]));
  }
}

Launch, click on pushA or pushB, observe that the shell state doesn't take the pushed route into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions