observe property Null safety

Gradient observe

Observe the output Gradient which applies modifications to the gradientInput based on the animation and other provided properties, considering storyboard and the relevant mappings from
GradientAnimations -> dynamic, where dynamic matches with the key:

  • GradientAnimation.colorArithmetic: ColorArithmetic
  • GradientAnimation.stopsArithmetic: StopsArithmetic
  • GradientAnimation.tweenSpec: Map<GradientProperty, Tween<dynamic>>

Implementation

// - `GradientAnimation.tweenSpec: TweenSpec`
//   - Where [TweenSpec] is a mapping from [GradientProperty]s -> [Tween]s
/// - `GradientAnimation.tweenSpec: Map<GradientProperty, Tween<dynamic>>`
Gradient get observe {
  if (storyboard.isEmpty || storyboard.containsKey(GradientAnimation.none)) {
    return _gradient;
  }
  var observation = _gradient;
  _makeAssertations();

  if (storyboard.containsKey(GradientAnimation.colorArithmetic)) {
    observation = _copyWith(observation,
        colors: _gradient.colors
            .map<Color>(
              (c) => (storyboard[GradientAnimation.colorArithmetic]
                      as ColorArithmetic)
                  .call(c, animation.value),
            )
            .toList());
  }

  if (storyboard.containsKey(GradientAnimation.stopsArithmetic)) {
    observation = _copyWith(observation,
        stops: interpretStops(_gradient)
            .map<double>(
              (s) => (storyboard[GradientAnimation.stopsArithmetic]
                      as StopsArithmetic)
                  .call(s, animation.value),
            )
            .toList());
  }

  if (!storyboard.containsKey(GradientAnimation.tweenSpec)) {
    return observation;
  }

  // final spec = storyboard[GradientAnimation.tweenSpec] as TweenSpec;
  final spec = storyboard[GradientAnimation.tweenSpec]
      as Map<GradientProperty, Tween<dynamic>>;
  return _copyWith(
    observation,
    begin: spec.containsKey(GradientProperty.begin)
        ? spec[GradientProperty.begin]!.evaluate(animation)
        : observation.begin,
    end: spec.containsKey(GradientProperty.end)
        ? spec[GradientProperty.end]!.evaluate(animation)
        : observation.end,
    center: spec.containsKey(GradientProperty.center)
        ? spec[GradientProperty.center]!.evaluate(animation)
        : observation.center,
    radius: spec.containsKey(GradientProperty.radius)
        ? spec[GradientProperty.radius]!.evaluate(animation)
        : observation.radius,
    focal: spec.containsKey(GradientProperty.focal)
        ? spec[GradientProperty.focal]!.evaluate(animation)
        : observation.focal,
    focalRadius: spec.containsKey(GradientProperty.focalRadius)
        ? spec[GradientProperty.focalRadius]!.evaluate(animation)
        : observation.focalRadius,
    startAngle: spec.containsKey(GradientProperty.startAngle)
        ? spec[GradientProperty.startAngle]!.evaluate(animation)
        : observation.startAngle,
    endAngle: spec.containsKey(GradientProperty.endAngle)
        ? spec[GradientProperty.endAngle]!.evaluate(animation)
        : observation.endAngle,
    softness: spec.containsKey(GradientProperty.softness)
        ? spec[GradientProperty.softness]!.evaluate(animation)
        : observation.softness,
    shadeFactor: spec.containsKey(GradientProperty.shadeFactor)
        ? spec[GradientProperty.shadeFactor]!.evaluate(animation)
        : observation.shadeFactor,
    distance: spec.containsKey(GradientProperty.distance)
        ? spec[GradientProperty.distance]!.evaluate(animation)
        : observation.distance,
  );
}