我想在整个应用程序中保留底部导航栏,但在某些路径(例如登录页面)中排除底部导航栏。
我创建了BottomNavigationBar小部件:
class MyBottomNavigationBar extends StatefulWidget {
final int bottomIndex;
const MyBottomNavigationBar({Key key, this.bottomIndex}) :
super(key: key);
State createState() => _MyBottomNavigationBarState();
}
class _MyBottomNavigationBarState extends State
<MyBottomNavigationBar> {
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(LineIcons.film),
title: Text(
'1',
),
),
BottomNavigationBarItem(
icon: Icon(LineIcons.ticket),
title: Text(
'2',
),
),
BottomNavigationBarItem(
icon: Icon(LineIcons.user),
title: Text(
'3',
),
),
],
currentIndex: widget.bottomIndex,
onTap: (int index) {
setState(() {
switch (index) {
case 0 :
Navigator.push(
context,
MaterialPageRoute(builder: (context) => HomePage()));
break;
case 1:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyTickets()));
break;
case 2:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainProfile()));
break;
}
});
}
);
} }
然后在我要创建BottomNavigationBar的每个页面的build()中编写: bottomNavigationBar:MyBottomNavigationBar(bottomIndex:0,) 要么 bottomNavigationBar:MyBottomNavigationBar(bottomIndex:1,), 要么 bottomNavigationBar:MyBottomNavigationBar(bottomIndex:2,),
一切都很好,但是我有一个问题:每次当我使用bottomNavigationBar打开任何页面时,我的主页(HomePage())都会重新构建并从api调用方法。我该如何避免呢?谢谢
答案 0 :(得分:0)
也许您可以使用索引堆栈来实现此目的。只需检查此链接即可获得所需的输出。