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
623 stars 211 forks source link

[Feature request] refresh with data replacement #115

Closed clragon closed 3 years ago

clragon commented 3 years ago

When using a SmartRefresher and calling pagingController.refresh() inside the onRefresh method, this will lead to the pagingController throwing all items away and then loading new ones.

This also means, that during the loading, the initial loading screen of the Page is shown. The SmartRefresher header is also shown. This means, there are now two loading indicators on the screen.

What I would wish as a feature is refreshing with data replacement. This would mean:

EdsonBueno commented 3 years ago

Hi @clragon . I'm not sure what SmartRefresher is, but I'm guessing it's not from Flutter. You can do what you want simply by not calling pagingController.refresh(). Inside your onRefresh, call a function you create that will load the first page again and does exactly what you want (e.g. not replacing item if it fails). If you want to keep the indicator on the screen until items are loaded, you can use this solution.

fullflash commented 3 years ago

pagingController.refresh() resets previously loaded content.

authors requested feature "pagingController loading new data, but not emptying the items just yet" is very useful.

lauglam commented 2 years ago

@clragon Extends and override these methods to get what you want


class PagingControllerExtent<PageKeyType, ItemType>
    extends PagingController<PageKeyType, ItemType> {
  PagingControllerExtent({
    required super.firstPageKey,
    super.invisibleItemsThreshold,
  });

  /// Whether it is in the state of background refresh
  bool background = false;

  @override
  void refresh({bool background = false}) {
    if (background) {
      this.background = true;
      // Since the state of PagingState has too much influence,
      // skip it here and directly notifyPageRequestListeners.
      notifyPageRequestListeners(firstPageKey);
    } else {
      super.refresh();
    }
  }

  @override
  void appendPage(List<ItemType> newItems, PageKeyType? nextPageKey) {
    // Add judgment here
    final previousItems = background ? <ItemType>[] : value.itemList ?? <ItemType>[];
    final itemList = previousItems + newItems;
    value = PagingState<PageKeyType, ItemType>(
      itemList: itemList,
      error: null,
      nextPageKey: nextPageKey,
    );
    // reduction
    background = false;
  }
}
Heilum commented 8 months ago

good, I think the default value of 'background' parameter of 'refresh' method better be true!