-1

I need to show the color from in my container. Mean i have color codes in string format and i need to use as the Container Color.

                  decoration: BoxDecoration(
                  color: widget.product.colors[i].toColor(),
                  borderRadius: BorderRadius.all(Radius.circular(40)),

This is the simple code i am doing like this but its showing error

Invalid argument(s): Can not interpret string 0xFFF6625E

If i remove the .toColor() then its showing

Error: The argument type 'String' can't be assigned to the parameter type 'Color'.

Can any one please tell how can i show this ?

Umaiz Khan
  • 1,319
  • 3
  • 20
  • 66
  • Check this out: https://stackoverflow.com/questions/49835146/how-to-convert-flutter-color-to-string-and-back-to-a-color – Andrej Jan 12 '21 at 10:57

4 Answers4

2

You can use below code:

  decoration: BoxDecoration(
    //make sure that the widget.product.colors[i] is a hex code (i.e: "0xff0000")
     color : Color(int.parse(widget.product.colors[i])),
     borderRadius: BorderRadius.all(Radius.circular(40)),
     border: Border.all(color: Color(int.parse(kPrimaryColor)))
Gourango Sutradhar
  • 1,461
  • 10
  • 16
2

You can write a method like this..

static Color hexToColor(String code) {
    if(code.length == 6) {
      return Color(int.parse(code.substring(0, 6), radix: 16) + 0xFF000000);
    }
    else {
      return Color(int.parse(code.substring(0, 8), radix: 16) + 0x00000000);
    }
  }
Shanto
  • 609
  • 4
  • 8
0

You could use Supercharged package:

"#ff00ff".toColor(); // painless hex to color
"red".toColor(); // supports all web color names
Andrey Gritsay
  • 979
  • 1
  • 10
  • 18
0

You can try this:

color: const Color(widget.product.colors[i]),
shorol
  • 790
  • 5
  • 11