ScrollBuilder.threshold constructor

ScrollBuilder.threshold({
  1. Key? key,
  2. required ScrollController controller,
  3. required double threshold,
  4. required Widget child,
  5. Widget? before,
  6. Duration duration = const Duration(milliseconds: 150),
  7. bool showAfter = true,
})

Implementation

factory ScrollBuilder.threshold({
  Key? key,
  required ScrollController controller,
  required double threshold,
  required Widget child,
  Widget? before,
  Duration duration = const Duration(milliseconds: 150),
  bool showAfter = true,
}) {
  double lastOffset = 0;
  return ScrollBuilder(
      controller: controller,
      child: child,
      key: key,
      builder: (context, child) {
        final isShown = showAfter ? controller.offset > threshold : controller.offset < threshold;
        return AnimatedSwitcher(
          duration: duration,
          child: isShown ? child : (before ?? SizedBox(height: 0)),
        );
      },
      shouldRebuild: () {
        final newOffset = controller.offset;
        final isSwitched =
            (lastOffset < threshold && newOffset > threshold) || (lastOffset > threshold && newOffset < threshold);
        lastOffset = newOffset;
        return isSwitched;
      });
}