Want to use hexadecimal color strings in Flutter? The Color class expects an ARGB integer. Since you try to use it with RGB value, represent it as int and prefix it with 0xff.
1 | Color mainColor = Color(0xffb74093); |
If you get annoyed by this and still wish to use strings, you can extend Color and add a string constructor
1 2 3 4 5 6 7 8 9 10 11 | 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)); } |
usage
1 2 | Color color1 = HexColor("b74093"); Color color2 = HexColor("#b74093"); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.