pageItems static method

List<int?> pageItems({
  1. required int page,
  2. required int pageCount,
  3. int siblingCount = 1,
  4. int boundaryCount = 1,
})

The page numbers to show for page of pageCount, null where an ellipsis goes: page 5 of 16 with one sibling and one boundary page is [1, null, 4, 5, 6, null, 16]. An ellipsis never stands for one page.

Implementation

static List<int?> pageItems({required int page, required int pageCount, int siblingCount = 1, int boundaryCount = 1}) {
  if (pageCount <= 0) return const [];
  final current = page.clamp(1, pageCount);
  final shown = <int>{
    for (var i = 1; i <= boundaryCount && i <= pageCount; i++) i,
    for (var i = pageCount - boundaryCount + 1; i <= pageCount; i++)
      if (i >= 1) i,
    for (var i = current - siblingCount; i <= current + siblingCount; i++)
      if (i >= 1 && i <= pageCount) i,
  };
  final result = <int?>[];
  for (final number in shown.toList()..sort()) {
    if (result.isNotEmpty) {
      final previous = result.last!;
      if (number - previous == 2) {
        result.add(previous + 1);
      } else if (number - previous > 2) {
        result.add(null);
      }
    }
    result.add(number);
  }
  return result;
}