# Container with color optimization

> A container with a color and no other background decoration no longer builds the same child widgets.




:::important
These breaking change docs are accurate, as of the release
under which they are published. Over time, the
workarounds described here might become inaccurate.
We don't, in general, keep these breaking change docs up
to date as of each release.

这些破坏性改动文档的准确性仅限于其发布时对应的版本。
随着时间的推移，文档中描述的方案可能会逐渐失效。
通常情况下，我们不会在每次发布新版本时都对这些文档进行同步更新。

The [breaking change index file](/release/breaking-changes)
lists the docs created for each release.

[破坏性改动列表](/release/breaking-changes) 
列出了每个版本的文档。

:::


## Summary

A new `ColoredBox` widget has been added to the framework,
and the `Container` widget has been optimized to use it
if a user specifies a `color` instead of a `decoration`.

## Context

It is very common to use the `Container` widget as follows:

```dart
return Container(color: Colors.red);
```

Previously, this code resulted in a widget hierarchy that used a
`BoxDecoration` to actually paint the background color.
The `BoxDecoration` widget covers many cases other than
just painting a background color,
and is not as efficient as the new `ColoredBox` widget,
which only paints a background color.

Widget tests that wanted to assert based on the color
of a container in the widget tree would previously have
to find the `BoxDecoration` to actually get
the color of the container.
Now, they are able to check the `color` property
on the `Container` itself, unless a `BoxDecoration`
was explicitly provided as the `decoration` property.
It is still an error to supply both `color` and
`decoration` to `Container`.

## Migration guide

Tests that assert on the color of a `Container`
or that expected it to create a
`BoxDecoration` need to be modified.

Code before migration:

```dart
testWidgets('Container color', (WidgetTester tester) async {
  await tester.pumpWidget(Container(color: Colors.red));

  final Container container = tester.widgetList<Container>().first;
  expect(container.decoration.color, Colors.red);
  // Or, a test may have specifically looked for the BoxDecoration, e.g.:
  expect(find.byType(BoxDecoration), findsOneWidget);
});
```

Code after migration:

```dart
testWidgets('Container color', (WidgetTester tester) async {
  await tester.pumpWidget(Container(color: Colors.red));

  final Container container = tester.widgetList<Container>().first;
  expect(container.color, Colors.red);
  // If your test needed to work directly with the BoxDecoration, it should
  // instead look for the ColoredBox, e.g.:
  expect(find.byType(BoxDecoration), findsNothing);
  expect(find.byType(ColoredBox), findsOneWidget);
});
```

## Timeline

Landed in version: 1.15.4<br>
In stable release: 1.17

## References

API documentation:

* [`Container`][]
* [`ColoredBox`][]
* [`BoxDecoration`][]

Relevant issues:

* [Issue 9672][]
* [Issue 28753][]

Relevant PR:

* [Colored box and container optimization #50979][]

[`Container`]: https://api.flutter-io.cn/flutter/widgets/Container-class.html
[`ColoredBox`]: https://api.flutter-io.cn/flutter/widgets/ColoredBox-class.html
[`BoxDecoration`]: https://api.flutter-io.cn/flutter/painting/BoxDecoration-class.html
[Issue 9672]: https://github.com/flutter/flutter/issues/9672
[Issue 28753]: https://github.com/flutter/flutter/issues/28753
[Colored box and container optimization #50979]: https://github.com/flutter/flutter/pull/50979

