Skip to main content

use_decorated_box

Use 'DecoratedBox' rather than a 'Container' with only a 'Decoration'.

Description

#

The analyzer produces this diagnostic when a Container is created that only sets the decoration.

Example

#

The following code produces this diagnostic because the only attribute of the container that is set is the decoration:

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

Widget buildArea() {
  return Container(
    decoration: const BoxDecoration(
      color: Colors.red,
      borderRadius: BorderRadius.all(
        Radius.circular(5),
      ),
    ),
    child: const Text('...'),
  );
}

Common fixes

#

Replace the Container with a DecoratedBox:

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

Widget buildArea() {
  return DecoratedBox(
    decoration: const BoxDecoration(
      color: Colors.red,
      borderRadius: BorderRadius.all(
        Radius.circular(5),
      ),
    ),
    child: const Text('...'),
  );
}