sized_box_shrink_expand
使用 SizedBox shrink 和 expand 命名构造函数。
详情
#适当地使用 SizedBox.shrink(...)
和 SizedBox.expand(...)
构造函数。
当命名构造函数能更简洁地表达代码意图时,应使用 SizedBox.shrink(...)
或 SizedBox.expand(...)
构造函数,而不是更通用的 SizedBox(...)
构造函数。
示例
错误示例
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(),
);
}
正确示例
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
如果您改为使用 YAML map 语法配置 Linter 规则,请在 linter > rules 下添加 sized_box_shrink_expand: true
analysis_options.yaml
yaml
linter:
rules:
sized_box_shrink_expand: true