urlWindow static method

List<int?> urlWindow(
  1. int current,
  2. int last, {
  3. int onEachSide = 3,
})

Page labels of LengthAwarePaginator::linkCollection() between the previous/next links: a port of UrlWindow (null = ...).

Implementation

static List<int?> urlWindow(int current, int last, {int onEachSide = 3}) {
  List<int> range(int from, int to) => [for (var p = from; p <= to; p++) p];
  if (last < onEachSide * 2 + 8) {
    return range(1, last);
  }
  final window = onEachSide + 4;
  if (current <= window) {
    return [...range(1, window + onEachSide), null, last - 1, last];
  }
  if (current > last - window) {
    return [1, 2, null, ...range(last - (window + onEachSide - 1), last)];
  }
  return [
    1,
    2,
    null,
    ...range(current - onEachSide, current + onEachSide),
    null,
    last - 1,
    last,
  ];
}