Skip to content

Commit

Permalink
feat: add support for nullable prefetchIndex
Browse files Browse the repository at this point in the history
Signed-off-by: Sahil Kumar <xdsahil@gmail.com>
  • Loading branch information
xsahil03x committed Feb 7, 2024
1 parent e2844a3 commit 2f708fa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ SuperPager(
//
// By default, the pager will start loading when the user scrolls
// within 3 items of the end of the loaded content.
//
// If set to null, the pager will not start loading more content until
// they are specifically requested by the user.
prefetchIndex: 3,
// Defines the maximum number of items to keep in memory before
Expand Down
8 changes: 6 additions & 2 deletions lib/src/paging_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class PagingConfig {
this.maxSize,
}) : initialLoadSize = initialLoadSize ?? pageSize * initialPageMultiplier,
assert(
prefetchIndex <= pageSize / 2,
prefetchIndex == null || prefetchIndex <= pageSize / 2,
'Prefetch index must be less than pageSize / 2',
),
assert(
Expand Down Expand Up @@ -43,7 +43,11 @@ class PagingConfig {
/// E.g., If this value is set to 3, a [SuperPager] will attempt to load the
/// next page in advance when the user scrolls within 3 items of the end of
/// currently loaded content.
final int prefetchIndex;
///
/// A value of `null` indicates that no list items will be loaded until they
/// are specifically requested. This is generally not recommended, so that
/// users don't observe a end of list while scrolling.
final int? prefetchIndex;

/// Defines requested load size for initial load from [PagingSource],
/// typically larger than [pageSize], so on first load data there's a large
Expand Down
4 changes: 4 additions & 0 deletions lib/src/widget/bidirectional_paging_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ class _BidirectionalPagingListViewState<Key, Value>

// Helper function to generate prepend and append load trigger notifications
void generatePrependAppendLoadTriggerNotification(int index) {
// If there is no prefetch index, we don't need to generate any
// notifications.
if (fetchIndex == null) return;

// Check if the index corresponds to near the top or bottom based on the
// 'reverse' flag.
final nearTop =
Expand Down
4 changes: 4 additions & 0 deletions lib/src/widget/paging_sliver_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ class _PagingSliverListState<Key, Value>
final prefetchIndex = pager.config.prefetchIndex;

void generatePrependAppendLoadTriggerNotification(int index) {
// If there is no prefetch index, we don't have to generate any
// notification.
if (prefetchIndex == null) return;

// Generate prepend notification.
if (index == prefetchIndex) {
onBuildingPrependLoadTriggerItem?.call();
Expand Down

0 comments on commit 2f708fa

Please sign in to comment.