fastScrollWidget method

Widget fastScrollWidget(
  1. Widget child
)

Implementation

Widget fastScrollWidget(Widget child) {
  int? t; //Tid
  late double p; //Position
  return Listener(
      onPointerMove: (pos) {
        //Get pointer position when pointer moves
        //If time since last scroll is undefined or over 100 milliseconds
        if (t == null || DateTime.now().millisecondsSinceEpoch - t! > 100) {
          t = DateTime.now().millisecondsSinceEpoch;
          p = pos.position.dx; //x position
        } else {
          //Calculate velocity
          final double v = (p - pos.position.dx) /
              (DateTime.now().millisecondsSinceEpoch - t!);
          if (v < -2 || v > 2) {
            final vx = (v * 1.2).isFinite ? (v * 1.2).round() : 0;
            //Don't run if velocity is to low
            //Move to page based on velocity (increase velocity multiplier to scroll further)
            widget.pageController.animateToPage(
                widget.pageController.page!.toInt() + vx,
                duration: const Duration(milliseconds: 400),
                curve: Curves.easeOutCubic);
          }
        }
      },
      child: child);
}