目錄

sized_box_shrink_expand

使用 SizedBox 的 shrink 和 expand 命名建構子。

此規則自 Dart 2.16 起可用。

詳細資訊

#

適當地使用 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