我正在尝试将PageStorageBucket
,PageStorage
和PageStorageKey
与BottomNavigationBar的每个选项卡下的单独导航器一起使用。当我切换标签时,它不会持续滚动位置。
如果我从树中删除导航器小部件,而直接使用页面,它将按预期工作。每个选项卡都会记住其滚动位置。
请帮助我使用导航器小部件或PageStore对象的方式有问题吗?
class MyTabPage extends StatefulWidget {
ViewModel viewModel;
RouteProvider routeProvider;
MyTabPage({
final Key key,
final RouteProvider routeProvider,
@required final this.viewModel})
: assert(viewModel != null),
assert(routeProvider != null),
super(key: key);
@override
_MyTabPageState createState() => _MyTabPageState();
}
class _MyTabPageState extends State<MyTabPage> {
List<MyTabNavigator> myPageTabNavigators;
final PageStorageBucket pageStorageBucket = PageStorageBucket();
@override
void initState() {
myPageTabNavigators = _tabItems.map((item) =>
_tabNavigator(item, PageStorageKey("TNTNTN" + item.title)).toList(growable: false);
super.initState();
}
@override
Widget build(final BuildContext context) {
return StreamBuilder(
stream: viewModel.selectedItemStream,
initialData: widget.viewModel.items.first,
builder: (final context, final snapshot) {
final myPageTabItem selectedItem = snapshot.data as myPageTabItem;
return WillPopScope(
child: Scaffold(
body: PageStorage(
bucket: pageStorageBucket,
child: myPageTabNavigators[widget.viewModel.items.indexOf(selectedItem)]
),
bottomNavigationBar: MyPageNavigationBar(
selectedTabItem: selectedItem
),
)
);
});
}
MyTabNavigator _tabNavigator(final MyTabItem myTabItem, Key key) {
return MyTabNavigator(
key: key,
myTabItem: myTabItem,
routeProvider: this.widget.routeProvider
);
}
}
class MyTabNavigator extends StatefulWidget {
final MyTabItem tabItem;
final RouteProvider routeProvider;
MyTabNavigator({
Key key,
@required final this.myTabItem,
@required final this.routeProvider})
: assert(myTabItem != null),
assert(routeProvider != null),
super(key: key);
@override
_MyTabNavigatorState createState() => _MyTabNavigatorState();
}
class _MyTabNavigatorState extends State<MyTabNavigator> {
@override
Widget build(final BuildContext context) {
String initialRoute = widget.myTabItem.initialRoute;
// return widget.routeProvider.applicationRoutes[initialRoute](context); ==> this works, but below line does not work
return Navigator(
key: widget.key,
initialRoute: initialRoute,
onGenerateRoute: _onGenerateRoute
);
}
Route<dynamic> _onGenerateRoute(final RouteSettings routeSettings) {
return MaterialPageRoute(
builder: (context) {
return widget.routeProvider.applicationRoutes[routeSettings.name](context);
},
settings: routeSettings
);
}
}