Dart 如何更改BottomNavigationBarItem图标选中时,颤振

Dart 如何更改BottomNavigationBarItem图标选中时,颤振,dart,flutter,bottombar,Dart,Flutter,Bottombar,我是个新手。我有一个带有4项的BottomNavigationBar。我想在按下时更改项目的图标。请帮忙 这就是我到目前为止所做的 bottomNavigationBar : new BottomNavigationBar( currentIndex: index, onTap: (int index) { setState(() { this.index = index; } )

我是个新手。我有一个带有4项的
BottomNavigationBar
。我想在按下时更改项目的图标。请帮忙

这就是我到目前为止所做的

bottomNavigationBar : new BottomNavigationBar(
        currentIndex: index,
        onTap: (int index) {
          setState(() {
            this.index = index;
          }
          );
          _navigateToScreens(index);
        },
        type: BottomNavigationBarType.fixed,
        items: [
          new BottomNavigationBarItem(
              backgroundColor: Colors.white,
              icon: new Image.asset('images/1.0x/icon1.png'),
              title: new Text("Route1", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0))),
          new BottomNavigationBarItem(
              icon: new Image.asset('images/1.0x/icon2.png'),
              title: new Text("Route2", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0))),
          new BottomNavigationBarItem(
              icon: new Image.asset('images/1.0x/icon3.png'),
              title: new Text("Route3", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0),)),
          new BottomNavigationBarItem(
              icon: new Image.asset('images/1.0x/icon4.png'),
              title: new Text("Route4", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0),))
        ]);

您可以通过检查当前索引是否等于
BottomNavigationBarItem
index的索引来更改图标

静态索引值的简单示例:

bottomNavigationBar : new BottomNavigationBar(
        currentIndex: index,
        onTap: (int index) {
          setState(() {
            this.index = index;
          }
          );
          _navigateToScreens(index);
        },
        type: BottomNavigationBarType.fixed,
        items: [
          new BottomNavigationBarItem(
              backgroundColor: Colors.white,
              icon: index==0?new Image.asset('images/1.0x/icon1.png'):new Image.asset('images/1.0x/newIcon.png'),
              title: new Text("Route1", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0))),
          new BottomNavigationBarItem(
              icon: index==1?new Image.asset('images/1.0x/icon2.png'):new Image.asset('images/1.0x/newIcon.png'),
              title: new Text("Route2", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0))),
          new BottomNavigationBarItem(
              icon: index==2?new Image.asset('images/1.0x/icon3.png'):new Image.asset('images/1.0x/newIcon.png'),
              title: new Text("Route3", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0),)),
          new BottomNavigationBarItem(
              icon: index==3?new Image.asset('images/1.0x/icon4.png'):new Image.asset('images/1.0x/newIcon.png'),
              title: new Text("Route4", style: new TextStyle(
                  color: const Color(0xFF06244e), fontSize: 14.0),))
        ])

希望有帮助

如果您只想更改BottomNavigationBarItem图标的颜色,则不需要为一个图标设置两个不同颜色的图像。一个就够了

您可以使用ImageIcon从自定义图像创建图标,并使用其颜色属性更改图标颜色,使用currentIndex的值,如下所示:

bottomNavigationBar: BottomNavigationBar(
    type: BottomNavigationBarType.fixed,
    currentIndex: currentTab,
    onTap: (int index) {
      setState(() {
        currentTab = index;
        currentPage = pages[index];
      });
    },
    items: <BottomNavigationBarItem>[
      BottomNavigationBarItem(
          icon: ImageIcon(
            AssetImage("assets/img/tab1.png"),
            color: currentTab == 0
                ? Colors.orange
                : Colors.black,
          ),
          title: Text('Title 1',
            style: TextStyle(
                fontSize: 10.0,
                color: currentTab == 0
                    ? Colors.orange
                    : Colors.black),
          )
      ),
      BottomNavigationBarItem(
          icon: ImageIcon(
            AssetImage("assets/img/tab2.png"),
            color: currentTab == 1
                ? Colors.orange
                : Colors.black,
          ),
          title: Text('Title 2',
            style: TextStyle(
                fontSize: 10.0,
                color: currentTab == 1
                    ? Colors.orange
                    : Colors.black),)
      ),
      BottomNavigationBarItem(
          icon: ImageIcon(
            AssetImage("assets/img/tab3.png"),
            color: currentTab == 2
                ? Colors.orange
                : Colors.black,
          ),
          title: Text('Title 3',
            style: TextStyle(
                fontSize: 10.0,
                color: currentTab == 2
                    ? Colors.orange
                    : Colors.black),)
      ),
      BottomNavigationBarItem(
          icon: ImageIcon(
            AssetImage("assets/img/tab4.png"),
            color: currentTab == 3
                ? Colors.orange
                : Colors.black,
          ),
          title: Text('Title 4',
            style: TextStyle(
                fontSize: 10.0,
                color: currentTab == 3
                    ? Colors.orange
                    : Colors.black),)
      )
    ],
  ),
bottomNavigationBar:bottomNavigationBar(
类型:BottomNavigationBarType.fixed,
currentIndex:currentTab,
onTap:(int索引){
设置状态(){
currentTab=索引;
currentPage=页面[索引];
});
},
项目:[
底部导航气压计(
图标:图像图标(
资产评估(“资产/img/tab1.png”),
颜色:currentTab==0
?颜色:橙色
:颜色。黑色,
),
标题:文本(“标题1”,
样式:TextStyle(
字体大小:10.0,
颜色:currentTab==0
?颜色:橙色
:颜色。黑色),
)
),
底部导航气压计(
图标:图像图标(
AssetImage(“assets/img/tab2.png”),
颜色:currentTab==1
?颜色:橙色
:颜色。黑色,
),
标题:文本(“标题2”,
样式:TextStyle(
字体大小:10.0,
颜色:currentTab==1
?颜色:橙色
:颜色。黑色),)
),
底部导航气压计(
图标:图像图标(
AssetImage(“assets/img/tab3.png”),
颜色:currentTab==2
?颜色:橙色
:颜色。黑色,
),
标题:文本(“标题3”,
样式:TextStyle(
字体大小:10.0,
颜色:currentTab==2
?颜色:橙色
:颜色。黑色),)
),
底部导航气压计(
图标:图像图标(
AssetImage(“assets/img/tab4.png”),
颜色:currentTab==3
?颜色:橙色
:颜色。黑色,
),
标题:文本(“标题4”,
样式:TextStyle(
字体大小:10.0,
颜色:currentTab==3
?颜色:橙色
:颜色。黑色),)
)
],
),

希望有人会觉得这个有用

在BottomNavigationBarItem中的颤振中添加了一个新功能,即
活动图标
。我们可以用它来告诉什么应该是图标时,一个标签是活跃的

bottomNavigationBar : new BottomNavigationBar(
    currentIndex: index,
    onTap: (int index) {
      setState(() {
        this.index = index;
      }
      );
      _navigateToScreens(index);
    },
    type: BottomNavigationBarType.fixed,
    items: [
      new BottomNavigationBarItem(
          backgroundColor: Colors.white,
          icon: new Image.asset('images/1.0x/icon1.png'),
          activeIcon:new Image.asset('images/1.0x/newIcon.png'),
          title: new Text("Route1", style: new TextStyle(
              color: const Color(0xFF06244e), fontSize: 14.0))),
      new BottomNavigationBarItem(
          icon: new Image.asset('images/1.0x/icon2.png'),
          activeIcon:new Image.asset('images/1.0x/newIcon.png'),
          title: new Text("Route2", style: new TextStyle(
              color: const Color(0xFF06244e), fontSize: 14.0))),
      new BottomNavigationBarItem(
          icon: new Image.asset('images/1.0x/icon3.png'),
          activeIcon: new Image.asset('images/1.0x/newIcon.png'),
          title: new Text("Route3", style: new TextStyle(
              color: const Color(0xFF06244e), fontSize: 14.0),)),
      new BottomNavigationBarItem(
          icon: new Image.asset('images/1.0x/icon4.png'),
          activeIcon: new Image.asset('images/1.0x/newIcon.png'),
          title: new Text("Route4", style: new TextStyle(
              color: const Color(0xFF06244e), fontSize: 14.0),))
    ]),

希望有人会觉得这很有用。

如果有人正在寻找更改底部导航栏项目颜色的解决方案,当“type”设置为“shift”时,请尝试一下:

type: BottomNavigationBarType.shifting,
  items: [
    BottomNavigationBarItem(
      activeIcon: Icon(
          Icons.home,
          color: Colors.grey[700],
        ),
      icon: Icon(
          Icons.home,
          color: Colors.grey,
        ),
      title: Text(
        'Home',
        style: TextStyle(
          color: Colors.grey[600]
          ),
        ),
    ),

如果您只想更改颜色而不想更改图标本身,则选择图标时,
fixedColor
确定图标的颜色:

BottomNavigationBar(
        ...
        fixedColor: Colors.red,
        ...
)

我就是这样解决的。在底部导航栏中,有两个属性selectedItemColorunselectedItemColor

bottomNavigationBar: BottomNavigationBar(
    ...
    selectedItemColor: Theme.of(context).primaryColor,
    unselectedItemColor: Theme.of(context).secondaryHeaderColor,
    ...
    items: [
      BottomNavigationBarItem(
        icon: Icon(Icons.search),
        title: Text('Search'),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.star),
        title: Text('Featured'),
      ),
   ],
  ),
2020年

双向

目前更好的方法是:

    selectedItemColor: Colors.white,
    unselectedItemColor: Color(0xFFF434A50),
    items: <BottomNavigationBarItem>[
        BottomNavigationBarItem(
            icon: ImageIcon(AssetImage("assets/tab1.png"),),
            title: Text('Agents'),
        ),
    ]
activeIcon-所选选项卡

图标-取消选择选项卡


如果您正在显示图像资源中的图标,则以这种方式更改底部导航栏的活动图标:

BottomNavigationBarItem(
              activeIcon: Image.asset(
                'assets/images/useractive.png',
                height: 25,
                width: 25,
              ),
              icon: Image.asset(
                'assets/images/user.png',
                height: 25,
                width: 25,
              ),
              title: Text('My Time Out')
),

只想添加到现有答案中:虽然
fixedColor
(un)selectedItemColor
是解决问题的方法,但有一个问题:

它们将被
BottomNavigationBarItem.icon.color
覆盖


因此,请确保首先去掉硬编码的图标颜色。

谢谢您的回复。我把它贴出来作为答复。请检查。您可以编辑问题,而不是作为答案发布。是。我的错误。我也会这样做的。谢谢你这个坏主意。那太乏味了,写不出来。相反,您可以将列表包装成
主题
。并覆盖一些属性。例如
primaryColor
accentColor
,以及
caption.color
。这些值是BottomNavigationBar用于显示图标的值,具体取决于它们是否被选中。我确实同意您的看法。但这会如何更改图标,即
图像。这里的资产
?哦,我的错。我以为这是关于更改选定图标的颜色。那就忽略我的消息吧。@Darky,是的,我需要更改图标,否则你的答案会是正确的。谢谢我还需要在移动到其他屏幕后按“后退”按钮时,在第一个BottomNavigationBarItem中设置第一个图像,在其余项目中设置第二个图像(在条件表达式中)。这怎么可能?请提供帮助。如果您想更改所选图标的颜色,只需在底部导航栏上设置“fixedColor”。BottomNavigationBar(fixedColor:colorPrimary,项:[BottomNavigationBarItem(图标:图标(Icons.home))、标题:文本(“第一”)、BottomNavigationBarItem(图标:图标(图标.饼图)、标题:文本(“洞察”)]、,)这应该是与@miloss solution一起选择的答案。非常简洁,无需每次点击标签时切换图像这似乎只影响文本标签的颜色。图标颜色不受影响。不幸的是,我也尝试了
(取消)selectediconteme
,但没有成功。
BottomNavigationBarItem(
              activeIcon: Image.asset(
                'assets/images/useractive.png',
                height: 25,
                width: 25,
              ),
              icon: Image.asset(
                'assets/images/user.png',
                height: 25,
                width: 25,
              ),
              title: Text('My Time Out')
),
color: _selectedIndex == ThisIndex?SelectedColor:normalColor,