Why this mistake in decorating my container on the flutter?

Asked

Viewed 524 times

-1

Cannot provide Both a color and a Decoration The color argument is just a shorthand for "Decoration: new Boxdecoration(color: color)". 'package:flutter/src/widgets/container.Dart': Failed assertion: line 317 pos 15: 'color == null || Decoration == null'

  • You could post the code so we can analyze it better ?

2 answers

1

This error means you cannot use the property color along with the decoration.color...

Examples

Paint a Container

Container(
  color: Colors.red,
  width:100,
  height: 100
)

Create a Container with rounded edges

Container(
  decoration: BoxDecoration(
    borderRadius: new BorderRadius.only(
        topLeft: const Radius.circular(40.0),
        topRight: const Radius.circular(40.0)
    )
)

Explanation

The Container owned the property Color to make things easier for us, so we don’t need to create a BoxDecoration just to change the color...

But when we need to change more things in the Container we need to create the BoxDecoration and then Flutter asks us to use the property color of BoxDecoration, because otherwise the Widget "would be confused" on where to pick up your color.

So whenever you need to, for example, create a Container blue with rounded edges make:

Container(
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: new BorderRadius.only(
        topLeft: const Radius.circular(40.0),
        topRight: const Radius.circular(40.0)
    )
)

0

Container has a color validation. If you are using a decoration cannot use the attribute color out of it. In this case the color must be moved into the decoration attribute.

Example

Container(
  color: Colors.amber,
);

or

Container(
  decoration: BoxDecoration(
    color: Colors.amber
  )
);

The above syntax works normally, the syntax below will cause error because there is decoration and the color is out of it.

Container(
  decoration: BoxDecoration(),
  color: Colors.amber
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.