sized_box_shrink_expand
Use SizedBox shrink and expand named constructors.
此规则自 Dart 2.16 版本起可用。
详情
#Use SizedBox.shrink(...)
and SizedBox.expand(...)
constructors appropriately.
Either the SizedBox.shrink(...)
or SizedBox.expand(...)
constructor should be used instead of the more general SizedBox(...)
constructor when one of the named constructors capture the intent of the code more succinctly.
Examples
BAD:
dart
Widget buildLogo() {
return SizedBox(
height: 0,
width: 0,
child: const MyLogo(),
);
}
dart
Widget buildLogo() {
return SizedBox(
height: double.infinity,
width: double.infinity,
child: const MyLogo(),
);
}
GOOD:
dart
Widget buildLogo() {
return SizedBox.shrink(
child: const MyLogo(),
);
}
dart
Widget buildLogo() {
return SizedBox.expand(
child: const MyLogo(),
);
}
使用方法
#要启用 sized_box_shrink_expand
规则,请在你的 analysis_options.yaml
文件中,在 linter > rules 下添加 sized_box_shrink_expand
:
analysis_options.yaml
yaml
linter:
rules:
- sized_box_shrink_expand
除非另有说明,否则本网站上的文档反映的是 Dart 3.6.0。页面最后更新于 2025-02-05。 查看源代码 或 报告问题.