use-colored-box
Same as use_colored_box in Dart lints
- Group
- Style
- Path
lint/style/use-colored-box- Default
- On (recommended)
- Origin
- Dart lints
- Domain
- flutter
- Rule
- 484 of 512
Prefers ColoredBox over a Container that only paints a color behind its child.
A Container whose only arguments are color and child (plus an optional
key) can be replaced by the lighter, const-capable ColoredBox. Only
invocations that resolve to Flutter's Container are considered. Matching
the analyzer's nullability-suffix check, a color with a nullable static type
is excluded, but dynamic and Null still qualify. When the type is unknown,
a constructor invocation or a Flutter Colors.<name> access also qualifies.
The diagnostic covers the constructor name, excluding new and arguments.
Container(color: Colors.blue, child: child); // BAD
ColoredBox(color: Colors.blue, child: child); // GOOD
Invalid
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as m;
Widget namedColor(Widget child) {
return Container(
color: Colors.blue,
child: child,
);
}
Widget constructedColor(Widget child) {
return Container(color: const Color(0xFF00FF00), child: child);
}
Widget nonNullableParameter(Color color, Widget child) {
return Container(key: const Key('k'), color: color, child: child);
}
Widget dynamicColor(dynamic color, Widget child) {
return Container(child: child, color: color);
}
Widget prefixedContainer(Widget child) {
return m.Container(color: Colors.red, child: child);
}
Widget explicitNew(Widget child) {
return new Container(color: Colors.transparent, child: child);
}
Widget explicitNullColor(Widget child) {
return Container(color: null, child: child);
}Widget namedColor(Widget child) { return Container(Widget constructedColor(Widget child) { return Container(color: const Color(0xFF00FF00), child: child);Widget nonNullableParameter(Color color, Widget child) { return Container(key: const Key('k'), color: color, child: child);Widget dynamicColor(dynamic color, Widget child) { return Container(child: child, color: color);Widget prefixedContainer(Widget child) { return m.Container(color: Colors.red, child: child);Widget explicitNew(Widget child) { return new Container(color: Colors.transparent, child: child);Widget explicitNullColor(Widget child) { return Container(color: null, child: child);Valid
import 'package:flutter/material.dart';
Widget alreadyColored(Widget child) {
return ColoredBox(color: Colors.blue, child: child);
}
Widget nullableColor(Color? color, Widget child) {
return Container(color: color, child: child);
}
Widget shadeLookupIsNullable(Widget child) {
return Container(color: Colors.blue[100], child: child);
}
Widget noChild() {
return Container(color: Colors.blue);
}
Widget extraPadding(Widget child) {
return Container(
padding: const EdgeInsets.all(4),
color: Colors.blue,
child: child,
);
}
Widget unprovenColor(BuildContext context, Widget child) {
return Container(color: Theme.of(context).primaryColor, child: child);
}
Widget decorated(Widget child) {
return Container(
color: Colors.blue,
alignment: Alignment.center,
child: child,
);
}Configure use-colored-box
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"use-colored-box": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/use-colored-box: <reason>More style rules
- use-decorated-box
use_decorated_boxrecommendedflutterPrefers
DecoratedBoxover aContainerthat only decorates its child. - use-design-system-item
use-design-system-itemflutterFlags construction of a widget or type that should be replaced by its design-system equivalent.
- use-enums
use_enumsFlags classes that emulate an enum and could be declared as one.
- use-existing-destructuring
Reuse or extend an existing record destructuring instead of rereading a field.
- use-existing-variable
Reuse a local for stable SDK arithmetic and String.length/int parity getters.
- use-full-hex-values-for-flutter-colors
use_full_hex_values_for_flutter_colorsrecommendedflutterRequire an eight-digit hexadecimal integer for the canonical Flutter Color default constructor.
- use-function-type-syntax-for-parameters
use_function_type_syntax_for_parametersrecommendedFlags an old-style function-typed parameter written with inline parameter syntax.
- use-if-null-to-convert-nulls-to-bools
use_if_null_to_convert_nulls_to_boolsrecommendedPrefers
??to comparing a nullableboolagainst a boolean literal.