Is there a way to print the actual color name in Flutter. For example, my color is final myColor= Colors.green
so when I print the color such that print(myColor);
I want to see the color name as green, not the HEX value.
CodePudding user response:
This package does exactly that:
https://pub.dev/packages/color_parser
you can use it like this
print( ColorParser.color(Colors.green).toName() ) ;
CodePudding user response:
Nope. Long story short, there isn't unless you do a Map of all the Material colors corresponding to its color name as the key. Will be a whole lot of manual labour though.
CodePudding user response:
I needed the color from the name string, so i created a map.
static const Map<String, Color> colorStringToColor = {
'amber': Colors.amber,
'amberAccent': Colors.amberAccent,
'black': Colors.black,
'black12': Colors.black12,
'black26': Colors.black26,
'black38': Colors.black38,
'black45': Colors.black45,
'black54': Colors.black54,
'black87': Colors.black87,
'blue': Colors.blue,
'blueAccent': Colors.blueAccent,
'blueGrey': Colors.blueGrey,
'brown': Colors.brown,
'cyan': Colors.cyan,
'cyanAccent': Colors.cyanAccent,
'deepOrange': Colors.deepOrange,
'deepOrangeAccent': Colors.deepOrangeAccent,
'deepPurple': Colors.deepPurple,
'deepPurpleAccent': Colors.deepPurpleAccent,
'green': Colors.green,
'greenAccent': Colors.greenAccent,
'grey': Colors.grey,
'indigo': Colors.indigo,
'indigoAccent': Colors.indigoAccent,
'lightBlue': Colors.lightBlue,
'lightBlueAccent': Colors.lightBlueAccent,
'lightGreen': Colors.lightGreen,
'lightGreenAccent': Colors.lightGreenAccent,
'lime': Colors.lime,
'limeAccent': Colors.limeAccent,
'orange': Colors.orange,
'orangeAccent': Colors.orangeAccent,
'pink': Colors.pink,
'pinkAccent': Colors.pinkAccent,
'purple': Colors.purple,
'purpleAccent': Colors.purpleAccent,
'red': Colors.red,
'redAccent': Colors.redAccent,
'teal': Colors.teal,
'tealAccent': Colors.tealAccent,
'transparent': Colors.transparent,
'white': Colors.white,
'white10': Colors.white10,
'white12': Colors.white12,
'white24': Colors.white24,
'white30': Colors.white30,
'white38': Colors.white38,
'white54': Colors.white54,
'white60': Colors.white60,
'white70': Colors.white70,
'yellow': Colors.yellow,
'yellowAccent': Colors.yellowAccent,
};
You could use this to find the color name by doing this:
colorStringToColor.entries.firstWhere((element) => element.value == Colors.blue);
Or you could reverse the map to where the Color
is the key and the name String
is the value.
CodePudding user response:
Not sure whether this would be a feasible solution for you, but one way to achieve this is to extent the Color
class and create a map with the (relevant) color values and respective names:
extension ColorNames on Color {
static const colorNames = {
0xFF000000: 'Colors.black'
// ...
};
String colorName() => colorNames[value] ?? 'Undefined color';
}
Usage:
final color = Colors.black;
print(color.colorName());
CodePudding user response:
The "Color Parser" Package can help in this situation.
https://pub.dev/packages/color_parser
You can print name of the color using toName() method.
CodePudding user response:
Try
String? colorName;
if(myColor == colors.green){
setState({
colorName = "green";
});
}else{
setState({
colorName = "another color";
});
}
print(colorName);