How to listen to scroll events of a list view inside a list view in Flutter?

Viewed 62

I have my widget tree as follows:

ListView
  - ListItem1
  - ListItem2
  - AnotherListView (shrinkWrap: true, primary: false)

This is working as expected and I can scroll this complete widget tree as it is a one widget. Problem arises when AnotherListView reaches to end of the list and I try to fetch more data from server. The scroll controller of AnotherListView is not firing when placed like this in parent ListView. It works as expected independently.

Here's my code. StreamWidget is returning the list view.

Expanded(
   child: ListView(
      children: [
         _buildPostBody(),
         const Divider(),
         PostEngagementControlsWidget(postModel: widget.postModel),
         const Divider(),
         StreamWidget(
           iStreamDataFactory: PostCommentsDataFactory(
              context: context,
              post: widget.postModel,
            ),
           iStreamWidgetFactory: PostCommentsWidgetFactory(),
           ),
         ],
        ),
    );
    
1 Answers

You can define a ScrollController and can listen to its events in initState like this:

final ScrollController winController = ScrollController();

winController.addListener(() {
      if (winController.position.pixels == winController.position.maxScrollExtent) {
          loadWin();
      }
    });
Related