Rules / Correctness / proper-expanded-and-flexible

proper-expanded-and-flexible

lint/correctness/proper-expanded-and-flexible
recommendedflutter
Same as proper_expanded_and_flexible · Pyramid Lint

Require Expanded and Flexible to sit directly inside a flex widget.

Flags an Expanded or Flexible passed as the child: of a widget that is not a Row, Column, or Flex. These widgets report their flex factor to a surrounding flex container through the render tree; under any other parent they have no flex parent to negotiate with and throw a layout error at runtime. Put them in the children: of a Row, Column, or Flex, or remove the wrapper. The rule reports only when the illegal parent is directly visible at the construction site.

Invalid

example.dartdart
import 'package:flutter/material.dart';

Widget a() => Container(child: Expanded(child: Text('a')));

Widget b() => Center(child: Flexible(child: Text('b')));

Widget c() => SizedBox(child: Expanded(child: SizedBox()));

Widget d() => Padding(
      padding: EdgeInsets.zero,
      child: Expanded(child: Text('d')),
    );

Widget e() => const Align(child: Flexible(child: Text('e')));
Expanded and Flexible must be direct children of a Row, Column, or Flex.
2
3Widget a() => Container(child: Expanded(child: Text('a')));
Expanded and Flexible must be direct children of a Row, Column, or Flex.
4
5Widget b() => Center(child: Flexible(child: Text('b')));
Expanded and Flexible must be direct children of a Row, Column, or Flex.
6
7Widget c() => SizedBox(child: Expanded(child: SizedBox()));
Expanded and Flexible must be direct children of a Row, Column, or Flex.
10 padding: EdgeInsets.zero,
11 child: Expanded(child: Text('d')),
Expanded and Flexible must be direct children of a Row, Column, or Flex.
13
14Widget e() => const Align(child: Flexible(child: Text('e')));

Valid

example.dartdart
import 'package:flutter/material.dart';

Widget a() => Row(children: [Expanded(child: Text('a'))]);

Widget b() => Column(children: [Flexible(child: Text('b'))]);

Widget c() =>
    Flex(direction: Axis.horizontal, children: [Expanded(child: Text('c'))]);

Widget d() => Container(child: Text('d'));

Widget e() => Center(child: SizedBox(height: 8));

Widget f() => Row(children: [const Expanded(child: Text('f'))]);

How to configure

Set the severity of proper-expanded-and-flexible in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "proper-expanded-and-flexible": "error"
      }
    }
  }
}