【问题标题】:Is there a way to update BottomNavigationBarItems?有没有办法更新 BottomNavigationBarItem?
【发布时间】:2020-09-08 13:47:46
【问题描述】:

我是新来的 Flutter,一直在尝试使用 BottomNavigationBar。问题是,我根据需要制作了条形图,但是当 Switch 设置为 true 但无法找到解决方法时,我需要它来更新其项目。

我有两个List<BottomNavigationBarItems>,它们有两个不同的导航栏项目,我将它们分配给第三个列表,其中包含取决于开关状态的活动项目。这个变量是在我的 navbarItem 中设置项目的变量,但 setState() 似乎不会重新构建 navBar。

有没有办法更新项目或者我必须用其他小部件制作自己的导航栏?

Non Ready Items

Ready Items

List<BottomNavigationBarItem> nonReadyBottomItems = [
//some items
];
List<BottomNavigationBarItem> readyBottomItems = [
some other items
];
List<BottomNavigationBarItem> = nonReadyBottomItems;

Scaffold(
      body: Center(
        child: Switch(
            value: switchConnect,
            onChanged: (bool boolean) {
              setState(() {
                switchConnect = boolean;
              });
            }),
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (int i) {
          setState(() {
            pageIndex = i;
            if (switchConnect) {
              activeItems = readyBottomItems;
            } else if (!switchConnect) {
              activeItems = nonReadyBottomItems;
            }
          });
        },
        currentIndex: pageIndex,
        type: BottomNavigationBarType.fixed,
        items: activeItems,
      ),
    );

【问题讨论】:

    标签: flutter navigationbar


    【解决方案1】:

    是的,您创建状态完整的底部导航栏以更改小部件的状态。

    打开底页

    InkWell(
              onTap: () {
                showModalBottomSheet(
                    context: context,
                    isScrollControlled: true,
                    builder: (context) {
                      return ModalBottomSheet(
    
                          );
                    });
              })
    

    有状态的底页

    class ModalBottomSheet extends StatefulWidget {
    
    
      @override
     _ModalBottomSheetState createState() => _ModalBottomSheetState();
    }
    
     class _ModalBottomSheetState extends State<ModalBottomSheet>
      {
      @override
    Widget build(BuildContext context) {
    
    // TODO: implement build
    return Wrap(
      children: <Widget>[
        Container(
          margin:
              EdgeInsets.only(left: 10.0, right: 10.0, top: 15.0, bottom: 15.0),
          child: Column( 
           Widgets(),
          
            )
          )
        ],
      );
     }
    
    } 
    

    【讨论】:

      【解决方案2】:

      我实际上发现了 setState() 不起作用的原因。似乎 activeItems 变量的分配存在一些问题,因此它没有更改条形图,因为没有新内容。

      所以 setState 实际上在 BottomNavBar 上工作!

      【讨论】:

        猜你喜欢
        • 2010-10-13
        • 1970-01-01
        • 2022-01-26
        • 2017-12-03
        • 2020-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-13
        相关资源
        最近更新 更多