Skip to content

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.

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'),
);

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 build
ClipRRect(
borderRadius: BorderRadius.circular(12),
child: const Text('Clipped'),
);
// Do
const kCardRadius = BorderRadius.all(Radius.circular(12));
ClipRRect(borderRadius: kCardRadius, child: const Text('Clipped'));

The radius does not have to be a literal — the rule reports the factory call itself:

// Don't
final radius = BorderRadius.circular(spacing * 2);
// Do
final 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.

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.

This rule is in the opinionated preset, so it is on with preset: opinionated, or by name:

many_lints.yaml
rules:
prefer_const_border_radius: true

To turn it off again:

many_lints.yaml
rules:
prefer_const_border_radius: false

To keep the rule on but skip certain paths, use per-rule exclude.