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 映射语法来配置 linter 规则,请在 linter > rules 下添加 sized_box_shrink_expand: true
analysis_options.yaml
yaml
linter:
rules:
sized_box_shrink_expand: true
除非另有说明,否则本网站上的文档反映的是 Dart 3.7.1 版本。页面上次更新于 2025-03-07。 查看源代码 或 报告问题。