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
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.Here is an update using this extension :