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];
  List<TweenSequenceItem> items = [];
  var width = 0.0;
  double? firstBegin;
  final spans = line.spanList ?? line.defaultSpanList;
  final blankTime = (line.startTime ?? 0) - widget.position;
  if (blankTime > 0) {
    items.add(TweenSequenceItem(
        tween: Tween(begin: 0.0, end: 0.0), weight: blankTime.toDouble()));
  }
  for (LyricSpanInfo element in spans) {
    if (widget.position >= element.end) {
      width += element.drawWidth;
      continue;
    }
    var ratio = (widget.position - element.start) / element.duration;
    if (ratio < 0) {
      ratio = 0;
    }
    var begin = width += (ratio * element.drawWidth);
    firstBegin ??= begin;
    items.add(TweenSequenceItem(
        tween: Tween(begin: begin, end: width += element.drawWidth),
        weight: element.duration.toDouble()));
  }
  disposeHighlight();
  if (items.isEmpty) {
    lyricPaint.highlightWidth = width;
    return;
  }
  final highlightDuration = (line.endTime ?? 0) - widget.position;
  _highlightController = AnimationController(
    duration:
        Duration(milliseconds: highlightDuration > 0 ? highlightDuration : 0),
    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;
  }
}