prefer_align_over_container
v0.1.0 Warning Fix Widget Replacement
Flags Container widgets that only use the alignment parameter (plus optional key and child). When Container is used solely for alignment, the Align widget is a lighter, more descriptive alternative.
Why use this rule
Section titled “Why use this rule”A Container builds up to seven render objects depending on which arguments it
got. With only alignment set, exactly one of them does anything, and that one
is Align. Naming it directly says what the widget is for. The quick fix is a
rename — Container becomes Align and the arguments stay put.
// A badge pinned to the corner of a card.Container( alignment: Alignment.topRight, child: const Icon(Icons.star),);Align( alignment: Alignment.topRight, child: const Icon(Icons.star),);Examples
Section titled “Examples”Aligning inside a fixed-size parent
Section titled “Aligning inside a fixed-size parent”Align expands to fill its parent and places the child within it, exactly as
the Container did:
// Don'tSizedBox( height: 120, child: Container( alignment: Alignment.bottomCenter, child: const Text('Caption'), ),);
// DoSizedBox( height: 120, child: Align( alignment: Alignment.bottomCenter, child: const Text('Caption'), ),);key and child do not count
Section titled “key and child do not count”They exist on both widgets, so a Container carrying them plus alignment is
still reported:
// Don'tContainer( key: const ValueKey('badge'), alignment: Alignment.centerLeft, child: const Text('New'),);
// DoAlign( key: const ValueKey('badge'), alignment: Alignment.centerLeft, child: const Text('New'),);Centre alignment chains into another rule
Section titled “Centre alignment chains into another rule”Rewriting to Align(alignment: Alignment.center, …) is then reported by
prefer_center_over_align,
which wants Center. Go straight there:
// Don'tContainer(alignment: Alignment.center, child: const Text('Hi'));
// Do — skip the intermediate AlignCenter(child: const Text('Hi'));Known limitations
Section titled “Known limitations”Any other argument silences it. One padding, color, width or
decoration and the Container is doing work Align cannot:
// Not reported — the colour has nowhere to go on an AlignContainer( alignment: Alignment.topLeft, color: const Color(0xFFEEEEEE), child: const Text('Hello'),);Only a direct Container(...) is matched. A factory or helper that returns
a Container is not looked through.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_align_over_container: trueTo turn it off again:
rules: prefer_align_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_constrained_box_over_container— Use ConstrainedBox instead of Container when only constraints 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.