简体   繁体   English

如何更改 Dart Flutter listTile 项目的图标颜色?

[英]How to change the color of icons of Dart Flutter listTile items?

在此处输入图像描述

For items in the listTile, the color of the icons is yellow by default.对于 listTile 中的项目,图标的颜色默认为黄色。 How can I change it to black?我怎样才能把它变成黑色?

Codes:代码:

body: Container(
  padding: EdgeInsets.all(7),
  child: Column(
    
    children: [
      
      Text(defaultFlag, style: TextStyle(fontSize: 24),),
      SizedBox(height: 10,),


      Expanded(
        
        child: ListView.builder(

          itemCount: levels.length,
          itemBuilder: (BuildContext context, int index) {
            return Padding(
              padding: const EdgeInsets.all(2.0),
              child: Container(
                child: InkWell(
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      border: Border.all(color: Colors.black),
                    ),
                    child: ListTile(
                      leading: languageLevelIcon(levels[index].languageLevelName,),
                      title: Text(levels[index].languageLevelName, style: TextStyle(fontSize: 20),),
                      trailing: Icon(Icons.arrow_forward_ios, color: Colors.black,),
                      iconColor: Colors.black,
                    ),
                    ),
                  onTap: () {
                    print("tıklandı " + levels[index].languageLevelName);
                  },
                ),
              ),
            );
          },
        ),
      ),
    ],
  ),
),
      
        
      
    );
  }

  Widget languageLevelIcon(String levelName) {
    switch (levelName) {
      case "A1":
        return Icon(Icons.star, color: Colors.yellow,);
      case "A2":
        return Icon(Icons.star_outline, color: Colors.yellow,);
      case "B1":
        return Icon(Icons.keyboard_arrow_up, color: Colors.yellow,);
      case "B2":
        return Icon(Icons.arrow_drop_up, color: Colors.yellow,);
      case "C1":
        return Icon(Icons.add_circle ,color: Colors.yellow,);
      case "C2":
        return Icon(Icons.add_circle_outline_outlined, color: Colors.yellow,);
      default:
        return Icon(Icons.no_encryption, color: Colors.yellow,);
    }
  }
}

class languageLevel {
  String languageLevelName;

  languageLevel(this.languageLevelName);
}

I want to change it because the yellow color is not very clear.我想改变它,因为黄色不是很清楚。 Thanks for the help in advance.我在这里先向您的帮助表示感谢。 I want to change it to black color.我想把它改成黑色。

I set the icon based on languageLevelName .我根据languageLevelName设置图标。 Thank you very much in advance for the help.非常感谢您的帮助。

Could you define the Color of your languageLevelIcon as a parameter with a default value?您能否将languageLevelIconColor定义为具有默认值的参数?

  Icon languageLevelIcon(String levelName, [Color? color = Colors.yellow]) {
    switch (levelName) {
      case "A1":
        return Icon(Icons.star, color: color);
      case "A2":
        return Icon(Icons.star_outline, color: color);
      case "B1":
        return Icon(Icons.keyboard_arrow_up, color: color);
      case "B2":
        return Icon(Icons.arrow_drop_up, color: color);
      case "C1":
        return Icon(Icons.add_circle, color: color);
      case "C2":
        return Icon(Icons.add_circle_outline_outlined, color: color);
      default:
        return Icon(Icons.no_encryption, color: color);
    }
  }
}

Full code sample完整代码示例

在此处输入图像描述

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

// PRESENTATION LAYER

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.light(),
      home: const Scaffold(
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: ListView.builder(
            itemCount: 5,
            itemBuilder: (BuildContext context, int index) {
              return Padding(
                padding: const EdgeInsets.all(5.0),
                child: InkWell(
                  child: Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      border: Border.all(color: Colors.black),
                      color: Colors.yellow.shade200,
                    ),
                    child: ListTile(
                      leading:
                          languageLevelIcon('A$index', Colors.green.shade700),
                      title: Text(
                        'Level $index',
                        style: const TextStyle(fontSize: 20),
                      ),
                      trailing: const Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.black,
                      ),
                      iconColor: Colors.black,
                    ),
                  ),
                  onTap: () {},
                ),
              );
            },
          ),
        ),
      ),
    );
  }

  Icon languageLevelIcon(String levelName, [Color? color = Colors.yellow]) {
    switch (levelName) {
      case "A1":
        return Icon(Icons.star, color: color);
      case "A2":
        return Icon(Icons.star_outline, color: color);
      case "B1":
        return Icon(Icons.keyboard_arrow_up, color: color);
      case "B2":
        return Icon(Icons.arrow_drop_up, color: color);
      case "C1":
        return Icon(Icons.add_circle, color: color);
      case "C2":
        return Icon(Icons.add_circle_outline_outlined, color: color);
      default:
        return Icon(Icons.no_encryption, color: color);
    }
  }
}

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

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