SebastienBtr/Dash-Chat-2

Unable to Implement Pagination with Scroll Listener in Dash Chat 2

Closed this issue · 3 comments

Hello,

I am currently using the Dash Chat 2 package for Flutter in my project and I am facing some difficulties implementing pagination with a scroll listener for chat.

Here's what I am trying to achieve:

  • I want to load more messages when the user scrolls up to the top of the chat.
  • I am trying to use a scroll listener to detect when the user has scrolled to the top so that I can load more messages.

Could you please provide some guidance on how I can successfully implement this feature? Any help would be greatly appreciated.

Thank you in advance for your time and assistance.

Best regards,
Mujahedul Islam.

You will want to pass in a scroll controller to MessageListOptions(scrollController: _scrollController);

From there listen to it _scrollController.addListener(_onScroll);

and do something like:

  void _onScroll() {
    if (!_fetchingMessages) {
      // if the top is reached
      if (_scrollController.offset >=
          _scrollController.position.maxScrollExtent) {
        setState(() {
          _fetchingMessages = true;
        });
        // do things
        ...
        // reset fetching
        WidgetsBinding.instance.addPostFrameCallback((_) {
          setState(() {
            _fetchingMessages = false;
          });
        });
      }
    }
  }

Hey, you don't need to create a custom scrollController listener like suggested by @LegendAF. You can achieve the same by using onLoadEarlier from MessageListOptions and add your logic to fetch more messages and append them to the list of existing messages.

Let me know if you have more questions regarding the pagination

Closing this, let us know if you have more questions