How to use hexadecimal color strings in Flutter

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

Function to convert hexdecimal string to color

These are the steps need to follow when converting hexstring to int color.

  1. Remove the # value
  2. Add 0xFF to the front of the string if the color doesn’t have opacity value.
  3. If  color string have 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