is there a way to convert from Hex value to the color name using any package in flutter?
Asked
Active
Viewed 826 times
1
-
Just convert your hex to `int` and create a `Color(intValueOfHex)`. – rickimaru Feb 25 '22 at 23:10
-
@rickimaru OP wants a color *name*. – jamesdlin Feb 25 '22 at 23:12
-
Please provide enough code so others can better understand or reproduce the problem. – Community Feb 26 '22 at 00:27
-
If you talk about the material colors, there is no way to get the color name from the hex value. You have to make your own class to do that – F Perroch Feb 26 '22 at 00:35
2 Answers
1
There is this package called color_convert 1.0.2 that should help? However, it only takes color names found on a GitHub list from what I understand. So I don't think it can be used for a wide range of colors (the GitHub list has around 147 colors in RGB which I think can be converted to HexCode and then Text), but you could certainly take this color converter and work off of it to build your own for more specific colors. As of current I don't think there is a package that does exactly what you are looking for, but this is a good start.

Jared
- 189
- 12
0
You can achieve without using any pacakges.You can convert hex color as follows:
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return int.parse(hexColor, radix: 16);
}
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
Then access as:
static Color cardColor = HexColor('#D4AA3A');

Nabin Dhakal
- 1,949
- 3
- 20
- 48