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

// 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);

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_sized_box_square: false