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('...'),
);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。