具有超过3个项目的Flutter BottomNavigationBar会根据所选项目自动应用填充

问题描述

在我的应用程序中,目前有一个BottomNavigationBar,其中包含4个项目。它的代码如下:

  bottomNavigationBar: BottomNavigationBar(
    items: [
      BottomNavigationBarItem(icon: Icon(Icons.home),label: "Startseite"),BottomNavigationBarItem(icon: Icon(Icons.star),label: "Favoriten"),BottomNavigationBarItem(icon: Icon(Icons.casino),label: "Quiz"),BottomNavigationBarItem(icon: Icon(Icons.map),label: "Karte"),],currentIndex: _selectedindex,unselectedItemColor: Colors.grey,showUnselectedLabels: true,selectedItemColor: Colors.blue,onTap: _onItemTapped,),

_selectedindex只是一个整数。 _onItemTapped函数

  void _onItemTapped(int index) {
    setState(() {
      _selectedindex = index;
    });
    setState(() {
      if (index == 0) {
        SwipePage();
      } else if (index == 1) {
        DatabaseProvider.db.queryDb();
        Navigator.of(context).push(
          toFavouritesPage(),);
      } else if (index == 2) {
        Navigator.of(context).push(
          toQuizPage(),);
      } else if (index == 3) {
        Navigator.of(context).push(
          toMapsPage(),);
      }
    });
  }

有3个项目,看起来像这样(所有东西均匀分布):

Bar with 3 items

有4个项目,它看起来像这样(我不知道这种填充的来源):

Bar with 4 items

我如何摆脱填充物,使所有东西保持均匀间隔?

解决方法

将bottomNavigationBarType:设置为固定。默认情况下,四个或更多项目正在转移。这将通过使所选项目更加突出来突出显示所选项目。