sized-box-shrink-expand
Same as sized_box_shrink_expand in Dart lints
- Group
- Style
- Path
lint/style/sized-box-shrink-expand- Default
- On (recommended)
- Origin
- Dart lints
- Domain
- flutter
- Rule
- 434 of 512
Prefers SizedBox.shrink() / SizedBox.expand() over a SizedBox sized to zero or infinity.
The unnamed SizedBox constructor with width: 0, height: 0 is exactly
SizedBox.shrink, and with width: double.infinity, height: double.infinity
it is exactly SizedBox.expand; the named constructors state the intent
directly. Only invocations that resolve to Flutter's SizedBox and pass no
positional arguments are considered; other named arguments (child, key)
carry over unchanged.
SizedBox(width: 0, height: 0, child: logo); // BAD
SizedBox.shrink(child: logo); // GOOD
Invalid
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart' as w;
Widget zeroInts() {
return SizedBox(height: 0, width: 0);
}
Widget zeroDoublesWithChild(Widget logo) {
return SizedBox(
width: 0.0,
height: 0.0,
child: logo,
);
}
Widget infinite(Widget logo) {
return SizedBox(
height: double.infinity,
width: double.infinity,
child: logo,
);
}
Widget withKey() {
return SizedBox(key: const Key('k'), width: 0x0, height: 0);
}
Widget prefixed() {
return w.SizedBox(width: double.infinity, height: double.infinity);
}
Widget explicitNew() {
return new SizedBox(width: 0, height: 0);
}
Widget constant() {
return const SizedBox(width: 0, height: 0);
}Widget zeroInts() { return SizedBox(height: 0, width: 0);Widget zeroDoublesWithChild(Widget logo) { return SizedBox(Widget infinite(Widget logo) { return SizedBox(Widget withKey() { return SizedBox(key: const Key('k'), width: 0x0, height: 0);Widget prefixed() { return w.SizedBox(width: double.infinity, height: double.infinity);Widget explicitNew() { return new SizedBox(width: 0, height: 0);Widget constant() { return const SizedBox(width: 0, height: 0);Valid
import 'package:flutter/material.dart';
Widget alreadyShrink(Widget logo) {
return SizedBox.shrink(child: logo);
}
Widget alreadyExpand(Widget logo) {
return SizedBox.expand(child: logo);
}
Widget onlyWidth() {
return const SizedBox(width: 0);
}
Widget mixedDimensions() {
return const SizedBox(width: 0, height: double.infinity);
}
Widget nonZero() {
return const SizedBox(width: 8, height: 8);
}
Widget computedDimensions(double size) {
return SizedBox(width: size, height: size);
}
Widget negativeZeroIsNotALiteral() {
return const SizedBox(width: -0, height: -0);
}
Widget fromSize() {
return SizedBox.fromSize(size: Size.zero);
}Configure sized-box-shrink-expand
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"sized-box-shrink-expand": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/sized-box-shrink-expand: <reason>More style rules
- slash-for-doc-comments
slash_for_doc_commentsrecommendedFlags
/** ... - sort-child-properties-last
sort_child_properties_lastrecommendedflutterSort Widget-valued child properties last, except before closure arguments.
- sort-constructors-first
sort_constructors_firstFlags a constructor declared after a non-constructor member.
- sort-unnamed-constructors-first
sort_unnamed_constructors_firstFlags an unnamed constructor declared after a named constructor.
- tighten-type-of-initializing-formals
tighten_type_of_initializing_formalsFlags a nullable
this.xorsuper.xinitializing formal whose constructor assertsx != null. - type-annotate-public-apis
type_annotate_public_apisrecommendedRequires type annotations on public declarations.
- type-init-formals
type_init_formalsrecommendedFlags a *redundant* type annotation on an initializing formal or super parameter (
type-init-formals, adopted from package:lints). - unawaited-futures
unawaited_futuresrecommendedFutureresults inasyncbodies must be awaited or markedunawaited.