prefer_center_over_align
v0.1.0 Warning Fix Widget Replacement
Flags Align widgets that use Alignment.center (or omit the alignment parameter, which defaults to center). In these cases, the Center widget is a clearer and more idiomatic replacement.
Why use this rule
Section titled “Why use this rule”Center is an Align — a subclass that hard-codes Alignment.center. Using
it drops an argument and makes the intent readable at a glance in a deep tree.
The quick fix renames the widget and deletes the redundant alignment:.
// A loading spinner in the middle of the page.Align( alignment: Alignment.center, child: const CircularProgressIndicator(),);Center(child: const CircularProgressIndicator());Examples
Section titled “Examples”An Align with no alignment at all
Section titled “An Align with no alignment at all”Align’s alignment defaults to Alignment.center, so omitting it is the same
widget written less clearly. It is reported too:
// Don'tAlign(child: const Text('Centred'));
// DoCenter(child: const Text('Centred'));Alignment written as coordinates
Section titled “Alignment written as coordinates”Alignment(0, 0) is the centre, so a literal spelled that way is reported as
well:
// Don'tAlign(alignment: const Alignment(0, 0), child: const Text('Centred'));
// DoCenter(child: const Text('Centred'));Only exact zeros count — Alignment(0.0, 0.1) is not centre and is not
reported.
Any other alignment is fine
Section titled “Any other alignment is fine”// Not reportedAlign(alignment: Alignment.bottomRight, child: const Text('Corner'));Known limitations
Section titled “Known limitations”widthFactor and heightFactor are carried, not dropped. Center declares
both, so an Align using them still converts cleanly:
// Don'tAlign( alignment: Alignment.center, widthFactor: 2, child: const Text('Wide'),);
// DoCenter(widthFactor: 2, child: const Text('Wide'));Only a literal is recognised. An alignment behind a variable or a getter is opaque to the rule, even when it holds the centre:
// Not reportedconst spot = Alignment.center;Align(alignment: spot, child: const Text('Centred'));Any X.center is matched, not just Alignment.center.
AlignmentDirectional.center is the same point, so it converts cleanly — but so
would a center constant on some unrelated class of your own, which would not.
That shape is rare enough that the rule does not guard against it; if you hit
it, silence the line with
// ignore: many_lints/prefer_center_over_align.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_center_over_align: trueTo turn it off again:
rules: prefer_center_over_align: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_sized_box_square— Use SizedBox.square when width and height are equal.prefer_text_rich— Use Text.rich instead of RichText for better accessibility.prefer_align_over_container— Use Align instead of Container when only alignment is set.avoid_border_all— Use Border.fromBorderSide instead of Border.all for const support.