lerpFrom method Null safety

  1. @override
ShapeBorder? lerpFrom(
  1. ShapeBorder? a,
  2. double t
)
override

Linearly interpolates from another ShapeBorder (possibly of another class) to this.

When implementing this method in subclasses, return null if this class cannot interpolate from a. In that case, lerp will try a's lerpTo method instead. If a is null, this must not return null.

The base class implementation handles the case of a being null by deferring to scale.

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 this (or something equivalent to this), and values in between meaning that the interpolation is at the relevant point on the timeline between a and this. 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.

Instead of calling this directly, use ShapeBorder.lerp.

Implementation

@override
ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
  if (t == 0) {
    return a;
  }
  if (t == 1.0) {
    return this;
  }
  if (a is StarBorder) {
    return StarBorder(
      side: BorderSide.lerp(a.side, side, t),
      points: ui.lerpDouble(a.points, points, t)!,
      rotation: ui.lerpDouble(a._rotationRadians, _rotationRadians, t)! * _kRadToDeg,
      innerRadiusRatio: ui.lerpDouble(a.innerRadiusRatio, innerRadiusRatio, t)!,
      pointRounding: ui.lerpDouble(a.pointRounding, pointRounding, t)!,
      valleyRounding: ui.lerpDouble(a.valleyRounding, valleyRounding, t)!,
      squash: ui.lerpDouble(a.squash, squash, t)!,
    );
  }

  if (a is CircleBorder) {
    if (points >= 2.5) {
      final double lerpedPoints = ui.lerpDouble(points.round(), points, t)!;
      return StarBorder(
        side: BorderSide.lerp(a.side, side, t),
        points: lerpedPoints,
        squash: ui.lerpDouble(a.eccentricity, squash, t)!,
        rotation: rotation,
        innerRadiusRatio: ui.lerpDouble(math.cos(math.pi / lerpedPoints), innerRadiusRatio, t)!,
        pointRounding: ui.lerpDouble(1.0, pointRounding, t)!,
        valleyRounding: ui.lerpDouble(0.0, valleyRounding, t)!,
      );
    } else {
      // Have a slightly different lerp for two-pointed stars, since they get
      // kind of squirrelly with near-zero innerRadiusRatios.
      final double lerpedPoints = ui.lerpDouble(points, 2, t)!;
      return StarBorder(
        side: BorderSide.lerp(a.side, side, t),
        points: lerpedPoints,
        squash: ui.lerpDouble(a.eccentricity, squash, t)!,
        rotation: rotation,
        innerRadiusRatio: ui.lerpDouble(1, innerRadiusRatio, t)!,
        pointRounding: ui.lerpDouble(0.5, pointRounding, t)!,
        valleyRounding: ui.lerpDouble(0.5, valleyRounding, t)!,
      );
    }
  }

  if (a is StadiumBorder) {
    // Lerp from a stadium to a circle first, and from there to a star.
    final BorderSide lerpedSide = BorderSide.lerp(a.side, side, t);
    return _twoPhaseLerp(
      t,
      0.5,
      (double t) => a.lerpTo(CircleBorder(side: lerpedSide), t),
      (double t) => lerpFrom(CircleBorder(side: lerpedSide), t),
    );
  }
  if (a is RoundedRectangleBorder) {
    // Lerp from a rectangle to a stadium, then from a Stadium to a circle,
    // then from a circle to a star.
    final BorderSide lerpedSide = BorderSide.lerp(a.side, side, t);
    return _twoPhaseLerp(
      t,
      1 / 3,
      (double t) {
        return StadiumBorder(side: lerpedSide).lerpFrom(a, t);
      },
      (double t) {
        return _twoPhaseLerp(
          t,
          0.5,
          (double t) => StadiumBorder(side: lerpedSide).lerpTo(CircleBorder(side: lerpedSide), t),
          (double t) => lerpFrom(CircleBorder(side: lerpedSide), t),
        );
      },
    );
  }
  return super.lerpFrom(a, t);
}