prefer-padding-over-container

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

Prefers Padding over a Container used only for padding.

The unnamed Flutter constructor must have a provably non-null child and a provably non-null padding argument, with only an optional key. A lone margin is also replaceable by Padding; combining margin and padding is not reported. 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, EdgeInsetsGeometry value) {
  Container(padding: value, child: child);
  new Container(padding: value, child: child);
  ui.Container(child: child, padding: value);
  new ui.Container(key: const Key("box"), padding: value, child: child);
  LayoutContainer(padding: value, child: child);
  Container(key: const Key("ordered"), child: child, padding: (value));
  Container(margin: value, child: child);
  ui.Container(key: const Key("margin"), margin: value, child: child);
}
Prefer `Padding` over a `Container` used only for padding or margin.
6void examples(Widget child, EdgeInsetsGeometry value) {
7 Container(padding: value, child: child);
Prefer `Padding` over a `Container` used only for padding or margin.
7 Container(padding: value, child: child);
8 new Container(padding: value, child: child);
Prefer `Padding` over a `Container` used only for padding or margin.
8 new Container(padding: value, child: child);
9 ui.Container(child: child, padding: value);
Prefer `Padding` over a `Container` used only for padding or margin.
9 ui.Container(child: child, padding: value);
10 new ui.Container(key: const Key("box"), padding: value, child: child);
Prefer `Padding` over a `Container` used only for padding or margin.
10 new ui.Container(key: const Key("box"), padding: value, child: child);
11 LayoutContainer(padding: value, child: child);
Prefer `Padding` over a `Container` used only for padding or margin.
11 LayoutContainer(padding: value, child: child);
12 Container(key: const Key("ordered"), child: child, padding: (value));
Prefer `Padding` over a `Container` used only for padding or margin.
12 Container(key: const Key("ordered"), child: child, padding: (value));
13 Container(margin: value, child: child);
Prefer `Padding` over a `Container` used only for padding or margin.
13 Container(margin: value, child: child);
14 ui.Container(key: const Key("margin"), margin: value, child: child);

Valid

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

typedef LayoutContainer = Container;

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

Object parameterShadow(dynamic Container, Widget child, EdgeInsetsGeometry value) =>
    Container(padding: value, child: child);

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

Object aliasShadow(dynamic LayoutContainer, Widget child, EdgeInsetsGeometry value) =>
    LayoutContainer(padding: value, child: child);

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

void combined(Widget child, EdgeInsetsGeometry value) {
  Container(padding: value, margin: value, child: child);
}

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

Configure prefer-padding-over-container

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

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

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

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