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.
Why use this rule
Section titled “Why use this rule”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 literalSizedBox(width: 10, height: 10);
// Same double literalSizedBox(width: 24.0, height: 24.0);
// Same variable referenceconst size = 48.0;SizedBox(width: size, height: size);
// With a child widgetSizedBox(width: 50, height: 50, child: Text('Hello'));// Use SizedBox.squareSizedBox.square(dimension: 10);
// Different width and height is fineSizedBox(width: 100, height: 50);
// Only width specifiedSizedBox(width: 10);
// Only height specifiedSizedBox(height: 10);Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_sized_box_square: trueTo turn it off again:
rules: prefer_sized_box_square: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_center_over_align— Use Center instead of Align when alignment is center.prefer_text_rich— Use Text.rich instead of RichText for better accessibility.prefer_constrained_box_over_container— Use ConstrainedBox instead of Container when only constraints is set.avoid_border_all— Use Border.fromBorderSide instead of Border.all for const support.