ScrollController how can I detect Scroll start, stop and scrolling?

二次信任 提交于 2021-02-06 10:12:32

问题


I'm using ScrollController for SingleChildScrollView widget where I want to detect when scroll start, end/stop and still scrolling?

How can I detect, I'm using Listene

scrollController = ScrollController()
      ..addListener(() {
        scrollOffset = _scrollController.offset;
      });

Also try with _scrollController.position.activity.velocity but didn't help me.

Also there are

_scrollController.position.didEndScroll();
_scrollController.position.didStartScroll();

But how can I use it?


回答1:


From this link https://medium.com/@diegoveloper/flutter-lets-know-the-scrollcontroller-and-scrollnotification-652b2685a4ac

Just Wrap your SingleChildScrollView to NotificationListener and update your code like ..

NotificationListener<ScrollNotification>(
                onNotification: (scrollNotification) {
                  if (scrollNotification is ScrollStartNotification) {
                    _onStartScroll(scrollNotification.metrics);
                  } else if (scrollNotification is ScrollUpdateNotification) {
                    _onUpdateScroll(scrollNotification.metrics);
                  } else if (scrollNotification is ScrollEndNotification) {
                    _onEndScroll(scrollNotification.metrics);
                  }
                },
                child: SingleChildScrollView(
                /// YOUR OWN CODE HERE
               )
)

And just declare method like

_onStartScroll(ScrollMetrics metrics) {
    print("Scroll Start");
  }

  _onUpdateScroll(ScrollMetrics metrics) {
    print("Scroll Update");
  }

  _onEndScroll(ScrollMetrics metrics) {
    print("Scroll End");
  }

You will be notify by particular method.




回答2:


No need for NotificationListener, we can use solely scroll controller for this.

First, register a post-frame callback by using WidgetsBinding.instance.addPostFrameCallback to make sure that the scroll controller by that time has already associated with a scroll view. We will setup listener in that callback.

For listening to scrolling update we can use scrollController.addListener.

For listening to start and stop scrolling we can use bgScrollCtrl.position.isScrollingNotifier.addListener. You can check the code below:

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');
        }
      });
    });



回答3:


_scrollController.position.pixels

if(_scrollController.position.pixels == _scrollController.position.maxScrollExtent){
//scroll end
}

to use these, you should add a listener to your scrollview




回答4:


NotificationListner not work for me because i animate scrollview. When i touch it ScrollEndNotification is called.

So to detect scrollview reach bottom or top. I added Listner to scrollcontroller.

  _scrollController.addListener(_scrollListener);

  _scrollListener() {
    if (_scrollController.offset >= _scrollController.position.maxScrollExtent &&
        !_scrollController.position.outOfRange) {
      setState(() {
        debugPrint("reach the top");
      });
    }
    if (_scrollController.offset <= _scrollController.position.minScrollExtent &&
        !_scrollController.position.outOfRange) {
      setState(() {
        debugPrint("reach the top");
      });
    }
  }


来源:https://stackoverflow.com/questions/56071731/scrollcontroller-how-can-i-detect-scroll-start-stop-and-scrolling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!