how to add custom color to flutter?
basically flutter uses color AARRGGBB
format you can use below color code with any color property like:
new Container(color: const Color(0xff2980b9));
AA = transparency
RR = red
GG = green
BB = blue
now if you want to create custom color 8-digit code from 6-digit color code then just append transparency (AA) value to it
Transparency percentages Following are some example transparency percentages and their hex values
100% - FF
95% - F2
90% - E6
85% - D9
80% - CC
75% - BF
70% - B3
65% - A6
60% - 99
55% - 8C
50% - 80
45% - 73
40% - 66
35% - 59
30% - 4D
25% - 40
20% - 33
15% - 26
10% - 1A
5% - 0D
0% - 00
in my case i always use AA = ff because 6-digit color has ff transparency. for 6-digit color best site
There are several ways to do it, but this is the method I prefer to use. It's dead simple.
Create a custom
MaterialColor myColor = MaterialColor(0xFF880E4F, color);
Create a map and as you will see below that I modify the opacity channel from 50 through to 900 to give you the various color degrees of opacity.
Map<int, Color> color =
{
50:Color.fromRGBO(4,131,184, .1),
100:Color.fromRGBO(4,131,184, .2),
200:Color.fromRGBO(4,131,184, .3),
300:Color.fromRGBO(4,131,184, .4),
400:Color.fromRGBO(4,131,184, .5),
500:Color.fromRGBO(4,131,184, .6),
600:Color.fromRGBO(4,131,184, .7),
700:Color.fromRGBO(4,131,184, .8),
800:Color.fromRGBO(4,131,184, .9),
900:Color.fromRGBO(4,131,184, 1),
};
Same works for Color.fromRGBA if you prefer using Alpha over Opacity.
I would like to point out that I saw you were trying to do this.
primarySwatch: Colors.black[500]
This will give you the an error. You have to use the base MaterialColor you created. Using the color degree modifiers will make the compiler unhappy.
It's a MaterialColor
object (not Color
) you should assign for a swatch property, providing color values for the ten different luminances.
Many people suggested adjusting the alpha/opacity value of the colors used in the MaterialColor
. It's actually a big mistake because it will result in making your element translucent without providing color variety of different shades.
Please consider using this solution for a better approach.
Flutter: Creating a custom color swatch for MaterialColor
MaterialColor createMaterialColor(Color color) {
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);
}
You can create a Seprate class.
static const PrimaryColor = Color(0xFF808080);
static const PrimaryAssentColor = Color(0xFF808080);
static const PrimaryDarkColor = Color(0xFF808080);
static const ErroColor = Color(0xFF808080);
You shouldn't have [50]
on the end of here:
primarySwatch:Theme1.CompanyColors.black[50]
Just use the MaterialColor
object you created:
primarySwatch:Theme1.CompanyColors.black
I tested this by creating a MaterialColor
that's all red, and it worked fine: