EdsonBueno / infinite_scroll_pagination

Flutter package to help you lazily load and display pages of items as the user scrolls down your screen.
https://pub.dev/packages/infinite_scroll_pagination
MIT License
612 stars 202 forks source link

Add prepend functionality to allow adding to top of list #267

Closed okaforcj closed 5 months ago

okaforcj commented 1 year ago

One of the issues, I'm having and a few people are having is the ability to prepend to a list especially in use cases of a chat application. See issue: https://github.com/EdsonBueno/infinite_scroll_pagination/issues/244 . In my case, I'm paginating the chat and coupling it with a snapshot listener which should prepend the list with the newest chat item.

Currently there is no way of doing that efficiently and would have to do it outside of the pagingcontroller along with managing unnecessary state. It turns out the solution is pretty simple within the codebase. I've written the feature and simple test for it and I'm using this version in my app currently. It would be nice if it could be merged or considered. Please let me know if there are any concerns, I'll be more than happy to address them

Edit: PS There is no contributing guide, so I'm unsure of how to contribute the project, please let me know if there is a

akmalirfan commented 9 months ago

My current solution to prepend items is to check the index in the builder. If the index is 0, then the builder will return a Column with the prepended widgets and the first widget as its children.

Personally, I don't find this prepend functionality to be necessary but I agree this functionality might be a more elegant solution to this problem.

clragon commented 8 months ago

It should be very easy to add this functionality from the outside:

extension PrependPageExtension<PageKeyType, ItemType> on PagingController<PageKeyType, ItemType> {
  /// Prepends [newItems] to the previously loaded ones
  void prependPage(List<ItemType> newItems) {
    final previousItems = value.itemList ?? [];
    final itemList = newItems + previousItems;
    value = PagingState<PageKeyType, ItemType>(
      itemList: itemList,
      error: null,
      nextPageKey: nextPageKey,
    );
  }
}

and youre done. now you can use that method on any Controller just like it was always part of it.

lybur commented 5 months ago

It should be very easy to add this functionality from the outside:

extension PrependPageExtension<PageKeyType, ItemType> on PagingController<PageKeyType, ItemType> {
  /// Prepends [newItems] to the previously loaded ones
  void prependPage(List<ItemType> newItems) {
    final previousItems = value.itemList ?? [];
    final itemList = newItems + previousItems;
    value = PagingState<PageKeyType, ItemType>(
      itemList: itemList,
      error: null,
      nextPageKey: nextPageKey,
    );
  }
}

and youre done. now you can use that method on any Controller just like it was always part of it.

I was able to get the updated value, but it's not being displayed in PagedListView, what could be the issue? I applied the extension method in my bloc event.

clragon commented 5 months ago

I believe this to be a fine addition but Edson has never expressed whether this would be appropriate. Additionally, I am shying away from adding more methods to the Controller right now as I am looking into simplifying it greatly.

For those reasons I will close this PR for now. Thank you for your contribution nonetheless.