https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/others/custom_stateful_shell_route.dart
In this code, the ButtomBar is always displayed even when details is displayed. However, I would like to hide the ButtomBar when I am viewing ‘a/details’. Is there a way to do this?
One possible way to do this is to use navigationShell.shellRouteContext.routerState.matchedLocation.contains('details')
.
but I think there should be a simpler way. How should this be done?
Specifically, I would like to determine whether to enable or disable buttombar based on the page currently displayed in the following section.
class ScaffoldWithNavBar extends StatelessWidget {
const ScaffoldWithNavBar({
required this.navigationShell,
required this.children,
Key? key,
}) : super(key: key ?? const ValueKey<String>('ScaffoldWithNavBar'));
final StatefulNavigationShell navigationShell;
/// ([AnimatedBranchContainer]).
final List<Widget> children;
@override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedBranchContainer(
currentIndex: navigationShell.currentIndex,
children: children,
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Section A'),
BottomNavigationBarItem(icon: Icon(Icons.work), label: 'Section B'),
],
currentIndex: navigationShell.currentIndex,
onTap: (int index) => _onTap(context, index),
),
);
}
2
Answers
If you assign a rootKey to the ‘a/details’ page and perform navigation to that page using push, I believe your issue will be resolved.
and use
do not forget to add
Scaffold
yourDetailsScreen
‘sbuild
functionTo hide bottom navigation bar on certain screens, you can make the following changes in the example you provided at:
https://github.com/flutter/packages/blob/main/packages/go_router/example/lib/others/custom_stateful_shell_route.dart
First create two GlobalKeys:
In the GoRouter you create, assign _rootNavigatorKey to navigatorKey parameter, like:
and to shellRoutes where you want to hide the bottom navigation bar, like DetailsScreen add _rootNavigatorKey to parentNavigatorKey, like:
Now when you go or push to details screen, the bottom navigation bar will not be shown.
If you have more questions, you can ask me and I’ll be happy to answer them.