Reputation: 5
I have app that communicates with my API. I need to choose MaterialColor on my flutter app and send it to my API and later get it from API and use it in app. When I send it to my API it looks like this MaterialColor(primary value: Color(0xff4caf50))
but I can't use it later because it's not possible to cast String like this to Color.
Is there a simple way to reuse color that was sent to API?
Thank you in advance.
Upvotes: 0
Views: 1865
Reputation: 72
try this, works for me.
First create class for covert hexadecimal to material color
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
return int.parse(hexColor, radix: 16);
}
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
Convert material color to Hexadecimal and send to your API
Color materialColor = Colors.blue;
print(materialColor); //MaterialColor(primary value: Color(0xff2196f3))
final hexColor = materialColor.value.toRadixString(16); //convert to hex
//send this value hexColor to your server
......
print(hexColor); // ff2196f3
//how to covert back to material color
print(HexColor(hexColor)); //Color(0xff2196f3)
How to use /example getting color from API
Container(
height:300,
width:300,
color:HexColor(<color_from_API>),
)
Upvotes: 1