Flutter natively not supported by setting colors as a hex string value. But we can implement a simple method to do that job.
Method to convert a hexadecimal string to colour
These are the steps need to follow when converting hex string to int colour.
- Remove the # value
- Add 0xFF to the front of the string if the colour doesn't have opacity value.
- If the colour string has opacity value, discard the FF and append-only 0x.
- Convert to Color.
Color colorConvert(String color) {
color = color.replaceAll("#", "");
if (color.length == 6) {
return Color(int.parse("0xFF"+color));
} else if (color.length == 8) {
return Color(int.parse("0x"+color));
}
}
Originally published at mightytechno
Top comments (1)
Hey @mightytechno ,
I am just learning Flutter. So maybe this is not so good.
But Android Studio was complaining that there was no return value:
So I wrote like this: