Start with custom methods:

/// color creation method
  /// - [colorString] Color value
  /// - [alpha] transparency (default 1, 0-1)
  /// 
  // You can enter multiple formats of color codes, such as 0x000000, 0xFF000000,#000000
  static Color ADColor(String colorString, {double alpha = 1.0{})String colorStr = colorString;
    // colorString is not prefixed with 0xFF and has a length of 6
    if(! colorStr.startsWith('0xff') && colorStr.length == 6) {
      colorStr = '0xff' + colorStr;
    }
    // colorString is 8 bits, such as 0x000000
    if(colorStr.startsWith('0x') && colorStr.length == 8) {
      colorStr = colorStr.replaceRange(0.2.'0xff');
    }
    // colorString is 7 bits, such as #000000
    if(colorStr.startsWith(The '#') && colorStr.length == 7) {
      colorStr = colorStr.replaceRange(0.1.'0xff');
    }
    // Get the RGB channels for the color values
    Color color = Color(int.parse(colorStr));
    int red = color.red;
    int green = color.green;
    int blue = color.blue;
    // fromRGBO returns a color with transparency and RGB values
    return Color.fromRGBO(red, green, blue, alpha);
  }
Copy the code

Use:

Color myolor = The class name defined by the method. ADColor('0xfffa5956');
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = here is a problem, interested friends can continue to look at:

I recently learned about Flutter development and wanted to generate Color objects based on the hexadecimal Color values returned by the background. Methods that Flutter provides:

Color(int value);
Copy the code

As you can see, the value must be of type int. If you get the color value String, you can use the following method to convert it to int:

Color(int.parse(colorStr));
Copy the code

This method can be used for simple color requirements, but if you want to add transparency, it becomes more complicated. The official approach offers two:

Color.fromARGB(int a, int r, int g, int b)
Color.fromRGBO(int r, int g, int b, double opacity)
Copy the code

Should we put a simple color value to reverse his RGB channel? Moving on, is there a separate way to set transparency? The answer is yes, and there are two:

withAlpha(int a)
withOpacity(double opacity)
Copy the code

To test this, write:

// 1. Assign a color value
Color color = Color(int.parse('0xfffa5956'));
// 2. Set its transparency
color.withOpacity(0.1);
Copy the code

However, there is no effect, the color is still opaque, check to learn, 0x followed by the first two FF generally stands for transparency, ff itself stands for opacity, refer to the table: 00%=FF (opaque) 5%=F2 10%=E5 15%=D8 20%=CC 25%=BF 30%=B2 35%=A5 40%=99 45%= 8C 50%=7F 55%=72 60%=66 65%=59 70%= 4C 75%=3F 80%=33 85%=21 90%=19 95%=0c 100%=00

Maybe Flutter has set its transparency based on your color value, so setting the transparency separately will have no effect.

So we want to achieve color value plus transparency control, we must know his RGB channel, and then through fromARGB or fromRGBO method to achieve. The Color object has the RGB get method:

/// The red channel of this color in an 8 bit value.
  int get red => (0x00ff0000 & value) >> 16;

  /// The green channel of this color in an 8 bit value.
  int get green => (0x0000ff00 & value) >> 8;

  /// The blue channel of this color in an 8 bit value.
  int get blue => (0x000000ff & value) >> 0;
Copy the code

Hence the above custom method.