我想知道我如何能保持历史导航在孩子的标签从导航栏。
目前,我遵循这篇文章来创建一个NavBar导航。
当我在选项卡中导航时,我想保留历史记录(Home => FeedPeople => FeedPeople => FeedPeople...):
- 主页(一般饲料)(从链接访问FeedPeople)
- FeedPeople => FeedPeople => FeedPeople...
- 发现
- 产品展示
- 我的
问题:当我点击主页上的链接来推送FeedPeople并看到配置文件/:uuid,然后再次是FeedPeople,再次是FeedPeople,等等...当我点击导航栏上的标签,比如购物,然后再次点击主页,我看不到最后一个FeedPeople(最后一个配置文件),而是直接看到主页屏幕。
main.dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:my_app/feed.dart';
import 'package:my_app/feed_people.dart';
import 'package:my_app/route_names.dart';
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final _shellNavigatorKey = GlobalKey<NavigatorState>();
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Flutter Demo',
routerConfig: router,
);
}
final router = GoRouter(
initialLocation: '/',
navigatorKey: _rootNavigatorKey,
routes: [
ShellRoute(
navigatorKey: _shellNavigatorKey,
pageBuilder: (context, state, child) {
print(state.location);
return NoTransitionPage(
child: ScaffoldWithNavBar(
location: state.location,
child: child,
));
},
routes: [
GoRoute(
path: '/',
parentNavigatorKey: _shellNavigatorKey,
pageBuilder: (context, state) {
return const NoTransitionPage(
child: Feed(uuid: 'a78987-hiIh!ç767897'),
);
},
routes: [
GoRoute(
name: RoutesNames.feedPeople,
path: 'profil/:uuid',
builder: (context, state) =>
FeedPeople(uuid: state.pathParameters['uuid']!))
]),
GoRoute(
path: '/discover',
parentNavigatorKey: _shellNavigatorKey,
pageBuilder: (context, state) {
return const NoTransitionPage(
child: Scaffold(
body: Center(child: Text("Discover")),
),
);
},
),
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
path: '/shop',
pageBuilder: (context, state) {
return const NoTransitionPage(
child: Scaffold(
body: Center(child: Text("Shop")),
),
);
}),
],
),
GoRoute(
parentNavigatorKey: _rootNavigatorKey,
path: '/login',
pageBuilder: (context, state) {
return NoTransitionPage(
key: UniqueKey(),
child: Scaffold(
appBar: AppBar(),
body: const Center(
child: Text("Login"),
),
),
);
},
),
],
);
}
// ignore: must_be_immutable
class ScaffoldWithNavBar extends StatefulWidget {
String location;
ScaffoldWithNavBar({super.key, required this.child, required this.location});
final Widget child;
@override
State<ScaffoldWithNavBar> createState() => _ScaffoldWithNavBarState();
}
class _ScaffoldWithNavBarState extends State<ScaffoldWithNavBar> {
int _currentIndex = 0;
static const List<MyCustomBottomNavBarItem> tabs = [
MyCustomBottomNavBarItem(
icon: Icon(Icons.home),
activeIcon: Icon(Icons.home),
label: 'HOME',
initialLocation: '/',
),
MyCustomBottomNavBarItem(
icon: Icon(Icons.explore_outlined),
activeIcon: Icon(Icons.explore),
label: 'DISCOVER',
initialLocation: '/discover',
),
MyCustomBottomNavBarItem(
icon: Icon(Icons.storefront_outlined),
activeIcon: Icon(Icons.storefront),
label: 'SHOP',
initialLocation: '/shop',
),
MyCustomBottomNavBarItem(
icon: Icon(Icons.account_circle_outlined),
activeIcon: Icon(Icons.account_circle),
label: 'MY',
initialLocation: '/login',
),
];
@override
Widget build(BuildContext context) {
const labelStyle = TextStyle(fontFamily: 'Roboto');
return Scaffold(
body: SafeArea(child: widget.child),
bottomNavigationBar: BottomNavigationBar(
selectedLabelStyle: labelStyle,
unselectedLabelStyle: labelStyle,
selectedItemColor: const Color(0xFF434343),
selectedFontSize: 12,
unselectedItemColor: const Color(0xFF838383),
showUnselectedLabels: true,
type: BottomNavigationBarType.fixed,
onTap: (int index) {
_goOtherTab(context, index);
},
currentIndex: widget.location == '/'
? 0
: widget.location == '/discover'
? 1
: widget.location == '/shop'
? 2
: 3,
items: tabs,
),
);
}
void _goOtherTab(BuildContext context, int index) {
if (index == _currentIndex) return;
GoRouter router = GoRouter.of(context);
String location = tabs[index].initialLocation;
setState(() {
_currentIndex = index;
});
if (index == 3) {
context.push('/login');
} else {
router.go(location);
}
}
}
class MyCustomBottomNavBarItem extends BottomNavigationBarItem {
final String initialLocation;
const MyCustomBottomNavBarItem(
{required this.initialLocation,
required Widget icon,
String? label,
Widget? activeIcon})
: super(icon: icon, label: label, activeIcon: activeIcon ?? icon);
}
feed.dart(从导航栏点击主页)
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:my_app/route_names.dart';
class Feed extends StatelessWidget {
final String uuid;
const Feed({super.key, required this.uuid});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Welcome $uuid"),
),
body: Column(
children: <Widget>[
ElevatedButton(
child: const Text("Retour"),
onPressed: () {
context.pop(context);
}),
ElevatedButton(
child: const Text("go to feedPeople"),
onPressed: () {
String uuid = "za987YIU-89798Yu-95fGBU";
context.pushNamed(RoutesNames.feedPeople,
pathParameters: {"uuid": uuid});
})
],
));
}
}
feedPeople
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:my_app/route_names.dart';
class FeedPeople extends StatelessWidget {
final String uuid;
const FeedPeople({super.key, required this.uuid});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Welcome $uuid"),
),
body: Column(
children: <Widget>[
ElevatedButton(
child: const Text("Retour"),
onPressed: () {
context.pop(context);
}),
ElevatedButton(
child: const Text("Autre profil"),
onPressed: () {
String uuid = "a987YIU-89798Yu-95fGBU";
context.pushNamed(RoutesNames.feedPeople,
pathParameters: {"uuid": uuid});
})
],
));
}
}
我试着直接在回家路线中设置路线,但同样的问题。历史没有保留,当我从导航栏导航时,我直接看到“第一”屏幕(主页而不是最后一个FeedPeople)。
1条答案
按热度按时间shyt4zoc1#
使用go_router,使用StatefulShellRoute获取选项卡的持久状态。下面是dartpad go_router示例的修改版本,但使用StatefulShellRoute和NavigationBar(而不是BottomNavigationBar)来获得所需的输出。
你可以在dartpart here中使用下面的代码