Skip to content

gap_boxes_instead_of_spacing

Reports a Row, Column or Flex whose children are kept apart by empty boxes of one size. Use the flex’s spacing instead.

Tier
advisory
Scope
Flutter
Lowest preset
strict
Fix
deletes the boxes and gives the flex their size as spacing

A Flutter Row, Column or Flex whose children are kept apart by empty boxes of one size. The violation is on the flex’s constructor name.

  • Every second child is a gap box and no other is: one box between each two children, none at the start or end, none beside another.
  • A gap box is Flutter’s own SizedBox, made with its unnamed constructor and given only its size along the main axis (height in a column, width in a row) as a constant. All boxes have the same size.

It reports only where the two lay out the same:

  • the flex is Flutter’s own Row, Column or Flex, not a subclass, and its constructor has a spacing parameter. This is read from the element, so a Flutter older than spacing is never told to use it.
  • no spacing is given;
  • mainAxisAlignment is absent, start, center or end. The space… alignments share the free space among all children, boxes included.
  • a Flex has its direction written as an Axis value;
  • children is a list of expressions without if, for or spreads.

A single gap between two children is reported as well. The fix writes the spacing as the first box writes its size, and is not offered where a comment stands at a box.

The flex’s spacing puts the gap between each two children. Boxes say it once per gap, and they drift: a child added without its box, or one gap changed but not the others.

Violating
Column(children: [title, SizedBox(height: 8), body, SizedBox(height: 8), footer])
Corrected
Column(spacing: 8, children: [title, body, footer])

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: gap_boxes_instead_of_spacing

// ignore_for_file: assay/gap_boxes_instead_of_spacing silences it in a whole file, and // ignore: assay/gap_boxes_instead_of_spacing with the prefix works too. See Suppressions.