prefer_transform_over_container
v0.4.0 Warning Fix Widget Replacement
Flags Container widgets that only use the transform parameter (plus optional key and child). When Container is used solely for a transform, the Transform widget is a lighter, more descriptive alternative.
Why use this rule
Section titled “Why use this rule”Container is a convenience widget that composes many lower-level widgets internally. When you only need a matrix transform, using Transform directly avoids the overhead and clearly communicates your intent. It also gives you access to Transform’s named constructors like Transform.rotate and Transform.scale.
See also: Transform | Container
// Container with only transform parameterContainer( transform: Matrix4.rotationZ(math.pi / 6)..rotateX(0.15), child: const Text('Skewed'),);
// Container with only transform and keyContainer( key: const ValueKey('rotated'), transform: Matrix4.rotationZ(math.pi / 4), child: const Text('Rotated'),);// Use Transform directlyTransform( transform: Matrix4.rotationZ(math.pi / 6)..rotateX(0.15), child: const Text('Skewed'),);
// Container with transform and other parameters is fineContainer( transform: Matrix4.rotationZ(math.pi / 4), alignment: Alignment.topRight, child: const Text('Rotated with alignment'),);Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_transform_over_container: trueTo turn it off again:
rules: prefer_transform_over_container: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”prefer_container— Opposing convention. Replace sequences of nested widgets with a single Container.prefer_align_over_container— Use Align instead of Container when only alignment is set.prefer_constrained_box_over_container— Use ConstrainedBox instead of Container when only constraints is set.prefer_padding_over_container— Use Padding instead of Container when only padding or margin is set.