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”With only constraints set, the single render object a Container produces is
a ConstrainedBox. Writing it directly is one widget instead of a composition,
and it reads as what it is. The quick fix renames the constructor; the
constraints: argument moves across unchanged.
See also: ConstrainedBox | BoxConstraints
// Cap how wide a label is allowed to grow.Container( constraints: const BoxConstraints(maxWidth: 200), child: const Text('A long product name that should wrap'),);ConstrainedBox( constraints: const BoxConstraints(maxWidth: 200), child: const Text('A long product name that should wrap'),);Examples
Section titled “Examples”A minimum tap target
Section titled “A minimum tap target”// Don'tContainer( constraints: const BoxConstraints(minWidth: 48, minHeight: 48), child: const Icon(Icons.close),);
// DoConstrainedBox( constraints: const BoxConstraints(minWidth: 48, minHeight: 48), child: const Icon(Icons.close),);No child is still reported
Section titled “No child is still reported”// Don'tContainer(constraints: BoxConstraints.tightFor(width: 100));
// DoConstrainedBox(constraints: BoxConstraints.tightFor(width: 100));Consider SizedBox for a tight constraint
Section titled “Consider SizedBox for a tight constraint”If the constraint is tight in both axes, SizedBox says it in fewer words:
// Reported, and correct as far as this rule goesConstrainedBox( constraints: const BoxConstraints.tightFor(width: 100, height: 100), child: child,);
// Clearer still — and then// `prefer_sized_box_square` will suggest SizedBox.square(dimension: 100)SizedBox(width: 100, height: 100, child: child);Known limitations
Section titled “Known limitations”width:/height: on a Container are not constraints:. They are a
separate pair of arguments, so this rule does not see them:
// Not reported by this ruleContainer(width: 100, height: 100, child: child);Any other argument silences it. padding, color, alignment,
decoration — one of them and the Container is doing more than constraining:
// Not reportedContainer( constraints: const BoxConstraints(maxWidth: 200), padding: const EdgeInsets.all(8), child: const Text('Hello'),);key and child are the exceptions; neither counts against the rule.
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.