prefer_const_border_radius
v0.4.0 Warning Fix Widget Replacement
Flags usages of BorderRadius.circular() which should be replaced with BorderRadius.all(Radius.circular(...)). The BorderRadius.circular() factory delegates to BorderRadius.all(Radius.circular()) internally, but it cannot be made const because it is a factory constructor.
Why use this rule
Section titled “Why use this rule”BorderRadius.circular(8) is a factory, and a factory can never be const. It
forwards straight to BorderRadius.all(Radius.circular(8)), which can. Writing
the second form lets the whole surrounding expression be constant, so it is
allocated once at compile time instead of on every build.
See also: BorderRadius | Dart lint: prefer_const_constructors
// A rounded card decoration rebuilt on every frame.Container( decoration: BoxDecoration( color: const Color(0xFFEEEEEE), borderRadius: BorderRadius.circular(8), // LINT ), child: const Text('Hello'),);Container( decoration: const BoxDecoration( color: Color(0xFFEEEEEE), borderRadius: BorderRadius.all(Radius.circular(8)), ), child: const Text('Hello'),);Examples
Section titled “Examples”A shared radius constant
Section titled “A shared radius constant”Once the expression is const-capable, it can be hoisted to a top-level constant and reused:
// Don't — a new object per call site, per buildClipRRect( borderRadius: BorderRadius.circular(12), child: const Text('Clipped'),);
// Doconst kCardRadius = BorderRadius.all(Radius.circular(12));
ClipRRect(borderRadius: kCardRadius, child: const Text('Clipped'));Any argument shape is reported
Section titled “Any argument shape is reported”The radius does not have to be a literal — the rule reports the factory call itself:
// Don'tfinal radius = BorderRadius.circular(spacing * 2);
// Dofinal radius = BorderRadius.all(Radius.circular(spacing * 2));Here the value is not constant, so const is not available — but the explicit
form is still what the rule asks for, and it becomes const the moment spacing
does.
Known limitations
Section titled “Known limitations”The fix does not add const for you. It rewrites
BorderRadius.circular(r) into BorderRadius.all(Radius.circular(r)) and
stops there. Adding the keyword is the SDK’s
prefer_const_constructors
job — turn that on to get the second half of the win.
Only BorderRadius.circular is matched. BorderRadius.horizontal,
BorderRadius.vertical and BorderRadius.only are factories too, but they take
Radius arguments rather than doubles, so there is no mechanical rewrite to
offer.
Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_const_border_radius: trueTo turn it off again:
rules: prefer_const_border_radius: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_border_all— Use Border.fromBorderSide instead of Border.all for const support.avoid_expanded_as_spacer— Use Spacer instead of Expanded with an empty child.avoid_incorrect_image_opacity— Use Image’s opacity parameter instead of wrapping in Opacity.avoid_wrapping_in_padding— Avoid wrapping widgets that support padding in a Padding widget.