lerp method Null safety
- BoxDecoration? a,
- BoxDecoration? b,
- double t
Linearly interpolate between two box decorations.
Interpolates each parameter of the box decoration separately.
The shape is not interpolated. To interpolate the shape, consider using a ShapeDecoration with different border shapes.
If both values are null, this returns null. Otherwise, it returns a
non-null value. If one of the values is null, then the result is obtained
by applying scale to the other value. If neither value is null and t == 0.0
, then a
is returned unmodified; if t == 1.0
then b
is returned
unmodified. Otherwise, the values are computed by interpolating the
properties appropriately.
The t
argument represents position on the timeline, with 0.0 meaning
that the interpolation has not started, returning a
(or something
equivalent to a
), 1.0 meaning that the interpolation has finished,
returning b
(or something equivalent to b
), and values in between
meaning that the interpolation is at the relevant point on the timeline
between a
and b
. The interpolation can be extrapolated beyond 0.0 and
1.0, so negative values and values greater than 1.0 are valid (and can
easily be generated by curves such as Curves.elasticInOut).
Values for t
are usually obtained from an Animation<double>, such as
an AnimationController.
See also:
- Decoration.lerp, which can interpolate between any two types of Decorations, not just BoxDecorations.
- lerpFrom and lerpTo, which are used to implement Decoration.lerp and which use BoxDecoration.lerp when interpolating two BoxDecorations or a BoxDecoration to or from null.
Implementation
static BoxDecoration? lerp(BoxDecoration? a, BoxDecoration? b, double t) {
assert(t != null);
if (a == null && b == null) {
return null;
}
if (a == null) {
return b!.scale(t);
}
if (b == null) {
return a.scale(1.0 - t);
}
if (t == 0.0) {
return a;
}
if (t == 1.0) {
return b;
}
return BoxDecoration(
color: Color.lerp(a.color, b.color, t),
image: t < 0.5 ? a.image : b.image, // TODO(ianh): cross-fade the image
border: BoxBorder.lerp(a.border, b.border, t),
borderRadius: BorderRadiusGeometry.lerp(a.borderRadius, b.borderRadius, t),
boxShadow: BoxShadow.lerpList(a.boxShadow, b.boxShadow, t),
gradient: Gradient.lerp(a.gradient, b.gradient, t),
shape: t < 0.5 ? a.shape : b.shape,
);
}