prefer_padding_over_container
v0.1.0 Warning Fix Widget Replacement
Flags Container widgets that only use the padding or margin parameter (plus optional key and child). When Container is used solely for spacing, the Padding widget is a lighter, more descriptive alternative.
Why use this rule
Section titled “Why use this rule”Container composes half a dozen widgets internally; when the only argument is
padding or margin, Padding does the same job in one render object. The
quick fix does the rewrite for you — it renames the constructor and, for a
margin, renames the argument too.
// A Container used purely to inset a label.Container(padding: EdgeInsets.all(16), child: Text('Hello'));Padding(padding: EdgeInsets.all(16), child: Text('Hello'));Examples
Section titled “Examples”margin: becomes padding:
Section titled “margin: becomes padding:”On a Container with nothing else set, margin and padding render
identically — the margin has no decoration or colour to sit outside of. So the
fix renames the argument as it swaps the widget:
// Don'tContainer(margin: EdgeInsets.all(16), child: Text('Hello'));
// DoPadding(padding: EdgeInsets.all(16), child: Text('Hello'));No child is still reported
Section titled “No child is still reported”Padding takes an optional child too, so a childless spacer converts the same
way:
// Don'tContainer(margin: EdgeInsets.symmetric(horizontal: 8));
// DoPadding(padding: EdgeInsets.symmetric(horizontal: 8));Known limitations
Section titled “Known limitations”padding and margin together is not reported. Those two are no longer
equivalent once both exist — the fix would have to merge them — so the rule
stays quiet:
// Not reportedContainer(padding: EdgeInsets.all(8), margin: EdgeInsets.all(8));Any other argument stops it. width, color, decoration, alignment —
one of them and Container is doing something Padding cannot:
// Not reportedContainer(padding: EdgeInsets.all(8), width: 100, child: Text('Hello'));key and child are the two exceptions; they do not count against the rule.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_padding_over_container: trueTo turn it off again:
rules: prefer_padding_over_container: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_container— Opposing convention. Replace sequences of nested widgets with a single Container.prefer_align_over_container— Use Align instead of Container when only alignment is set.prefer_constrained_box_over_container— Use ConstrainedBox instead of Container when only constraints is set.prefer_transform_over_container— Use Transform instead of Container when only transform is set.