scrollHasMax static method

bool scrollHasMax(
  1. ScrollController scrollController,
  2. dynamic max
)

This function will set scroll to default position when user scroll to max position

List max = [10, 50]; // [top, bottom]
double max = 50; // this value will set to top and bottom

bool hasMax = scrollHasMax(scrollController, max);

// for example:
void yourScrollListener() {
  double pixel = scrollController.position.pixels;

  if (scrollHasMax(scrollController, [20, 50])) {
    scrollController.animateTo(pixel, duration: const Duration(milliseconds: 250), curve: Curves.easeInBack);
  }
}

Implementation

static bool scrollHasMax(ScrollController scrollController, dynamic max) {
  bool isMaxList = max is List;

  // if max is integer or double
  max = max is int ? max.toDouble() : max;

  if (isMaxList) {
    if (max.length == 1) max.add(max[0]);
    max = max.map((e) => e is int ? e.toDouble() : e).toList();
  }

  double maxT = isMaxList ? max[0] : max;
  double maxB = isMaxList ? max[1] : max;

  double pixel = scrollController.position.pixels;
  double maxPixel = scrollController.position.maxScrollExtent;
  return (pixel < -maxB || pixel > (maxPixel + maxT));
}