sized_ box_ shrink_ expand
Learn about the sized_box_shrink_expand linter rule.
Use SizedBox shrink and expand named constructors.
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:
Widget buildLogo() {
return SizedBox(
height: 0,
width: 0,
child: const MyLogo(),
);
}
Widget buildLogo() {
return SizedBox(
height: double.infinity,
width: double.infinity,
child: const MyLogo(),
);
}
GOOD:
Widget buildLogo() {
return SizedBox.shrink(
child: const MyLogo(),
);
}
Widget buildLogo() {
return SizedBox.expand(
child: const MyLogo(),
);
}
Enable
#
To enable the sized_box_shrink_expand rule, add
sized_box_shrink_expand under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- sized_box_shrink_expand
If you're instead using the YAML map syntax to configure
linter rules, add
sized_box_shrink_expand: true under
linter > rules:
linter:
rules:
sized_box_shrink_expand: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.