Detect DragEnd in CustomScrollView flutter?

1.3k views Asked by At

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

1

There are 1 answers

3
Guillaume Roux On

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