Skip to content

prefer_sized_box_square

v0.4.0 Warning Fix Widget Replacement

Flags SizedBox constructors where width and height are set to the same value. Flutter provides SizedBox.square(dimension: ...) as a cleaner way to express this intent.

When width and height are identical, SizedBox.square communicates “this is a square” at a glance, whereas SizedBox(width: 50, height: 50) requires the reader to compare both values. The named constructor eliminates duplication and makes the code more self-documenting.

See also: SizedBox.square | Dart lint: sized_box_shrink_expand

// Both width and height are the same literal
SizedBox(width: 10, height: 10);
// Same double literal
SizedBox(width: 24.0, height: 24.0);
// Same variable reference
const size = 48.0;
SizedBox(width: size, height: size);
// With a child widget
SizedBox(width: 50, height: 50, child: Text('Hello'));
// Use SizedBox.square
SizedBox.square(dimension: 10);
// Different width and height is fine
SizedBox(width: 100, height: 50);
// Only width specified
SizedBox(width: 10);
// Only height specified
SizedBox(height: 10);

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

many_lints.yaml
rules:
prefer_sized_box_square: true

To turn it off again:

many_lints.yaml
rules:
prefer_sized_box_square: false

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