use_colored_box
使用 ColoredBox
。
此規則自 Dart 2.17 起可用。
此規則有可用的快速修正。
詳細資訊
#當 Container
只有 Color
時,使用 ColoredBox
。
Container
是比 ColoredBox
更重的 Widget,而且 ColoredBox
有一個 const
建構子,這是一項額外的好處。
錯誤
dart
Widget buildArea() {
return Container(
color: Colors.blue,
child: const Text('hello'),
);
}
正確
dart
Widget buildArea() {
return const ColoredBox(
color: Colors.blue,
child: Text('hello'),
);
}
用法
#若要啟用 use_colored_box
規則,請在 analysis_options.yaml
檔案的 linter > rules 下新增 use_colored_box
analysis_options.yaml
yaml
linter:
rules:
- use_colored_box
除非另有說明,否則本網站上的文件反映的是 Dart 3.6.0。頁面最後更新於 2024-07-03。檢視原始碼或回報問題。