itemBuilder method

Widget itemBuilder(
  1. BuildContext context,
  2. int index
)

Implementation

Widget itemBuilder(BuildContext context, int index) {
  // The total number of widgets, is the number of loaded items, plus the
  // number of items that we appended to make all pages the same size,
  // plus 1 for the loader
  final total = (_effectiveController?.loadedItems.length ?? 0) +
      (_effectiveController?._appendedItems.length ?? 0) +
      1;

  if (doesDummyBuilderApply(total)) {
    if (!(_effectiveController?._isFetchingNotifier.value ?? false)) {
      _effectiveController?.fetchNewPage();
    }
    return widget.itemDummyBuilder?.call(context, null, index) ??
        const SizedBox();
  }

  if (index >= total) return const SizedBox();

  if (index == total - 1) {
    if (_effectiveController?.noItemsFound ?? false) {
      return getNoItemsFoundWidget();
    }

    if (_effectiveController?.error != null) {
      if (widget.showRetry) {
        return getRetryWidget();
      } else {
        return getErrorWidget(_effectiveController?.error);
      }
    }

    if (_effectiveController?.hasMoreItems ?? false) {
      _effectiveController?.fetchNewPage();
      return getLoadingWidget();
    } else {
      return Container();
    }
  } else {
    if (index >= (_effectiveController?.loadedItems.length ?? 0)) {
      // this means that the function is asking for an element from the
      // appended items, so we return an empty container
      return Container();
    }
    // Otherwise, we return the actual item
    return widget.itemBuilder(
        context, _effectiveController?.loadedItems[index], index);
  }
}