runAnimation method

void runAnimation()
inherited

Implementation

void runAnimation() {
  final removeKeys = _runningAnimation.keys.where((element) => !animationName.contains(element)).toList();

  removeKeys.forEach((key) {
    Animation animation = _runningAnimation[key]!;
    _runningAnimation.remove(key);
    animation.cancel();
  });

  for (var i = 0; i < animationName.length; i++) {
    final name = animationName[i];
    if (name == NONE) {
      continue;
    }

    final duration = _getSingleString(animationDuration, i);
    final delay = _getSingleString(animationDelay, i);
    final direction = camelize(_getSingleString(animationDirection, i));
    final iterationCount = _getSingleString(animationIterationCount, i);
    final playState = _getSingleString(animationPlayState, i);
    final timingFunction = _getSingleString(animationTimingFunction, i);
    final fillMode = camelize(_getSingleString(animationFillMode, i));

    EffectTiming? options = EffectTiming(
      duration: CSSTime.parseNotNegativeTime(duration)!.toDouble(),
      easing: timingFunction,
      delay: CSSTime.parseTime(delay)!.toDouble(),
      fill: FillMode.values.firstWhere((element) {
        return element.toString().split('.').last == fillMode;
      }, orElse: () {
        return FillMode.both;
      }),
      iterations: iterationCount == 'infinite' ? double.infinity : (double.tryParse(iterationCount) ?? 1),
      direction: PlaybackDirection.values.firstWhere((element) => element.toString().split('.').last == direction),
    );

    List<Keyframe>? keyframes = _getKeyFrames(name);

    if (keyframes != null) {
      KeyframeEffect effect = KeyframeEffect(this, target, keyframes, options);

      Animation? animation = _runningAnimation[name];

      if (animation != null) {
        animation.effect = effect;
      } else {
        animation = Animation(effect, target.ownerDocument.animationTimeline);

        animation.onstart = () {
          target.dispatchEvent(AnimationEvent(EVENT_ANIMATION_START, animationName: name));
        };

        animation.oncancel = (AnimationPlaybackEvent event) {
          target.dispatchEvent(AnimationEvent(EVENT_ANIMATION_END, animationName: name));
          _runningAnimation.remove(name);
          animation?.dispose();
        };

        animation.onfinish = (AnimationPlaybackEvent event) {
          if (isBackwardsFillModeAnimation(animation!)) {
            _revertOriginProperty(_runningAnimation[name]!);
          }

          target.dispatchEvent(AnimationEvent(EVENT_ANIMATION_END, animationName: name));
          animation.dispose();
        };

        _runningAnimation[name] = animation;
      }

      if (playState == 'running' &&
          animation.playState != AnimationPlayState.running &&
          animation.playState != AnimationPlayState.finished) {
        animation.play();
      } else {
        if (playState == 'paused' && animation.playState != AnimationPlayState.paused) {
          animation.pause();
        }
      }
    }
  }
}