【问题标题】:Problem with persistent bottom navigation bar持续底部导航栏的问题
【发布时间】: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调用方法。我怎样才能避免它?谢谢

【问题讨论】:

标签: flutter dart


【解决方案1】:

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

Flutter: BottomNavigationBar rebuilds Page on change of tab

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多