avoid_expanded_as_spacer
v0.4.0 Warning Fix Widget Replacement
Flags Expanded widgets that wrap an empty SizedBox or Container as their child. This pattern is equivalent to using the Spacer widget, which is purpose-built for this exact use case.
Why use this rule
Section titled “Why use this rule”Flutter provides the Spacer widget specifically for creating flexible space in Row, Column, and Flex layouts. Using Expanded with an empty child obscures the intent and adds an unnecessary widget to the tree. Spacer is clearer, more concise, and immediately communicates that the purpose is to fill available space.
// Expanded with empty SizedBoxconst Expanded(child: SizedBox());
// Expanded with empty ContainerExpanded(child: Container());
// Expanded with flex and empty SizedBoxconst Expanded(flex: 2, child: SizedBox());// Use Spacer directlyconst Spacer();
// Use Spacer with flex parameterconst Spacer(flex: 2);
// Expanded with a non-empty child is fineconst Expanded(child: Text('content'));Configuration
Section titled “Configuration”To disable this rule:
plugins: many_lints: diagnostics: avoid_expanded_as_spacer: false