Skip to content

prefer_constrained_box_over_container

v0.4.0 Warning Fix Widget Replacement

Flags Container widgets that only use the constraints parameter (plus optional key and child). When Container is used solely for constraints, the ConstrainedBox widget is a lighter, more descriptive alternative.

Container is a convenience widget that composes many lower-level widgets internally. When you only need constraints, using ConstrainedBox directly avoids the overhead and communicates your intent more clearly. It also keeps the widget tree shallow and easier to reason about.

See also: ConstrainedBox | Container

// Container with only constraints parameter
Container(
constraints: BoxConstraints(maxWidth: 200),
child: Text('Hello'),
);
// Container with only constraints, no child
Container(constraints: BoxConstraints.tightFor(width: 100));
// Use ConstrainedBox directly
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 200),
child: Text('Hello'),
);
// Container with additional properties besides constraints is fine
Container(
constraints: BoxConstraints(maxWidth: 200),
padding: EdgeInsets.all(8),
child: Text('Hello'),
);

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_constrained_box_over_container: false