Skip to content

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.

Container is a convenience widget that composes many lower-level widgets internally. When you only need padding or margin, using Padding directly avoids the overhead and makes the intent immediately clear. This also keeps the widget tree shallow and easier to read during debugging.

See also: Padding | Container

// Container with only margin parameter
Container(margin: EdgeInsets.all(16), child: Text('Hello'));
// Container with only margin, no child
Container(margin: EdgeInsets.symmetric(horizontal: 8));
// Container with only padding parameter
Container(padding: EdgeInsets.all(16), child: Text('Hello'));
// Container with only padding, no child
Container(padding: EdgeInsets.symmetric(vertical: 8));
// Use Padding directly
Padding(padding: EdgeInsets.all(16), child: Text('Hello'));
Padding(padding: EdgeInsets.symmetric(horizontal: 8));
Padding(padding: EdgeInsets.all(16), child: Text('Hello'));
Padding(padding: EdgeInsets.symmetric(vertical: 8));

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_padding_over_container: false