sized_box_shrink_expand
Use SizedBox shrink and expand named constructors.
This rule is available as of Dart 2.16.
Details
#Use SizedBox.shrink(...)
and SizedBox.expand(...)
constructors
appropriately.
Either the SizedBox.shrink(...)
or SizedBox.expand(...)
constructor should
be used instead of the more general SizedBox(...)
constructor when one of the
named constructors capture the intent of the code more succinctly.
Examples
BAD:
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(),
);
}
GOOD:
dart
Widget buildLogo() {
return SizedBox.shrink(
child: const MyLogo(),
);
}
dart
Widget buildLogo() {
return SizedBox.expand(
child: const MyLogo(),
);
}
Usage
#To enable the sized_box_shrink_expand
rule,
add sized_box_shrink_expand
under linter > rules in your
analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- sized_box_shrink_expand
除非另有说明,文档之所提及适用于 Dart 3.5.4 版本,本页面最后更新时间: 2024-08-02。 查看文档源码 或者 报告页面问题。