0

I want to convert the string to color but I can't,

My Code

String color = "0xffff5252";
Color result= Color(int.parse(color, radix: 16));
print("$result");

** I get the following problem**

Invalid radix-16 number (at character 1) 0xffff5252

1
  • the string starts with "0x", "-0x" or "+0x", in which case the radix is set to 16 and the "0x" is ignored. Seems it works fine without radix Commented Nov 1, 2022 at 19:31

3 Answers 3

0

You can try something like below

String color = "0xffff5252";
Color result= Color(int.parse(color));
print(result);
Sign up to request clarification or add additional context in comments.

Comments

0

if youe motive is to define a color by first defining into string and then by parsing it, then there's a much simpler way to define color,

    const Color primary = Color(0xFF92A3FD);
const Color secondary = Color(0xFF9DCEFF);
const Color thirdColor = Color(0xFFC58BF2);
const Color fourthColor = Color(0xFFEEA4CE);
const Color bgTextField = Color(0xFFF7F8F8);

const Color facebookColor = Color(0xFF3b5998);
const Color googleColor = Color(0xFFDB4437);

Comments

0

Try this hexToColor function:

 // Construct a color from a hex code string, of the format RRGGBB.
 Color hexToColor(String code) {
 return Color(int.parse(code.substring(1, 6), radix: 16) + 0xFF000000);
 }

This worked for me to store a color & use it as a profile avatar color.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.