prefer-constrained-box-over-container
- Group
- Style
- Path
lint/style/prefer-constrained-box-over-container- Default
- Off, opt in
- Origin
- Falcon
- Domain
- flutter
- Rule
- 331 of 512
Prefers ConstrainedBox over a Container used only for constraints.
The unnamed Flutter constructor must have a provably non-null child and
a provably non-null constraints argument, with only an optional key.
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
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as ui;
typedef LayoutContainer = Container;
void examples(Widget child, BoxConstraints value) {
Container(constraints: value, child: child);
new Container(constraints: value, child: child);
ui.Container(child: child, constraints: value);
new ui.Container(key: const Key("box"), constraints: value, child: child);
LayoutContainer(constraints: value, child: child);
Container(key: const Key("ordered"), child: child, constraints: (value));
}void examples(Widget child, BoxConstraints value) { Container(constraints: value, child: child); Container(constraints: value, child: child); new Container(constraints: value, child: child); new Container(constraints: value, child: child); ui.Container(child: child, constraints: value); ui.Container(child: child, constraints: value); new ui.Container(key: const Key("box"), constraints: value, child: child); new ui.Container(key: const Key("box"), constraints: value, child: child); LayoutContainer(constraints: value, child: child); LayoutContainer(constraints: value, child: child); Container(key: const Key("ordered"), child: child, constraints: (value));Valid
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as ui;
typedef LayoutContainer = Container;
void keepContainer(Widget child, BoxConstraints value, BoxConstraints? nullable, Widget? nullableChild, dynamic unknown) {
Container(constraints: value);
Container(child: child);
Container(constraints: null, child: child);
Container(constraints: nullable, child: child);
Container(constraints: unknown, child: child);
Container(constraints: value, child: null);
Container(constraints: value, child: nullableChild);
Container(constraints: value, width: 12, child: child);
Container(constraints: value, decoration: const BoxDecoration(), child: child);
Container(constraints: value, color: Colors.red, child: child);
}
Object parameterShadow(dynamic Container, Widget child, BoxConstraints value) =>
Container(constraints: value, child: child);
Object prefixShadow(dynamic ui, Widget child, BoxConstraints value) =>
ui.Container(constraints: value, child: child);
Object aliasShadow(dynamic LayoutContainer, Widget child, BoxConstraints value) =>
LayoutContainer(constraints: value, child: child);
void localShadow(dynamic factory, Widget child, BoxConstraints value) {
final Container = factory;
Container(constraints: value, child: child);
}
void unusedTransformAlignment(Widget child, BoxConstraints value) {
Container(constraints: value, transformAlignment: Alignment.center, child: child);
}Configure prefer-constrained-box-over-container
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"style": {
"prefer-constrained-box-over-container": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/prefer-constrained-box-over-container: <reason>More style rules
- prefer-constructors-over-static-methods
prefer_constructors_over_static_methodsFlags static methods that create and return an instance of their own class.
- prefer-containerflutter
Suggests collapsing compatible nested layout widgets into one
Container. - prefer-correct-callback-field-name
Match callback field names to a configured regular expression.
- prefer-correct-error-name
Require configured error parameter names in catches and SDK error callbacks.
- prefer-correct-handler-name
Match callback handler methods to a configured regular expression.
- prefer-correct-identifier-length
prefer-correct-identifier-lengthFlags declared identifiers that are shorter or longer than the allowed range.
- prefer-correct-notifier-file-nameriverpod
Requires
snake_casefile names ending in _notifier.dart for resolved Riverpod notifier classes. - prefer-correct-provider-file-nameriverpod
Requires
snake_casefile names ending in _provider.dart for resolved Riverpod provider declarations, including annotated generator functions.