跳到主要內容

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