skip to Main Content
extension MaterialCode on Color {
  MaterialColor toMaterialColor() {
    final List<double> strengths = <double>[.05];
    final Map<int, Color> swatch = {};

    for (var i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }

    for (final strength in strengths) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        (r + ((ds < 0 ? r : (255 - r)) * ds).round()).toInt(),
        (g + ((ds < 0 ? g : (255 - g)) * ds).round()).toInt(),
        (b + ((ds < 0 ? b : (255 - b)) * ds).round()).toInt(),
        1,
      );
    }

    return MaterialColor(value, swatch);
  }
}

This is extension method of making any color to material color .

in latest version of flutter 3.27 red, green, blue is replaced by r,g,b so i made changes accordingly but

in return statement value is depreciated so what will be possible answer?

2

Answers


  1. You could literally copy the source code from the API docs for the current Color.value method. But how exactly are you going to use that in the long run? Why do you need to see a "value"? The legacy "value" is a 32-bit int. The new r/g/b/a are all floating point with a lot more precision.

    Login or Signup to reply.
  2. Here is an update using this extension :

    extension MaterialCode on Color {
      static int floatToInt8(double x) {
        return (x * 255.0).round() & 0xff;
      }
    
      /// A 32 bit value representing this color.
      ///
      /// The bits are assigned as follows:
      ///
      /// * Bits 24-31 are the alpha value.
      /// * Bits 16-23 are the red value.
      /// * Bits 8-15 are the green value.
      /// * Bits 0-7 are the blue value.
      int get toInt32 {
        return floatToInt8(a) << 24 |
            floatToInt8(r) << 16 |
            floatToInt8(g) << 8 |
            floatToInt8(b) << 0;
      }
    
      MaterialColor toMaterialColor() {
        final List<double> strengths = <double>[.05];
        final Map<int, Color> swatch = {};
    
        for (var i = 1; i < 10; i++) {
          strengths.add(0.1 * i);
        }
    
        for (final strength in strengths) {
          final double ds = 0.5 - strength;
          swatch[(strength * 1000).round()] = Color.fromRGBO(
            (r + ((ds < 0 ? r : (255 - r)) * ds).round()).toInt(),
            (g + ((ds < 0 ? g : (255 - g)) * ds).round()).toInt(),
            (b + ((ds < 0 ? b : (255 - b)) * ds).round()).toInt(),
            1,
          );
        }
    
        return MaterialColor(toInt32, swatch);
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search