如何从BottomNavigationBar中删除图标?

时间:2020-02-05 15:51:35

标签: flutter flutter-layout bottomnavigationview

我只需要BottomNavigationBarItem中的标签,但是找不到删除它们的方法。
您可以将showSelectedLabelsshowUnselectedLabels设置为false来隐藏标签,但是没有对应的图标。

构造函数:

BottomNavigationBar({
    Key key,
    @required this.items,
    this.onTap,
    this.currentIndex = 0,
    this.elevation = 8.0,
    BottomNavigationBarType type,
    Color fixedColor,
    this.backgroundColor,
    this.iconSize = 24.0,
    Color selectedItemColor,
    this.unselectedItemColor,
    this.selectedIconTheme = const IconThemeData(),
    this.unselectedIconTheme = const IconThemeData(),
    this.selectedFontSize = 14.0,
    this.unselectedFontSize = 12.0,
    this.selectedLabelStyle,
    this.unselectedLabelStyle,
    this.showSelectedLabels = true,
    bool showUnselectedLabels,
  })

2 个答案:

答案 0 :(得分:2)

此问题的关键是查看各个BottomNavigationBarItem()

如果您插入一个高度为0.0的容器作为标题,则将获得该项目的所有垂直空间,用于图标或activeIcon。并且由于BottomNavigationBarItem接受任何小部件作为图标或activeIcon,因此您几乎可以随意使用所需的任何内容。

在您的情况下,可能只是一个Text()小部件,如下所示:

BottomNavigationBarItem(
  icon: Text("My Item"),
  activeIcon: Text("My Item"),
  title: Container(
    height: 0.0,
  ),
)

答案 1 :(得分:1)

我认为更好的方法是配置BottomNavigationBar。只需添加这一行,它就会正常工作。无需为每个项目添加行

    selectedIconTheme: IconThemeData(opacity: 0.0, size: 0),
    unselectedIconTheme: IconThemeData(opacity: 0.0, size: 0),

例如

bottomNavigationBar: BottomNavigationBar(
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: 'Home',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.business),
        label: 'Business',
      ),
    ],
    currentIndex: _selectedIndex,
    selectedIconTheme: IconThemeData(opacity: 0.0, size: 0),
    unselectedIconTheme: IconThemeData(opacity: 0.0, size: 0),
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,