FLUTTER BottomNavigationBar 第一项始终处于激活状态

时间:2021-01-17 09:45:40

标签: flutter navigationbar

我正在修改应用程序布局并使用导航栏,但我的第一个项目总是被激活,无论我是否按下它。有什么想法吗?

在第一项没有显示后,我的标签也是如此。

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
        ),

      bottomNavigationBar: BottomAppBar(
        notchMargin: 5,
        shape: CircularNotchedRectangle(),
       // color: Colors.black,
        child:
            BottomNavigationBar(
          elevation: 50,
          //backgroundColor: Colors.black,
          unselectedItemColor: Colors.black,
          selectedItemColor: Colors.green,
          items: [
//the first one is always activated
            BottomNavigationBarItem(
              icon: Icon(Icons.accessibility_new),
              label: ("Lo"),
              activeIcon: Icon(Icons.book),
             //IF I add background color here it will fill the entire bar
              //backgroundColor: Colors.green,
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.book),
              label: ("Ed"),
            ),
            BottomNavigationBarItem(
                icon: Icon(Icons.access_time),
                label: ("Pi"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.accessible_sharp),
              label: ("ac"),
            ),
          ],
        ),
      ),
    );
  }
}

Flutter screen

2 个答案:

答案 0 :(得分:1)

使用一个变量来保存当前选中的索引

int selectedIndex = 0;
// ....

bottomNavigationBar: BottomAppBar(
  notchMargin: 5,
  child: BottomNavigationBar(
    unselectedItemColor: Colors.black,
    selectedItemColor: Colors.green,
    onTap: (index) {
      setState(() {
        selectedIndex = index;
      });
    },
    currentIndex: selectedIndex,
    items: [
      BottomNavigationBarItem(
        icon: Icon(Icons.accessibility_new),
        label: ("Lo"),
        activeIcon: Icon(Icons.book),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.book),
        label: ("Ed"),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.access_time),
        label: ("Pi"),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.accessible_sharp),
        label: ("ac"),
      ),
    ],
  ),
),

答案 1 :(得分:1)

为了正确显示(未选择的)标签,请尝试将此行添加到您的底部导航栏:

showUnselectedLabels: true,

如果这不起作用,也许您未选择的标签与背景颜色相同。

祝你的项目好运;).
-塞德里克