handleHighlight method

void handleHighlight()

enable highlight animation if playing status is null,no highlight.

Implementation

void handleHighlight() {
  var lyrics = widget.model?.lyrics;
  if (!widget.ui.enableHighlight() ||
      widget.playing == null ||
      widget.model.isNullOrEmpty ||
      lyricPaint.playingIndex >= lyrics!.length) return;
  var line = lyrics[lyricPaint.playingIndex];
  var lineDuration = (line.endTime ?? 0) - (line.startTime ?? 0);
  List<TweenSequenceItem> items = [];
  var width = 0.0;
  var duration = 0;
  double? firstBegin;
  for (LyricSpanInfo element in (line.spanList ?? line.defaultSpanList)) {
    if (widget.position >= element.end) {
      width += element.drawWidth;
      duration += element.duration;
      continue;
    }
    var ratio = (widget.position - element.start) / element.duration;
    if (ratio < 0) {
      ratio = 0;
    }
    var begin = width += (ratio * element.drawWidth);
    if (firstBegin == null) {
      firstBegin = begin;
    }
    items.add(TweenSequenceItem(
        tween: Tween(begin: begin, end: width += element.drawWidth),
        weight: element.duration / (lineDuration - duration)));
  }
  disposeHighlight();
  if (items.isEmpty) {
    lyricPaint.highlightWidth = width;
    return;
  }
  _highlightController = AnimationController(
    duration: Duration(milliseconds: lineDuration - duration),
    vsync: this,
  );
  var animate = TweenSequence(items)
      .chain(CurveTween(curve: Curves.easeInOut))
      .animate(_highlightController!)
    ..addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        disposeHighlight();
      }
    });
  animate
    ..addListener(() {
      lyricPaint.highlightWidth = animate.value;
    });
  if (widget.playing == true) {
    _highlightController?.forward();
  } else {
    lyricPaint.highlightWidth = firstBegin ?? width;
  }
}