renderData method

  1. @override
Future<Map<String, Object?>> renderData()
override

Generates and returns a map of data for rendering the pagination UI.

The method calculates the page range to display, handles edge cases where the page number is out of bounds, and includes additional query parameters.

The returned map includes:

  • total: The total number of items.
  • count: The total number of pages.
  • page: The current page.
  • pageSize: The number of items per page.
  • toEnd: The index of the last item on the current page.
  • profix: The page query parameter name.
  • rangeTo: The last page in the displayed range.
  • rangeFrom: The first page in the displayed range.
  • disableFirst: Whether the "first page" button should be disabled.
  • disableLast: Whether the "last page" button should be disabled.
  • other: Additional query parameters.
  • orderBy: The field used for ordering.
  • orderReverse: Whether the order is reversed.

Implementation

@override
Future<Map<String, Object?>> renderData() async {
  page = page < 1 ? 1 : page;
  var count = (total / pageSize).ceil();
  if (page > count) {
    page = count;
  }
  var toEnd = page * pageSize;
  toEnd = (toEnd > total) ? total : toEnd;
  var rangeFrom = page - widthSide;
  var rangeTo = page + widthSide;
  if (page <= widthSide) {
    rangeTo += widthSide - page + 1;
  }

  if (count - widthSide < page) {
    rangeFrom -= widthSide - (count - page);
  }

  if (useRequsetQueries) {
    rq.uri.queryParameters.forEach((key, value) {
      if (key != profix && !otherQuery.containsKey(key)) {
        otherQuery[key] = value;
      }
    });
  }

  var other = otherQuery.joinMap('=', '&');

  var viewParams = {
    'total': total,
    'count': count,
    'page': page,
    'pageSize': pageSize,
    'toEnd': toEnd,
    'profix': profix,
    'rangeTo': rangeTo,
    'rangeFrom': rangeFrom,
    'disableFirst': page - 1 <= 0,
    'disableLast': page - count >= 0,
    'other': other.isEmpty ? "" : "&$other",
    'orderBy': orderBy,
    'orderReverse': orderReverse,
  };

  return viewParams;
}