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

FlutterDartFlutter LayoutBackground ColorFlutter Container

Flutter Problem Overview


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

Flutter Solutions


Solution 1 - Flutter

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

Solution 2 - Flutter

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

Solution 3 - Flutter

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

Solution 4 - Flutter

Flutter team says that color property in BoxDecoration() is quite frequently used in applying background color to Container widget. As such they have put a separate shorthand for color property in Container widget. So, when we use both color property and BoxDecoration() color property in same Container widget, an assertion will be thrown as follows:

Cannot provide both a color and a decoration
The color argument is just a shorthand for "decoration: new BoxDecoration(color: color)".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaryView Question on Stackoverflow
Solution 1 - FlutterMaryView Answer on Stackoverflow
Solution 2 - FlutterCopsOnRoadView Answer on Stackoverflow
Solution 3 - FlutterParesh MangukiyaView Answer on Stackoverflow
Solution 4 - FlutterPhan HeroView Answer on Stackoverflow