How to put opacity for container in flutter

Flutter uses hexadecimal ARGB values for colors, which are formatted as const Color(0xAARRGGBB). That first pair of letters, the AA, represent the alpha channel. You must convert your decimal opacity values to a hexadecimal value.

Hex Opacity 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

For instance:

static const Color mainColor = Color(0xff0097A7);

to:

static  Color accentColor = Color(0x1A0097A7);

will change the opacity to 10%.


Flutter uses a 32 bit color value in ARGB format, where A = Alpha, R = RED, G = GREEN and B = BLUE.

So to control the opacity you can change the values of first two digits of the hex value in const Color(0xFF0E3311), you can use values ranging from 0x000E3311,0x010E3311....0xFF0E3311.

Hope that helps!


Change the line

const Color(0xFF0E3311)

to

const Color(0xFF0E3311).withOpacity(0.5)

or any value you want.


If you just want to set an opacity to your color it's simple as adding 2 hex numbers before your color code. Check this answer to know all the values.

But if you want to change the opacity of all the widget, in your case a Container, you can wrap it into a Opacity widget like this:

double _opacityValue = 0.50;//This value goes from 0.0 to 1.0. In this case the opacity is from 50%

final Widget _bodyWithOpacity = Opacity(
  opacity: _opacityValue,
  child: body,
);

Check here the Opacity's documentation and this quick video if you want to know more about it!