Search code examples
widgetflutterpull-to-refreshswiperefreshlayout

With a Flutter RefreshIndicator can you tell which way it was pulled, from top or bottom in onRefresh?


With Flutter I'm using a RefreshIndicator with my listview, which works well. I'd like to refresh the data when pulled down from the top and load more paginated data if pulled up from the bottom. Is there a way to tell in the onRefresh callback to tell if it's been pulled from the top or from the bottom?

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
  return RefreshIndicator(
    onRefresh: () {},
    child: ListView ...

Solution

  • I don't think thats currently possible via the onRefresh method.

    Maybe you can achieve what you want with a ScrollController:

    1) Define it: var _scrollController = new ScrollController();

    2) On thew widget initState override add your scroll controller
    listener and check if the element has been scrolled and, if so, get the direction on which he scrolled. Further, if you want it to load more content after the scroll hits the end of the list you can also do that here.

    _scrollController.addListener(() {
        print(lastOffset < _scrollController.position.pixels ? "down" : "up");
        lastOffset = _scrollController.position.pixels;
        if (_scrollController.position.pixels ==
            _scrollController.position.maxScrollExtent) {
            // loadMoreContent();
           }
         });
    });