Reputation: 21
Is it possible to convert value of Material Color to Hex Code in flutter, I have been trying for a while now but i just cant work around it. any help would be appreciated.
Upvotes: 2
Views: 1669
Reputation: 3637
const Color colorPrimary = const Color(0xFF32ad79);
int hexCode = colorPrimary.value
Upvotes: 0
Reputation: 1893
There is a utils package that contains a ColorUtils class that can convert hex to int and int to hex. That can be used to create the Flutter colors or a HEX value from the Flutter color.
Github: https://github.com/Ephenodrom/Dart-Basic-Utils
PuDev: https://pub.dev/packages/basic_utils
Install :
basic_utils: ^2.0.0
Example :
Color color = Color(ColorUtils.hexToInt("#FFFFFF"));
String hex = ColorUtils.intToHex(color.value);
Upvotes: 3
Reputation: 24726
Take the integer representation and convert it into a hexadecimal string:
String hexCode = '#${color.value.toRadixString(16).padLeft(8, '0')}';
Upvotes: 2