Skip to content

flexible_outside_flex

Reports a Flexible or Expanded whose parent is a Flutter widget that is not a Row, Column or Flex. Make it a child of a Row, Column or Flex, or remove it and keep its child.

Tier
blocking
Scope
Flutter
Lowest preset
recommended
Fix
none

A Flexible, an Expanded or a widget of a class that extends Flexible, written as the child of a Flutter framework widget that lays out its children itself and is not a Flex. The violation is on the name of the Flexible’s constructor.

Such parents are:

  • Padding, Center, Align, SizedBox, Opacity, Stack, Wrap and every other render object widget of Flutter;
  • the scrollables ListView, GridView, CustomScrollView and SingleChildScrollView.

The parent is the widget whose child argument the Flexible is, or among whose children it is written, also through an if or for element or a conditional expression. A Row, a Column and a Flex are its right parents and are never reported.

The rule is silent in a package that does not use Flutter.

A Flexible hands its child to the Flex it is a child of. Anywhere else, Flutter throws “Incorrect use of ParentDataWidget” when the widget is laid out, and a release build shows a grey box.

The rule reports only where the parent is certain.

Violating
Padding(
padding: const EdgeInsets.all(8),
child: Expanded(child: Text(title)),
)
Corrected
Row(
children: [
Expanded(child: Text(title)),
],
)

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

Write why on the line directly above the suppression:

// Why this place is fine.
// ignore: flexible_outside_flex

// ignore_for_file: assay/flexible_outside_flex silences it in a whole file, and // ignore: assay/flexible_outside_flex with the prefix works too. See Suppressions.