lerp method

  1. @override
Gradient lerp(
  1. double t
)
override

Return the value this variable has at the given animation clock value t.

If begin and end are gradients of the same type or if either is null, employs Gradient.lerp; which itself is a step up from the standard behavior.

  • Gradient.lerp will fade to null between gradients of dissimilar types which gives a fad-out/fade-in tween

In all other circumstances, however, this method can generated an IntermediateGradient.

This is done by comparing the runtimeType of begin & end against t, providing the first type before 0.5 and the second after; creating a GradientPacket containing both gradients and passing t which can provide any requested potential gradient property using lerp; and interpolating the colors and stops of begin & end by creating and passing along a PrimitiveGradient.

If needed, consider overriding IntermediateGradient._copyWith by providing a custom GradientCopyWith during construction as overrideCopyWith.

Implementation

@override
Gradient lerp(double t) {
  if (begin == null ||
      end == null ||
      (begin.runtimeType == end.runtimeType)) {
    return Gradient.lerp(begin, end, t)!;
  }

  final resolvedBegin = begin is IntermediateGradient
      ? (begin as IntermediateGradient).resolved
      // : begin is Steps
      //     ? (begin as Steps).asGradient
      : begin!;
  final resolvedEnd = end is IntermediateGradient
      ? (end as IntermediateGradient).resolved
      // : end is Steps
      //     ? (end as Steps).asGradient
      : end!;
  if (resolvedBegin.runtimeType == resolvedEnd.runtimeType) {
    return Gradient.lerp(resolvedBegin, resolvedEnd, t)!;
  }

  if (isAgressive) {
    final interpolated =
        PrimitiveGradient.fromStretchLerp(resolvedBegin, resolvedEnd, t);
    return IntermediateGradient(
      PrimitiveGradient.byProgressiveMerge(
          t < 0.5 ? PrimitiveGradient.from(resolvedBegin) : interpolated,
          t < 0.5 ? interpolated : PrimitiveGradient.from(resolvedEnd),
          t < 0.5 ? t * 2 : (t - 0.5) * 2),
      GradientPacket(resolvedBegin, resolvedEnd, t),
      overrideCopyWith: _copyWith,
    );
  }

  return IntermediateGradient(
    PrimitiveGradient.byCombination(resolvedBegin, resolvedEnd, t),
    GradientPacket(resolvedBegin, resolvedEnd, t),
    overrideCopyWith: _copyWith,
  );
}