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.
Why use this rule
Section titled “Why use this rule”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 parameterContainer( constraints: BoxConstraints(maxWidth: 200), child: Text('Hello'),);
// Container with only constraints, no childContainer(constraints: BoxConstraints.tightFor(width: 100));// Use ConstrainedBox directlyConstrainedBox( constraints: BoxConstraints(maxWidth: 200), child: Text('Hello'),);
// Container with additional properties besides constraints is fineContainer( constraints: BoxConstraints(maxWidth: 200), padding: EdgeInsets.all(8), 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: prefer_constrained_box_over_container: trueTo turn it off again:
rules: prefer_constrained_box_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_padding_over_container— Use Padding instead of Container when only padding or margin is set.prefer_transform_over_container— Use Transform instead of Container when only transform is set.