toMaterialColor method

MaterialColor toMaterialColor()

Converts this Color to a MaterialColor with shades of the same color.

The returned MaterialColor has the same base color as this Color, and includes ten shades of the color, ranging from 50% to 100% opacity.

For example, to create a MaterialColor from Colors.blue, you can use Colors.blue.toMaterialColor().

Returns a MaterialColor object.

Implementation

MaterialColor toMaterialColor() {
  List strengths = <double>[.05];
  final swatch = <int, Color>{};
  final int r = color.red, g = color.green, b = color.blue;

  for (int i = 1; i < 10; i++) {
    strengths.add(0.1 * i);
  }
  strengths.forEach((strength) {
    final double ds = 0.5 - strength;
    swatch[(strength * 1000).round()] = Color.fromRGBO(
      r + ((ds < 0 ? r : (255 - r)) * ds).round(),
      g + ((ds < 0 ? g : (255 - g)) * ds).round(),
      b + ((ds < 0 ? b : (255 - b)) * ds).round(),
      1,
    );
  });
  return MaterialColor(color.value, swatch);
}