createMaterialColor method
Creates a MaterialColor from the color.
Example:
Color color = Colors.blue;
MaterialColor materialColor = color.createMaterialColor();
print('Material Color: $materialColor'); // Output: MaterialColor(primary value: 428051,
// {50: Color(0xffe3f2fd), 100: Color(0xffbbdefb),
// 200: Color(0xff90caf9), 300: Color(0xff64b5f6),
// 400: Color(0xff42a5f5), 500: Color(0xff2196f3),
// 600: Color(0xff1e88e5), 700: Color(0xff1976d2),
// 800: Color(0xff1565c0), 900: Color(0xff0d47a1)});
Implementation
MaterialColor createMaterialColor() {
List strengths = <double>[.05];
Map<int, Color> swatch = <int, Color>{};
final int r = red, g = green, b = blue;
for (int i = 1; i < 10; i++) {
strengths.add(0.1 * i);
}
for (var strength in strengths) {
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(value, swatch);
}