skip to Main Content

After upgrading Flutter to version 3.27.0, I started seeing some warnings related to deprecated properties in the Color class:

Warning:

info: 'value' is deprecated and shouldn't be used. Use component accessors like .r or .g. (deprecated_member_use)

This warning is for the following code snippet:

String get colorHexString => color.value.toRadixString(16).substring(2);

Warning:

info: 'green' is deprecated and shouldn't be used. Use .g. (deprecated_member_use)

This warning is for this part of the code:

String toHex({bool hashSign = false, bool withAlpha = false}) =>
  '${hashSign ? '#' : ''}'
          '${_generateAlpha(alpha: alpha, withAlpha: withAlpha)}'
          '${red.toRadixString(16).padLeft(2, '0')}'
          '${green.toRadixString(16).padLeft(2, '0')}'
          '${blue.toRadixString(16).padLeft(2, '0')}'
      .toUpperCase();

2

Answers


  1. Chosen as BEST ANSWER

    The following solutions worked for me:

    1. colorHexString Getter:

    This method constructs the hex string for a Color object, including its alpha value:

     String get colorHexString {
        final red = (color.r * 255).toInt().toRadixString(16).padLeft(2, '0');
        final green = (color.g * 255).toInt().toRadixString(16).padLeft(2, '0');
        final blue = (color.b * 255).toInt().toRadixString(16).padLeft(2, '0');
        final alpha = (color.a * 255).toInt().toRadixString(16).padLeft(2, '0');
    
        return '$alpha$red$green$blue';
      }
    
    1. toHex Method:

    This method generates a hex string for a Color object with optional parameters for including a hash sign (#) and alpha transparency:

       String toHex({bool hashSign = false, bool withAlpha = false}) {
        final alpha = (a * 255).toInt().toRadixString(16).padLeft(2, '0');
        final red = (r * 255).toInt().toRadixString(16).padLeft(2, '0');
        final green = (g * 255).toInt().toRadixString(16).padLeft(2, '0');
        final blue = (b * 255).toInt().toRadixString(16).padLeft(2, '0');
    
        return '${hashSign ? '#' : ''}'
                '${withAlpha ? alpha : ''}'
                '$red$green$blue'
            .toUpperCase();
      }
    

  2. I created this utils class to convert from Color to int and vice versa

    import 'dart:ui';
    
    class AppColorUtils {
      // Convert Color to int
      static int colorToInt(Color color) {
        // Convert alpha from 0.0-1.0 to 0-255
        final a = (color.a * 255).round();
        // For r,g,b, just round to the nearest integer
        final r = color.r.round();
        final g = color.g.round();
        final b = color.b.round();
    
        // Combine the components into a single int using bit shifting
        return (a << 24) | (r << 16) | (g << 8) | b;
      }
    
    // Convert int to Color
      static Color intToColor(int value) {
        // Extract alpha and convert to 0.0-1.0
        final a = ((value >> 24) & 0xFF) / 255.0;
        // Extract r,g,b as direct double values
        final r = ((value >> 16) & 0xFF);
        final g = ((value >> 8) & 0xFF);
        final b = (value & 0xFF);
    
        return Color.fromRGBO(r.round(), g.round(), b.round(), a);
      }
    }
    

    In your case you would only have to do:

    String get colorHexString => AppColorUtils.colorToInt(color.value).toRadixString(16).substring(2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search