How to use hexadecimal color strings in Flutter

Flutter natively not supported by setting colors as a hex string value. But we can implement a simple method to do that job.

Method to convert a hexa decimal string to color

These are the steps need to follow when converting hex string to int colour.

  1. Remove the # value
  2. Add 0xFF to the front of the string if the colour doesn’t have opacity value.
  3. If the colour string has opacity value, discard the FF and append-only 0x.
  4. Convert to Color.
    
        Color colorConvert(String color) {
            color = color.replaceAll("#", "");
            if (color.length == 6) {
              return Color(int.parse("0xFF"+color));
            } else if (color.length == 8) {
              return Color(int.parse("0x"+color));
            }
          }
    
hexadecimal color strings in Flutter
You Will Like Also
Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

x