prefer-constrained-box-over-container

Group
Style
Path
lint/style/prefer-constrained-box-over-container
Default
Off, opt in
Origin
Falcon
Domain
flutter
Rule
331 of 512
flutter

Prefers ConstrainedBox over a Container used only for constraints.

The unnamed Flutter constructor must have a provably non-null child and a provably non-null constraints argument, with only an optional key. Other arguments, nullable or unresolved values, and shadowed constructors are ignored. The dedicated widget makes the layout intent explicit. Contexts explicitly requiring Container are excluded.

Invalid

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

typedef LayoutContainer = Container;

void examples(Widget child, BoxConstraints value) {
  Container(constraints: value, child: child);
  new Container(constraints: value, child: child);
  ui.Container(child: child, constraints: value);
  new ui.Container(key: const Key("box"), constraints: value, child: child);
  LayoutContainer(constraints: value, child: child);
  Container(key: const Key("ordered"), child: child, constraints: (value));
}
Prefer `ConstrainedBox` over a `Container` used only for constraints.
6void examples(Widget child, BoxConstraints value) {
7 Container(constraints: value, child: child);
Prefer `ConstrainedBox` over a `Container` used only for constraints.
7 Container(constraints: value, child: child);
8 new Container(constraints: value, child: child);
Prefer `ConstrainedBox` over a `Container` used only for constraints.
8 new Container(constraints: value, child: child);
9 ui.Container(child: child, constraints: value);
Prefer `ConstrainedBox` over a `Container` used only for constraints.
9 ui.Container(child: child, constraints: value);
10 new ui.Container(key: const Key("box"), constraints: value, child: child);
Prefer `ConstrainedBox` over a `Container` used only for constraints.
10 new ui.Container(key: const Key("box"), constraints: value, child: child);
11 LayoutContainer(constraints: value, child: child);
Prefer `ConstrainedBox` over a `Container` used only for constraints.
11 LayoutContainer(constraints: value, child: child);
12 Container(key: const Key("ordered"), child: child, constraints: (value));

Valid

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

typedef LayoutContainer = Container;

void keepContainer(Widget child, BoxConstraints value, BoxConstraints? nullable, Widget? nullableChild, dynamic unknown) {
  Container(constraints: value);
  Container(child: child);
  Container(constraints: null, child: child);
  Container(constraints: nullable, child: child);
  Container(constraints: unknown, child: child);
  Container(constraints: value, child: null);
  Container(constraints: value, child: nullableChild);
  Container(constraints: value, width: 12, child: child);
  Container(constraints: value, decoration: const BoxDecoration(), child: child);
  Container(constraints: value, color: Colors.red, child: child);
}

Object parameterShadow(dynamic Container, Widget child, BoxConstraints value) =>
    Container(constraints: value, child: child);

Object prefixShadow(dynamic ui, Widget child, BoxConstraints value) =>
    ui.Container(constraints: value, child: child);

Object aliasShadow(dynamic LayoutContainer, Widget child, BoxConstraints value) =>
    LayoutContainer(constraints: value, child: child);

void localShadow(dynamic factory, Widget child, BoxConstraints value) {
  final Container = factory;
  Container(constraints: value, child: child);
}

void unusedTransformAlignment(Widget child, BoxConstraints value) {
  Container(constraints: value, transformAlignment: Alignment.center, child: child);
}

Configure prefer-constrained-box-over-container

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-constrained-box-over-container": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-constrained-box-over-container: <reason>