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

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.


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


Problem:

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

The source code clearly mentions that either of color or decoration should be null.

assert(color == null || decoration == null,'Cannot provide both a color and a decoration\n'    'To provide both, 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))