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
除非另有說明,否則本網站上的文件反映的是 Dart 3.6.0。頁面最後更新時間為 2024-07-03。 檢視原始碼 或回報問題。