Rules / Complexity / avoid-unnecessary-containers

avoid-unnecessary-containers

lint/complexity/avoid-unnecessary-containers
recommendedflutter
Same as avoid_unnecessary_containers · Dart lints

Flags a Container whose only argument is a child.

A Container that wraps nothing but a child adds a widget to the tree that neither paints, sizes, nor positions anything — it is pure overhead. Dropping it and using the child directly shrinks the widget tree, which helps rebuild performance and readability. The rule fires only when Container has exactly one named argument, child, and no positional arguments.

Invalid

example.dartdart
// Bad: a Container whose only argument is child.
import 'package:flutter/material.dart';

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

Widget b1() => Container(child: SizedBox());

Widget b2() {
  return Container(
    child: Text('x'),
  );
}

Widget b3() => Container(child: Icon(Icons.home));

Widget b4() => Container(child: Padding(padding: EdgeInsets.zero));

Widget b5() => Container(child: Text('last'));
Avoid a Container with only a child; use the child directly.
3
4Widget b0() => Container(child: Text('a'));
Avoid a Container with only a child; use the child directly.
5
6Widget b1() => Container(child: SizedBox());
Avoid a Container with only a child; use the child directly.
8Widget b2() {
9 return Container(
Avoid a Container with only a child; use the child directly.
13
14Widget b3() => Container(child: Icon(Icons.home));
Avoid a Container with only a child; use the child directly.
15
16Widget b4() => Container(child: Padding(padding: EdgeInsets.zero));
Avoid a Container with only a child; use the child directly.
17
18Widget b5() => Container(child: Text('last'));

Valid

example.dartdart
// Good: Containers with more than a child, or non-Container widgets.
import 'package:flutter/material.dart';

Widget g0() => Container(width: 10, child: Text('a'));

Widget g1() => Container(color: Colors.red, child: Text('b'));

Widget g2() => Container();

Widget g3() => Container(padding: EdgeInsets.zero, child: Text('c'));

Widget g4() => SizedBox(child: Text('d'));

Widget g5() => Center(child: Text('e'));

Widget g6() => Container(alignment: Alignment.center, child: Text('f'));

How to configure

Set the severity of avoid-unnecessary-containers in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "avoid-unnecessary-containers": "error"
      }
    }
  }
}