avoid_wrapping_in_padding
v0.4.0 Warning Fix Widget Replacement
Flags Padding widgets whose child already supports a padding parameter (such as Container, Card, ListView, etc.). Instead of wrapping in Padding, you should pass padding directly to the child widget.
Why use this rule
Section titled “Why use this rule”Many Flutter widgets accept a padding parameter in their constructor. Wrapping them in a Padding widget adds an unnecessary layer to the widget tree when the same effect can be achieved by passing padding directly to the child. This keeps the tree flatter, reduces nesting, and makes the code easier to read.
See also: Padding | Container | Dart lint: avoid_unnecessary_containers
// Container supports padding, no need to wrap in PaddingPadding( padding: EdgeInsets.all(16), child: Container(child: Text('Hello')),);
// Card supports paddingPadding( padding: EdgeInsets.symmetric(horizontal: 12), child: Card(child: Text('Card content')),);// Pass padding directly to ContainerContainer(padding: EdgeInsets.all(16), child: Text('Hello'));
// Padding wrapping a widget that doesn't support padding is finePadding(padding: EdgeInsets.all(8), child: Text('Hello'));
// Container already has its own padding set -- wrapping is acceptablePadding( padding: EdgeInsets.all(8), child: Container(padding: EdgeInsets.all(4), child: Text('Hello')),);Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: avoid_wrapping_in_padding: trueTo turn it off again:
rules: avoid_wrapping_in_padding: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_padding_over_container— Use Padding instead of Container when only padding or margin is set.avoid_border_all— Use Border.fromBorderSide instead of Border.all for const support.avoid_expanded_as_spacer— Use Spacer instead of Expanded with an empty child.avoid_incorrect_image_opacity— Use Image’s opacity parameter instead of wrapping in Opacity.