Skip to content

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.

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

// Container supports padding, no need to wrap in Padding
Padding(
padding: EdgeInsets.all(16),
child: Container(child: Text('Hello')),
);
// Card supports padding
Padding(
padding: EdgeInsets.symmetric(horizontal: 12),
child: Card(child: Text('Card content')),
);
// Pass padding directly to Container
Container(padding: EdgeInsets.all(16), child: Text('Hello'));
// Padding wrapping a widget that doesn't support padding is fine
Padding(padding: EdgeInsets.all(8), child: Text('Hello'));
// Container already has its own padding set -- wrapping is acceptable
Padding(
padding: EdgeInsets.all(8),
child: Container(padding: EdgeInsets.all(4), child: Text('Hello')),
);

To disable this rule:

plugins:
many_lints:
diagnostics:
avoid_wrapping_in_padding: false