proper-expanded-and-flexible

Same as proper_expanded_and_flexible in Pyramid Lint

Path
lint/correctness/proper-expanded-and-flexible
Default
On (recommended)
Origin
Pyramid Lint
Domain
flutter
Rule
423 of 512
recommendedflutter

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'))]);

Configure proper-expanded-and-flexible

Set its severity in falcon.json. See the configuration guide.

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

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/proper-expanded-and-flexible: <reason>
  • proper-from-environmentproper_from_environmentrecommended

    Require fromEnvironment to be evaluated in a const context.

  • proper-super-init-stateproper_super_init_staterecommendedflutter

    Require super.initState() to be the first statement of a resolved Flutter State subclass's initState override.

  • require-super-disposerecommendedflutter

    Require super.dispose() in Flutter State dispose overrides.

  • sort-pub-dependenciessort_pub_dependenciescross-file

    Require pubspec.yaml dependency sections to be sorted alphabetically.

  • test-types-in-equalstest_types_in_equalsrecommended

    Flags casting the parameter of operator == instead of testing its type.

  • throw-in-finallythrow_in_finallyrecommended

    Flags throw inside a finally block.

  • type-literal-in-constant-patterntype_literal_in_constant_patternrecommended

    Don't use constant patterns whose expressions have the core Type type.

  • undeclared-dependenciescross-file

    Report package imports and exports missing from the enclosing pubspec.