Skip to content

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.

Center is a specialized subclass of Align that always aligns to center. Using Center makes the intent immediately obvious and removes the redundant alignment: Alignment.center argument. If you omit alignment on Align, Flutter defaults to center anyway — so you should just use Center.

See also: Center | Align

// Align with explicit Alignment.center
Align(alignment: Alignment.center, child: Text('Hello'));
// Align without alignment defaults to center
Align(child: Text('World'));
// Use Center directly
Center(child: Text('Hello'));
Center(child: Text('World'));

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_center_over_align: true

To turn it off again:

many_lints.yaml
rules:
prefer_center_over_align: false

To keep the rule on but skip certain paths, use per-rule exclude.