【发布时间】:2020-04-07 03:17:21
【问题描述】:
嗨,我是 Flutter 的新手,现在我尝试使用此代码为所有选项卡声明我的数据,但是当我单击选项卡时,底部导航器被关闭并在我单击手机上的后退按钮时再次显示,是有谁知道错误或问题?
class BottomTab extends StatefulWidget {
final String text;
BottomTab({this.text});
@override
_BottomTabState createState() => _BottomTabState();
}
class _BottomTabState extends State<BottomTab> {
void initState() {
super.initState();
}
_onTap(int index) {
setState(() => _selectedIndex = index);
if (index == 0) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) =>
new HomeScreen(value: widget.value)));
} else if (index == 1) {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) =>
new FirstPage(value: widget.value)));
} else {
Navigator.push(
context,
new MaterialPageRoute(
builder: (BuildContext context) =>
new HomeScreen(value: widget.value)));
}
}
final List<Widget> pages = [
HomeScreen(),
FirstPage(),
];
final PageStorageBucket bucket = PageStorageBucket();
int _index = 0;
Widget _myList(int index) => BottomNavigationBar(
onTap: _onTap,
currentIndex: index,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(icon: Icon(Icons.more), title: Text('More')),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: _myList(_index),
body: PageStorage(
child: pages[_index],
bucket: bucket,
),
);
}
}
【问题讨论】:
标签: flutter navigation