Programming question & answer

How can i use hexadecimal color code in Flutter?

RRTutors community answer Published Nov 27, 2019

Answer

In Flutter the  Color  class only accepts integers as parameters
Other wise we need to use names constructors fromARGB/fromRGBO
If we have the Hexa color code then we need to convert this hexa color code into int type color code.
Option 1:
This we can achive by replace "#" with 0XFF in Hexa string and create const color variable by

const color = const Color(0xff0276e8);


Then we can use this value to any where.
Option 2:
Create a class which extends color class

class ColorUtil extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

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

and use this in any wdiget by

Color color=ColorUtil("#0276e8")
Color color=ColorUtil("0276e8")