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
recommendedflutter

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

example.dartdart
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);
}
Use `SizedBox.shrink` to avoid needing to specify the `height` and `width`.
4Widget zeroInts() {
5 return SizedBox(height: 0, width: 0);
Use `SizedBox.shrink` to avoid needing to specify the `height` and `width`.
8Widget zeroDoublesWithChild(Widget logo) {
9 return SizedBox(
Use `SizedBox.expand` to avoid needing to specify the `height` and `width`.
16Widget infinite(Widget logo) {
17 return SizedBox(
Use `SizedBox.shrink` to avoid needing to specify the `height` and `width`.
24Widget withKey() {
25 return SizedBox(key: const Key('k'), width: 0x0, height: 0);
Use `SizedBox.expand` to avoid needing to specify the `height` and `width`.
28Widget prefixed() {
29 return w.SizedBox(width: double.infinity, height: double.infinity);
Use `SizedBox.shrink` to avoid needing to specify the `height` and `width`.
32Widget explicitNew() {
33 return new SizedBox(width: 0, height: 0);
Use `SizedBox.shrink` to avoid needing to specify the `height` and `width`.
36Widget constant() {
37 return const SizedBox(width: 0, height: 0);

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "sized-box-shrink-expand": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/sized-box-shrink-expand: <reason>
  • slash-for-doc-commentsslash_for_doc_commentsrecommended

    Flags /** ...

  • sort-child-properties-lastsort_child_properties_lastrecommendedflutter

    Sort Widget-valued child properties last, except before closure arguments.

  • sort-constructors-firstsort_constructors_first

    Flags a constructor declared after a non-constructor member.

  • sort-unnamed-constructors-firstsort_unnamed_constructors_first

    Flags an unnamed constructor declared after a named constructor.

  • tighten-type-of-initializing-formalstighten_type_of_initializing_formals

    Flags a nullable this.x or super.x initializing formal whose constructor asserts x != null.

  • type-annotate-public-apistype_annotate_public_apisrecommended

    Requires type annotations on public declarations.

  • type-init-formalstype_init_formalsrecommended

    Flags a *redundant* type annotation on an initializing formal or super parameter (type-init-formals, adopted from package:lints).

  • unawaited-futuresunawaited_futuresrecommended

    Future results in async bodies must be awaited or marked unawaited.