startTimer method

void startTimer()

Implementation

void startTimer() {
  final double widgetWidth =
      _key.currentContext!.findRenderObject()!.paintBounds.size.width;
  final double widgetHeight =
      _key.currentContext!.findRenderObject()!.paintBounds.size.height;

  timer = Timer.periodic(
    Duration(milliseconds: duration),
    (timer) {
      final double maxScrollExtent =
          scrollController!.position.maxScrollExtent;
      final double pixels = scrollController!.position.pixels;
      //When the distance of animateTo is greater than the maximum sliding distance, return to the specific position of the first child so that the end is exactly on the right side, and then continue to roll, creating the illusion of marquee
      if (pixels + _moveDistance >= maxScrollExtent) {
        if (widget.scrollAxis == Axis.horizontal) {
          //maxScrollExtent is the maximum sliding distance, the non-slidable distance is not counted (that is, the width of the ListView control), maxScrollExtent + widgetWidth is the true width of the children
          //(maxScrollExtent+widgetWidth-blankWidth)/2 can calculate the length of a TextView control, and then subtract widgetWidth. Calculate the offset required for the first child to shift to the rightmost
          //When animateTo slides to the end, but there is still a distance from the end, this distance should be taken into account when jumpingTo pixels-maxScrollExtent
          //The original calculation formula (maxScrollExtent+widgetWidth-blankWidth)/2 -widgetWidth + pixels- maxScrollExtent, the following calculation formula is simplified
          position = (maxScrollExtent - blankWidth! - widgetWidth) / 2 +
              pixels -
              maxScrollExtent;
        } else {
          position = (maxScrollExtent - blankHeight! - widgetHeight) / 2 +
              pixels -
              maxScrollExtent;
        }
        scrollController!.jumpTo(position);
      }
      position += _moveDistance;
      scrollController!.animateTo(
        position,
        duration: Duration(milliseconds: duration),
        curve: Curves.linear,
      );
    },
  );
}