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.


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)
    )
    

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 color, you can use the below code.

decoration: BoxDecoration(color: Colors.red).