When you try to add a background color, you get an error message that says you can't have a color and a box decoration. Anyone know how to fix this?
In Flutter, How do you add a background color to a container while also adding borders
41 Views Asked by John Brown AtThere are 4 best solutions below
On
The error message arises because you cannot directly specify both a color property and a decoration property with a color field in the same widget in Flutter. This is because both properties attempt to fill the background of the widget, leading to ambiguity and potential inconsistencies.
Here's how to fix the issue:
Use decoration with color:
If you want to use a solid color background, utilize the BoxDecoration widget within the decoration property:
Container(
decoration: BoxDecoration(
color: Colors.blue, // Set the desired background color
),
)
On
You can can not use color outside of decoration property if you are using it inside your container. The error says:
Cannot provide both a color and a decoration To provide both, use "decoration: BoxDecoration(color: color)". 'package:flutter/src/widgets/container.dart': Failed assertion: line 269 pos 15: 'color == null || decoration == null'
If you use decoration property then don't define color outside of decoration otherwise you will get an error.
To use border color, you can write like this:
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.red,
width: 5,
),
),
Here is link for more detail of container: https://api.flutter.dev/flutter/widgets/Container-class.html


Because you don't need to add color separately as decoration has the same property to achieve the same. Also it has the Border inside the same decoration prop using the
BoxDecorationclass.Check this thread Flutter BoxDecoration’s background color overrides the Container's background color, why?