Flutter 永久性底部导航栏存在问题

Flutter 永久性底部导航栏存在问题,flutter,dart,Flutter,Dart,我想在我的整个应用程序中有一个持久的底部导航栏,但在一些路径(如登录页面)中不包括底部导航栏 我创建了BottomNavigationBar小部件: class MyBottomNavigationBar extends StatefulWidget { final int bottomIndex; const MyBottomNavigationBar({Key key, this.bottomIndex}) : super(key: key)

我想在我的整个应用程序中有一个持久的底部导航栏,但在一些路径(如登录页面)中不包括底部导航栏

我创建了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;
        }
      });
    }
    );
类MyBottomNavigationBar扩展StatefulWidget{
最终指数;
常量MyBottomNavigationBar({Key,this.bottomIndex}):
超级(键:键);
State createState()=>\u MyBottomNavigationBarState();
}
类_MyBottomNavigationBarState扩展状态
{
@凌驾
小部件构建(构建上下文){
返回底部导航栏(
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
图标:图标(LineIcons.film),
标题:正文(
'1',
),
),
底部导航气压计(
图标:图标(LineIcons.ticket),
标题:正文(
'2',
),
),
底部导航气压计(
图标:图标(LineIcons.user),
标题:正文(
'3',
),
),
],
currentIndex:widget.bottomIndex,
onTap:(int索引){
设置状态(){
开关(索引){
案例0:
导航器。推(
上下文
MaterialPackageRoute(生成器:(上下文)=>HomePage());
打破
案例1:
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>MyTickets());
打破
案例2:
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>MainProfile());
打破
}
});
}
);
} }

然后,在要创建BottomNavigationBar的每个页面的build()中,我写道: bottomNavigationBar:MyBottomNavigationBar(bottomIndex:0,) 或 bottomNavigationBar:MyBottomNavigationBar(bottomIndex:1,), 或 bottomNavigationBar:MyBottomNavigationBar(bottomIndex:2,)


一切正常,但我有一个问题:每次使用bottomNavigationBar打开任何页面时,我的主页(HomePage())都是从api重建和调用方法。我怎样才能避免呢?谢谢

也许您可以使用索引堆栈实现这一点。只需检查此链接,您可能会在其中获得所需的输出


也许您可以使用索引堆栈实现这一点。只需检查此链接,您可能会在其中获得所需的输出


我们没有足够的信息来帮助您。请检查问题指南:我们没有足够的信息来帮助您。请检查问题指南: