apply<T extends DataGridRow> method

  1. @override
DataGridState<T>? apply<T extends DataGridRow>(
  1. EventContext<T> context
)
override

Apply this event's transformation to the state. Returns the new state, or null if no state change should occur. Can return a Future for async operations.

Implementation

@override
DataGridState<T>? apply<T extends DataGridRow>(EventContext<T> context) {
  final currentContextState = context.state;

  if (!currentContextState.pagination.enabled) return null;

  final paginationState = currentContextState.pagination;

  final totalPages = paginationState.totalPages(
    currentContextState.totalItems,
  );
  final validPage = math.max(1, math.min(page, totalPages));

  if (validPage == paginationState.currentPage) return null;

  final newPagination = paginationState.copyWith(currentPage: validPage);

  final List<double> newDisplayOrder;
  if (paginationState.serverSide) {
    newDisplayOrder = currentContextState.displayOrder;
  } else {
    final startIndex = newPagination.startIndex(
      currentContextState.totalItems,
    );
    final endIndex = newPagination.endIndex(currentContextState.totalItems);
    final allIds = currentContextState.rowsById.keys.toList();
    newDisplayOrder = allIds.sublist(
      math.min(startIndex, allIds.length),
      math.min(endIndex, allIds.length),
    );
  }

  return currentContextState.copyWith(
    pagination: newPagination,
    displayOrder: newDisplayOrder,
  );
}