Flutter BoxDecoration’s background color overrides the Container's background color, why?
Container’s color
is shorthand for BoxDecoration’s color
, so BoxDecoration's color
in the Container's decoration
property overrides its Container's color
.
From docs:
The
color
anddecoration
arguments cannot both be supplied, since it would potentially result in the decoration drawing over the background color. To supply a decoration with a color, usedecoration: BoxDecoration(color: color)
.
And if you are only using Container
to provide color, I'd suggest you using ColoredBox
which is more optimised than a regular Container
.
ColoredBox(
color: Colors.red, // color
child: ...,
)
BoxDecoration
is used to actually paint the background color. TheBoxDecoration
widget covers many cases other than just painting a background color, and is not as efficient as the newColoredBox
widget, which only paints a background color.