What is the equivalent of Android's RecyclerView.SCROLL_STATE_IDLE in Flutter

Issue

Android gives scroll states like RecyclerView.SCROLL_STATE_IDLE which tells when user stops the scroll. I’m not able to find any alternative in flutter for Pageview or ListView ScrollListener.

My Problem

I need to detect scroll up/down in PageView to perform some operation based on that. Flutter gives the direction (_myPageViewController.position.userScrollDirection) but it gives a continuous callback. I need to detect it only when the user stops scrolling.

Another scenario

I need to do auto-play videos in a listView. So I need to detect when the user stops scrolling and then get the position and play that.

In android I would have done this with the help RecyclerView.SCROLL_STATE_IDLE state in RecyclerView's scrollListener. Need something similar in flutter.

Solution

According to @pskink and @doc NotificationListener<ScrollNotification> gives the callback of scroll start, scroll end.

NotificationListener<ScrollNotification>(
  onNotification: (scrollNotification) {
    if (scrollNotification is ScrollEndNotification) {
      print("Scroll End"); //Scroll end callback
     }
    return false;
    },
  child: ListView.builder(
      //...
      ),
)

This also works with other scrollable widgets like PageView.

Answered By – Sp4Rx

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.