fluttercolorshexcolor-codescolor-conversion

How to parse hex color code into their respective integer value in flutter


I'm designing a container and pulling the colorcode from firebase database which is stored as String. Now I want to convert that string(Hex code) into integer(some hex code contains only integer), because Color(0XFFf3234f) take argument as integer only. decoration: BoxDecoration(color:Color(ItemList[index].ColorCode)),, then it is throwing an error The argument type 'String' can't be assigned to the parameter type 'int'. Then I've used:

class HexColor extends Color {

  static int _getColorFromHex(String hexColor) {

    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {

    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}

This is working fine for colorcode having strings and numbers but it gets failed for color code having numbers only, by throwing following error: The method 'toUpperCase' was called on null. Receiver: null Tried calling: toUpperCase()

Then I tried separating numeric values alone but it didn't work at all.

Here are few color codes I'm using: a74556,827255,a3d57a,662482


Solution

  • just simply convert ColorCode to int:

    Color(int.parse('0xff${ItemList[index].ColorCode}'))