1

I want to detect vertical end of dragging of a CustomScrollView what I did was something like this:

GestureDetector(
  onVerticalDragEnd: (details) {}
  child: CustomScrollView(...)
);

but it's not working, it seems like these two widgets have conflicts, I'm looking for a workaround with this issue

Shaheen Zahedi
  • 1,216
  • 3
  • 15
  • 40

1 Answers1

2

Using a GestureDetector is not the correct way, here is a way of triggering a method when you stop dragging your scroll view:

class MyWidget extends StatelessWidget {
  _onEndScroll(ScrollMetrics metrics) {
    print('Stopped Dragging');
  }
  
  @override
  Widget build(BuildContext context) {
    return NotificationListener<ScrollNotification>(
      onNotification: (scrollNotification) {
        if (scrollNotification is ScrollEndNotification) {
          _onEndScroll(scrollNotification.metrics);
        }
        return false;
      },
      child: SingleChildScrollView(
          child: Column(children: <Widget>[
        ...List<Widget>.generate(
          100,
          (index) => ListTile(title: Text(index.toString())),
        )
      ])),
    );
  }
}

Simply wrap your scroll view inside a NotificationListener widget then you will be able to get any notification from your scroll view and you only need to manage your action depending on the notification's type. (I return false at the end of onNotification to keep listening to upcoming notification.)

Test the full code on DartPad

Guillaume Roux
  • 6,352
  • 1
  • 13
  • 38
  • Yeah I've tried that one before, but it has problems, first I can't detect the velocity unless Scroll is ended, second I wanna call scrollcontroller.animateTo() in _onEndScroll which somehow can't be done, it seems like I have to update my question or delete it – Shaheen Zahedi Oct 07 '20 at 09:57
  • Yeah you should create another question with those details as they were not mentionned firsthand. – Guillaume Roux Oct 07 '20 at 11:02
  • in order for me to delete this, I think you have to delete your answer too – Shaheen Zahedi Oct 22 '20 at 05:44