首页 > 解决方案 > 如何更改 Flutter 底部导航栏中的 SVG 图标?

问题描述

我正在尝试在被点击时更改我的 SVG 图标颜色。我试过了,selectedColor但图标没有改变颜色。所以我试图使用快捷方式 if...else 语句,但我仍然没有改变颜色。

这是代码:

bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: SvgPicture.asset('assets/dashboard-outline.svg', width: 25),
              label: 'Dashboard'
          ),
          BottomNavigationBarItem(
            icon: SvgPicture.asset('assets/tank-outline.svg', color: Colors.black54, width: 30),
            label: 'Tanks',
          ),
          BottomNavigationBarItem(
              icon: SvgPicture.asset('assets/activity-outline.svg', width: 18),
              label: 'Activity'
          ),
          BottomNavigationBarItem(
              icon: SvgPicture.asset('assets/account-outline.svg', width: 20),
              label: 'Account'
          ),
        ],
        currentIndex: _index,
        onTap: _onItemTapped,
        selectedItemColor: Color.fromRGBO(0,99,183,1)
      ),

我有一个不同的 SVG 图片图标文件,可以在点击时更改它。

这是我正在尝试的代码:

bool onSelected = true;

  Widget build(BuildContext context) {
    return Scaffold(
      body: _myPages[_index],

      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: SvgPicture.asset(onSelected == false ? 'assets/dashboard-outline.svg' : 'assets/dashboard-fill.svg', width: 25),
              label: 'Dashboard'
          ),

任何帮助,将不胜感激。

标签: fluttericonsflutter-layoutflutter-dependencies

解决方案


您可以更改您的项目并验证此属性以定义正确的颜色:

 SvgPicture.asset(
 'assets/dashboard-outline.svg', 
  width: 25), 
  color: _currentIndex == 0 ? Colors.red : Colors.black,

推荐阅读