I am using StatefulShellRoute.indexedStack of go_router.
The issue is when I am trying to go to the route ‘extra’, I can see the route path getting printed correctly, in the order –
PATH: /
PATH: /terms-of-service
PATH: /terms-of-service/extra
But when I try to pop from the “extra” route I go to “terms-of-service” page where I can see the UI of TermsAndService page but in the console the route is printed as “/”.
I have provided the code to reproduce the bug.
Similar issue is reported in –
In Go_Router package, how to hide StatefulShellRoute.indexedStack? I tried using keys but I just can not make it work
Get current shell route inside builder in go_router flutter
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: router,
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Home Page Content'),
const SizedBox(
height: 15,
),
FilledButton(
child: const Text('Term of Service'),
onPressed: () => context.pushNamed('terms-of-service')),
],
),
),
);
}
}
class BottomNavigationBarScaffold extends StatefulWidget {
const BottomNavigationBarScaffold({super.key, required this.navigationShell});
final Widget navigationShell;
@override
State<BottomNavigationBarScaffold> createState() =>
_BottomNavigationBarScaffoldState();
}
class _BottomNavigationBarScaffoldState
extends State<BottomNavigationBarScaffold> {
int currentIndex = 0;
void changeTab(int index) {
switch (index) {
case 0:
context.goNamed('home');
break;
case 1:
context.goNamed('settings');
break;
}
setState(() {
currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
final path = GoRouter.of(context).routeInformationProvider.value.uri.path;
print('PATH: ' + path);
return Scaffold(
body: widget.navigationShell,
bottomNavigationBar: path == '/terms-of-service' ? null :
BottomNavigationBar(
onTap: changeTab,
currentIndex: currentIndex,
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(
icon: Icon(Icons.settings), label: 'Settings'),
],
),
);
}
}
class SettingsPage extends StatelessWidget {
const SettingsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Settings'),
),
body: const Center(
child: Text('Settings Page Content'),
),
);
}
}
class TermsOfServicesPage extends StatelessWidget {
const TermsOfServicesPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Terms Of Services'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Terms Of Services Page Content'),
const SizedBox(
height: 15,
),
FilledButton(
onPressed: () => context.pushNamed('extra'),
child: Text('Goto extra page'),
)
],
),
),
);
}
}
class ExtraPage extends StatelessWidget {
const ExtraPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Extra Content'),
),
body: const Center(
child: Text('Extra Content'),
),
);
}
}
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorHomeKey = GlobalKey<NavigatorState>();
final _shellNavigatorSettingsKey = GlobalKey<NavigatorState>();
final router = GoRouter(
debugLogDiagnostics: true,
navigatorKey: _rootNavigatorKey,
routes: [
StatefulShellRoute.indexedStack(
parentNavigatorKey: _rootNavigatorKey,
builder: (context, state, navigationShell) =>
BottomNavigationBarScaffold(navigationShell: navigationShell),
branches: [
StatefulShellBranch(
navigatorKey: _shellNavigatorHomeKey,
routes: [
GoRoute(
name: 'home',
path: '/',
pageBuilder: (context, state) => MaterialPage(child: HomePage()),
routes: [
GoRoute(
name: 'terms-of-service',
path: 'terms-of-service',
parentNavigatorKey: _shellNavigatorHomeKey,
builder: (context, state) => const TermsOfServicesPage(),
routes: [
GoRoute(
name: 'extra',
path: 'extra',
parentNavigatorKey: _shellNavigatorHomeKey,
builder: (context, state) => const ExtraPage(),
),
],
),
],
),
],
),
StatefulShellBranch(
navigatorKey: _shellNavigatorSettingsKey,
routes: [
GoRoute(
parentNavigatorKey: _shellNavigatorSettingsKey,
name: 'settings',
path: '/settings',
pageBuilder: (context, state) => MaterialPage(child: SettingsPage()),
),
],
),
],
)
],
);
Kaushik Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.