Anonymous
Постоянная нижняя навигация Flutter с кнопкой возврата Android
Сообщение
Anonymous » 30 сен 2024, 00:48
Я пытаюсь реализовать нижнюю панель навигации в моем приложении Flutter с помощью Go Router. Прочитав пару статей, я узнал, что для реализации постоянной нижней навигации мне нужно использовать StatefulShellRoute маршрутизатора go. Итак, я реализовал StatefulShellRoute. Но я не получаю ожидаемого результата, используя этот код.
Это мой класс маршрутизатора
Код: Выделить всё
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://stackoverflow.com/questions/790 ... ack-button
1727646526
Anonymous
Я пытаюсь реализовать нижнюю панель навигации в моем приложении Flutter с помощью Go Router. Прочитав пару статей, я узнал, что для реализации постоянной нижней навигации мне нужно использовать StatefulShellRoute маршрутизатора go. Итак, я реализовал StatefulShellRoute. Но я не получаю ожидаемого результата, используя этот код. [b]Это мой класс маршрутизатора[/b] [code]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, ); } } [/code] [b]Это мой нижний экран навигации[/b] [code]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', ), ], ), ); } } [/code] Ожидаемый результат: [img]https://i.sstatic.net/DdZkmGw4.gif[/img] Вывод: [img]https://i.sstatic.net/Q38auDnZ.gif[/img] Чего я не получаю, так это того, что когда я нажимаю кнопку «Назад» на своем устройстве, приложение закрывается. Но чего я хочу добиться, так это того, что когда я буду использовать кнопку «Назад» на своем устройстве, она вернется на предыдущую вкладку. Подробнее здесь: [url]https://stackoverflow.com/questions/79037242/persistent-flutter-bottom-navigation-with-android-back-button[/url]