use_gap
Replaces a SizedBox or single-direction Padding used as a spacer inside a
multi-child widget (Column, Row, Wrap, Flex, ListView) with Gap from
the gap package. Gap picks its axis from the
parent, so flipping a Column to a Row cannot leave a height: behind.
many_lints: rules: use_gap: true prefer_spacing: false # the opposing rule; leave it offrules: use_gap: true prefer_spacing: false # the opposing rule; leave it offAdd the package too:
dart pub add gapfinal widgets = <Widget>[ Column( children: const [ Text('First'), SizedBox(height: 16), // LINT Text('Second'), ], ), Row( children: const [ Text('Left'), SizedBox(width: 8), // LINT Text('Right'), ], ),];final widgets = <Widget>[ Column( children: const [Text('First'), Gap(16), Text('Second')], ), Row( children: const [Text('Left'), Gap(8), Text('Right')], ),];Examples
Section titled “Examples”A SizedBox spacer in a Column
Section titled “A SizedBox spacer in a Column”// Don'tColumn( children: const [ Text('Total'), SizedBox(height: 12), // LINT Text('12.00 EUR'), ],);
// DoColumn( children: const [Text('Total'), Gap(12), Text('12.00 EUR')],);A single-direction Padding used as a spacer
Section titled “A single-direction Padding used as a spacer”Padding is reported only when its EdgeInsets.only sets exactly one direction
and that direction matches the parent’s axis:
// Don'tRow( children: const [ Text('Name'), Padding(padding: EdgeInsets.only(left: 8)), // LINT Text('Ada'), ],);
// DoRow( children: const [Text('Name'), Gap(8), Text('Ada')],);Skipping short lists
Section titled “Skipping short lists”min_children is the number of entries the children: list must have before a
spacer inside it is reported. With min_children: 3, a two-element list is left
alone:
many_lints: rules: use_gap: min_children: 3rules: use_gap: min_children: 3// Not reported: only 2 childrenColumn( children: const [Text('Header'), SizedBox(height: 8)],);
// Reported: 3 childrenColumn( children: const [ Text('Header'), SizedBox(height: 8), // LINT Text('Body'), ],);Cases the rule leaves alone
Section titled “Cases the rule leaves alone”A SizedBox that is not a pure spacer is never reported — it is doing real
layout work that Gap cannot express:
Column( children: [ // Has a child: it is a box, not a spacer. const SizedBox(height: 40, child: Text('Fixed row')), // Sets both dimensions. const SizedBox(width: 100, height: 100), // Wrong axis for a Column: a Gap here would change the layout. const SizedBox(width: 16), ],);Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
min_children |
int | 1 |
Minimum number of children in the list before spacers are reported |
Known limitations
Section titled “Known limitations”The Gap import is yours. The quick fix writes Gap(...); if package:gap
is not imported the result parses but does not compile.
Only direct children. A spacer wrapped in anything — a Visibility, a
conditional helper, a ...[] spread whose elements are built elsewhere — is not
seen as a child of the flex.
Wrap and Flex have no fixed axis, so a spacer inside either is reported
whichever dimension it sets.
Turning this rule off
Section titled “Turning this rule off”rules: use_gap: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_spacing— Opposing convention. Use the spacing argument on Row/Column instead of SizedBox spacers.avoid_expanded_as_spacer— Use Spacer instead of Expanded with an empty child.always_pass_global_key— Don’t create a GlobalKey inside build.avoid_conditional_hooks— Never call hooks inside conditionals, loops, or ternaries.