buildPaginationControls function

Widget buildPaginationControls({
  1. required int currentPage,
  2. required int totalPages,
  3. required dynamic onPageChanged(
    1. int
    ),
  4. required bool isMobile,
})

Implementation

Widget buildPaginationControls({
  required int currentPage,
  required int totalPages,
  required Function(int) onPageChanged,
  required bool isMobile,
}) {
  int startPage = (currentPage - 2).clamp(1, totalPages);
  int endPage = (startPage + 4).clamp(1, totalPages);
  if (endPage - startPage < 4 && startPage > 1) {
    startPage = (endPage - 4).clamp(1, totalPages);
  }

  List<int> visiblePages = [];
  for (int i = startPage; i <= endPage; i++) {
    visiblePages.add(i);
  }

  return Container(
    padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 12),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        _buildPaginationButton(
          icon: Icons.first_page,
          isEnabled: currentPage > 1,
          onTap: () => onPageChanged(1),
        ),
        const SizedBox(width: 4),
        _buildPaginationButton(
          icon: Icons.chevron_left,
          isEnabled: currentPage > 1,
          onTap: () => onPageChanged(currentPage - 1),
        ),
        const SizedBox(width: 8),
        if (!isMobile) ...[
          if (startPage > 1) ...[
            _buildPageNumber(1, currentPage == 1, () => onPageChanged(1)),
            if (startPage > 2)
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4),
                child:
                    Text('...', style: TextStyle(color: Colors.grey.shade600)),
              ),
          ],
        ],
        ...visiblePages.map((page) => Padding(
              padding: const EdgeInsets.symmetric(horizontal: 2),
              child: _buildPageNumber(
                page,
                currentPage == page,
                () => onPageChanged(page),
              ),
            )),
        if (!isMobile) ...[
          if (endPage < totalPages) ...[
            if (endPage < totalPages - 1)
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 4),
                child:
                    Text('...', style: TextStyle(color: Colors.grey.shade600)),
              ),
            _buildPageNumber(totalPages, currentPage == totalPages,
                () => onPageChanged(totalPages)),
          ],
        ],
        const SizedBox(width: 8),
        _buildPaginationButton(
          icon: Icons.chevron_right,
          isEnabled: currentPage < totalPages,
          onTap: () => onPageChanged(currentPage + 1),
        ),
        const SizedBox(width: 4),
        _buildPaginationButton(
          icon: Icons.last_page,
          isEnabled: currentPage < totalPages,
          onTap: () => onPageChanged(totalPages),
        ),
      ],
    ),
  );
}