问题描述
我目前正在构建一个 Flutter 应用程序,该应用程序将在从一个屏幕导航到另一个屏幕时保留状态,并在使用 BottomNavigationBar.就像它在 Spotify 移动应用程序中工作一样;如果您在其中一个主屏幕的导航层次结构中向下导航到某个级别,则通过底部导航栏更改屏幕,然后再改回旧屏幕,将保留用户在该层次结构中的位置,包括保留国家.
I'm currently working on building a Flutter app that will preserve states when navigating from one screen, to another, and back again when utilizing BottomNavigationBar. Just like it works in the Spotify mobile application; if you have navigated down to a certain level in the navigation hierarchy on one of the main screens, changing screen via the bottom navigation bar, and later changing back to the old screen, will preserve where the user were in that hierarchy, including preservation of the state.
我的头撞在墙上,尝试了各种不同的东西,但都没有成功.
I have run my head against the wall, trying various different things without success.
我想知道如何防止 pageChooser()
中的页面在用户点击 BottomNavigationBar 项后切换时重新构建自己,而是保留他们已经找到的状态(页面都是有状态的小部件).
I want to know how I can prevent the pages in pageChooser()
, when toggled once the user taps the BottomNavigationBar item, from rebuilding themselves, and instead preserve the state they already found themselves in (the pages are all stateful Widgets).
import 'package:flutter/material.dart';
import './page_plan.dart';
import './page_profile.dart';
import './page_startup_namer.dart';
void main() => runApp(new Recipher());
class Recipher extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Pages();
}
}
class Pages extends StatefulWidget {
@override
createState() => new PagesState();
}
class PagesState extends State<Pages> {
int pageIndex = 0;
pageChooser() {
switch (this.pageIndex) {
case 0:
return new ProfilePage();
break;
case 1:
return new PlanPage();
break;
case 2:
return new StartUpNamerPage();
break;
default:
return new Container(
child: new Center(
child: new Text(
'No page found by page chooser.',
style: new TextStyle(fontSize: 30.0)
)
),
);
}
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: pageChooser(),
bottomNavigationBar: new BottomNavigationBar(
currentIndex: pageIndex,
onTap: (int tappedIndex) { //Toggle pageChooser and rebuild state with the index that was tapped in bottom navbar
setState(
(){ this.pageIndex = tappedIndex; }
);
},
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
title: new Text('Profile'),
icon: new Icon(Icons.account_box)
),
new BottomNavigationBarItem(
title: new Text('Plan'),
icon: new Icon(Icons.calendar_today)
),
new BottomNavigationBarItem(
title: new Text('Startup'),
icon: new Icon(Icons.alarm_on)
)
],
)
)
);
}
}
推荐答案
为了在 BottomNavigationBar
中保持状态,可以使用 IndexedStack
For keeping state in BottomNavigationBar
, you can use IndexedStack
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
onTap: (index) {
setState(() {
current_tab = index;
});
},
currentIndex: current_tab,
items: [
BottomNavigationBarItem(
...
),
BottomNavigationBarItem(
...
),
],
),
body: IndexedStack(
children: <Widget>[
PageOne(),
PageTwo(),
],
index: current_tab,
),
);
}
这篇关于使用BottomNavigationBar导航时,如何在颤动中保留小部件状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!