Это мой класс маршрутизатора
Код: Выделить всё
class AppNavigation {
AppNavigation._();
static final _rootNavigatorKey = GlobalKey();
static final GlobalKey _homeTabNavigatorKey =
GlobalKey();
static final GlobalKey _settingsTabNavigatorKey =
GlobalKey();
static final GlobalKey _profileTabNavigatorKey =
GlobalKey();
static final router = GoRouter(
navigatorKey: _rootNavigatorKey,
initialLocation: "/home",
debugLogDiagnostics: true,
routes: [
GoRoute(
path: SplashScreen.routePath,
name: SplashScreen.routeName,
pageBuilder: (context, state) => getPage(
child: const SplashScreen(),
state: state,
),
),
StatefulShellRoute.indexedStack(
parentNavigatorKey: _rootNavigatorKey,
pageBuilder: (
BuildContext context,
GoRouterState state,
StatefulNavigationShell navigationShell,
) {
return getPage(
child: BottomNavigationScreen(
child: navigationShell,
),
state: state,
);
},
branches: [
StatefulShellBranch(
navigatorKey: _homeTabNavigatorKey,
routes: [
GoRoute(
path: "/home",
pageBuilder: (context, GoRouterState state) {
return getPage(
child: const HomeScreen(),
state: state,
);
},
),
],
),
StatefulShellBranch(
navigatorKey: _settingsTabNavigatorKey,
routes: [
GoRoute(
path: "/settings",
pageBuilder: (context, GoRouterState state) {
return getPage(
child: const SettingsScreen(),
state: state,
);
},
),
],
),
StatefulShellBranch(
navigatorKey: _profileTabNavigatorKey,
routes: [
GoRoute(
path: "/profile",
pageBuilder: (context, GoRouterState state) {
return getPage(
child: const ProfileScreen(),
state: state,
);
},
),
],
)
],
)
],
);
static Page getPage({
required Widget child,
required GoRouterState state,
}) {
return MaterialPage(
key: state.pageKey,
child: child,
);
}
}
Код: Выделить всё
class BottomNavigationScreen extends StatefulWidget {
const BottomNavigationScreen({super.key, required this.child});
final StatefulNavigationShell child;
@override
State createState() => _BottomNavigationScreenState();
}
class _BottomNavigationScreenState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Bottom Navigator Shell'),
),
body: SafeArea(
child: widget.child,
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: widget.child.currentIndex,
onTap: (index) {
widget.child.goBranch(
index,
initialLocation: index == widget.child.currentIndex,
);
setState(() {});
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings',
),
],
),
);
}
}
[!Выход](https:/ /i.sstatic.net/Q38auDnZ.gif)
Чего я не получаю, так это того, что когда я нажимаю кнопку «Назад» на своем устройстве, приложение закрывается. Но чего я хочу добиться, так это когда я нажму кнопку «Назад» на своем устройстве, произойдет возврат на предыдущую вкладку.
Подробнее здесь: https://stackoverflow.com/questions/790 ... navigation