【发布时间】:2020-06-18 07:35:00
【问题描述】:
我希望在我的整个应用程序中拥有持久的底部导航栏,但在登录页面等某些路线中排除底部导航栏。
我创建了 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() 中我写: 底部导航栏:MyBottomNavigationBar(底部索引:0,) 要么 bottomNavigationBar: MyBottomNavigationBar(bottomIndex: 1,), 要么 bottomNavigationBar: MyBottomNavigationBar(bottomIndex: 2,),
一切都很好,但我有一个问题:每次我用bottomNavigationBar打开任何页面时,我的主页(HomePage())都会重建并从api调用方法。我怎样才能避免它?谢谢
【问题讨论】:
-
我们没有足够的信息来帮助您。请查看问题指南:stackoverflow.com/help/how-to-ask