Container with color optimization
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:
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:
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:
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
In stable release: 1.17
References
#API documentation:
Relevant issues:
Relevant PR:
Unless stated otherwise, the documentation on this site reflects the latest stable version of Flutter. Page last updated on 2024-04-04. View source or report an issue.