Flutter Detect if user is scrolling

345

If you, for some reason, are avoiding the very helpful widget NotificationListener, then you might find this GestureDetector hack useful

It's not perfect, but basically you wrap the scrollable widget you're using (SingleChildScrollView, Wrap, etc) in a GestureDetector, and use its onTapDown to assume the user has begun scrolling, and onTapUp to assume they've stopped scrolling, and it can work for most cases.

I haven't tested this code, but here's an example to give you insight:

ClickableListWheelScrollView(
  //...
  child: GestureDetector(
    onTapUp: () {
      print('scrolling has stopped');
    },
    onTapDown: () {
      print('scrolling has started');
    },
    child: ListWheelScrollView.useDelegate(
      //...
    ),
  ),
)

Share:
345
Chris
Author by

Chris

Updated on December 01, 2022

Comments

  • Chris
    Chris over 1 year

    I am using ClickableListWheelView and I would like to add a notification to it, so I can check when the user starts/stops scrolling. For that I tried this inside _ClickableListWheelScrollViewState's init :

    Got it from: https://stackoverflow.com/a/63675037/11968226

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      scrollCtrl.addListener(() { 
        print('scrolling');
      });
      scrollCtrl.position.isScrollingNotifier.addListener(() { 
        if(!scrollCtrl.position.isScrollingNotifier.value) {
          print('scroll is stopped');
        } else {
          print('scroll is started');
        }
      });
    });
    

    But this is only printing "scrolling" but never "stop/start".

    I do not want to wrap the view inside a NotificationListener.

    What am I missing here?