use_decorated_box
Use DecoratedBox
.
此规则自 Dart 2.16 版本起可用。
此规则提供 快速修复 。
详情
#DO use DecoratedBox
when Container
has only a Decoration
.
A Container
is a heavier Widget than a DecoratedBox
, and as bonus, DecoratedBox
has a const
constructor.
BAD:
dart
Widget buildArea() {
return Container(
decoration: const BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
child: const Text('...'),
);
}
GOOD:
dart
Widget buildArea() {
return const DecoratedBox(
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
child: Text('...'),
);
}
使用方法
#要启用 use_decorated_box
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 use_decorated_box
:
analysis_options.yaml
yaml
linter:
rules:
- use_decorated_box
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.