Chris
Chris

Reputation: 2274

Flutter Detect if user is scrolling

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?

Upvotes: 4

Views: 4017

Answers (1)

daddy7860
daddy7860

Reputation: 431

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(
      //...
    ),
  ),
)

Upvotes: 3

Related Questions