跳到主要內容

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