我想在整个应用程序中保留持久的底部导航栏,但在某些路由(例如登录页面)中排除底部导航栏。

我创建了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打开ANY页面时,我的主页(HomePage())都是从api重建并调用方法的。如何避免呢?谢谢

最佳答案

也许您可以使用索引堆栈来实现此目的。只需检查此链接即可获得所需的输出。

Flutter: BottomNavigationBar rebuilds Page on change of tab

关于flutter - 底部导航栏持续存在问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60549273/

10-10 17:24