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
What it reports
Section titled “What it reports”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 (heightin a column,widthin 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,ColumnorFlex, not a subclass, and its constructor has aspacingparameter. This is read from the element, so a Flutter older thanspacingis never told to use it. - no
spacingis given; mainAxisAlignmentis absent,start,centerorend. Thespace…alignments share the free space among all children, boxes included.- a
Flexhas itsdirectionwritten as anAxisvalue; childrenis a list of expressions withoutif,foror 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.
Example
Section titled “Example”Column(children: [title, SizedBox(height: 8), body, SizedBox(height: 8), footer])Column(spacing: 8, children: [title, body, footer])Rule parameters
Section titled “Rule parameters”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. |
Suppressing it
Section titled “Suppressing it”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.