I have a Flutter Container widget and I defined a color for it (pink), but for some reason, the color in BoxDecoration overrides it (green). Why?
new Container( color: Colors.pink, decoration: new BoxDecoration( borderRadius: new BorderRadius.circular(16.0), color: Colors.green, ), );
To set background color for Container widget, set its color property with the required Color value or set the decoration property with required background color value in it.
The color and decoration arguments cannot both be supplied, since it would potentially result in the decoration drawing over the background color.
Steps to set the background image:Step 1: Add the Container widget. Step 2: Add the decoration parameter (inside Container) and assign the BoxDecoration class. Step 3: Add the image parameter (inside BoxDecoration) and assign the DecorationImage class.
Container’s color is shorthand for BoxDecoration’s color, so BoxDecoration's color in the Container's decoration property overrides its Container's color.
You can't use color and decoration at the same time. From docs:
The
coloranddecorationarguments 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).
Use only color:
Container( color: Colors.red ) Use only decoration and provide color here:
Container( decoration: BoxDecoration(color: Colors.red) ) If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With