带颜色优化的 Container
概述
#框架中新增了一个 ColoredBox
widget,并且 Container
widget 得到了优化,当用户指定 color
而非 decoration
时,它会使用 ColoredBox
。
背景
#以下是使用 Container
widget 的常见方式:
dart
return Container(color: Colors.red);
以前,此代码会生成一个 widget 层级,该层级使用 BoxDecoration
来实际绘制背景颜色。BoxDecoration
widget 涵盖了除绘制背景颜色之外的许多情况,并且不如新的 ColoredBox
widget 高效,后者仅绘制背景颜色。
以前,希望基于 widget 树中 container 颜色的 widget 测试,必须找到 BoxDecoration
才能实际获取 container 的颜色。现在,它们能够直接检查 Container
本身的 color
属性,除非 BoxDecoration
被明确地作为 decoration
属性提供。同时向 Container
提供 color
和 decoration
仍然是错误的。
迁移指南
#对 Container
颜色进行断言或期望其创建 BoxDecoration
的测试需要进行修改。
迁移前的代码
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);
});
迁移后的代码
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);
});
时间线
#引入版本: 1.15.4
稳定版本: 1.17
参考资料
#API 文档
相关问题
相关 PR