Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter BoxDecoration’s background color overrides the Container's background color, why?

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,   ), ); 
like image 226
Mary Avatar asked Aug 16 '17 23:08

Mary


People also ask

How do you change the background color of a container in Flutter?

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.

Can we use color and decoration property simultaneously in the container?

The color and decoration arguments cannot both be supplied, since it would potentially result in the decoration drawing over the background color.

How do you add a background to a container in Flutter?

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.


2 Answers

Container’s color is shorthand for BoxDecoration’s color, so BoxDecoration's color in the Container's decoration property overrides its Container's color.

like image 79
Mary Avatar answered Oct 16 '22 10:10

Mary


Problem:

You can't use color and decoration at the same time. From docs:

The color and decoration 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, use decoration: BoxDecoration(color: color).


Solutions:

  • Use only color:

    Container(   color: Colors.red ) 
  • Use only decoration and provide color here:

    Container(   decoration: BoxDecoration(color: Colors.red) ) 
like image 36
CopsOnRoad Avatar answered Oct 16 '22 11:10

CopsOnRoad