跳到主要内容

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