简体   繁体   English

如何更改 Flutter 中的图标颜色?

[英]How to change icon color in Flutter?

This is the code snippet这是代码片段

ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: controller.items.length,
                    itemBuilder: (_, index) {
                      final _item = controller.items[index];
                      if(_item['rota'] == Routes.home){
                          return Obx(() => Container(

                      decoration: (controller.selectedIndex == index)
                                ? const BoxDecoration(
                              border: Border(
                                top: BorderSide(width: 3.0, color: Colors.white),
                                bottom: BorderSide(width: 3.0, color: Colors.white),
                              ),
                            )
                                : null,
                            child: Obx(() =>  Card(
                                color: const Color(0XFF007E94),
                                elevation: 3,

                                child: ListTile(
                                    title: Text(
                                    _item['titulo'],
                                    style: TextStyle(color: (controller.selectedIndex == index ) ? Colors.black :  Colors.white),
                                  ),
                                  leading: _item['icone'],
                                    onTap: () {
                                      controller.selectedIndex = index ;
                                      Get.toNamed(_item['rota']);
                                    },
                                    selected: controller.selectedIndex == index
                            ),
                              ),
                            ),
                          ),
                        );
                      }

The code snippet that changes the color of the letter is this one.改变字母颜色的代码片段就是这个。

  title: Text( _item['titulo'],
    style: TextStyle(color: (controller.selectedIndex == index ) ? Colors.black :  Colors.white),
                              ),

Please, how can I change the icon color taking into account the menu list that is coming from the Page class?请问,考虑到来自 Page 类的菜单列表,如何更改图标颜色?

What I need is when the menu is selected the icon color changes to black.我需要的是选择菜单时,图标颜色更改为黑色。

@override
  void onInit() {
    items.add(
      {
        'titulo': 'Home',
        'icone': const Icon(
          Icons.house_rounded,
          color: Colors.white,
        ),
        'rota': Routes.home,
      },
    );

    items.add(
      {
        'titulo': 'Novas Edições',
        'icone': Image.asset(AppImages.novasEdicoes),
        'rota': Routes.newEditions,
      },
    );

Just check whether the current index is selected.只需检查是否选择了当前索引。 If so, use black color, otherwise, choose the one from your items array.如果是这样,请使用黑色,否则,请从您的 items 数组中选择一个。

For your use case, it's really as simple as this:对于您的用例,它真的很简单:

Image.asset(
  AppImages.novasEdicoes, 
  color: index == controller.selectedIndex ? Colors.black : items[index].color
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM