import 'package:flutter/material.dart'; /// 페이지네이션 위젯 (<< < 1 2 3 ... 10 > >>) /// - totalCount: 전체 아이템 수 /// - currentPage: 현재 페이지 (1부터 시작) /// - pageSize: 페이지당 아이템 수 /// - onPageChanged: 페이지 변경 콜백 class Pagination extends StatelessWidget { final int totalCount; final int currentPage; final int pageSize; final ValueChanged onPageChanged; const Pagination({ Key? key, required this.totalCount, required this.currentPage, required this.pageSize, required this.onPageChanged, }) : super(key: key); @override Widget build(BuildContext context) { // 전체 페이지 수 계산 final int totalPages = (totalCount / pageSize).ceil(); // 페이지네이션 버튼 최대 10개 final int maxButtons = 10; // 시작 페이지 계산 int startPage = ((currentPage - 1) ~/ maxButtons) * maxButtons + 1; int endPage = (startPage + maxButtons - 1).clamp(1, totalPages); List pageButtons = []; for (int i = startPage; i <= endPage; i++) { pageButtons.add( Padding( padding: const EdgeInsets.symmetric(horizontal: 2), child: ElevatedButton( style: ElevatedButton.styleFrom( minimumSize: const Size(36, 36), backgroundColor: i == currentPage ? Colors.blue : Colors.white, foregroundColor: i == currentPage ? Colors.white : Colors.black, padding: EdgeInsets.zero, ), onPressed: i == currentPage ? null : () => onPageChanged(i), child: Text('$i'), ), ), ); } return Row( mainAxisAlignment: MainAxisAlignment.center, children: [ // 가장 처음 페이지로 이동 IconButton( icon: const Icon(Icons.first_page), tooltip: '처음', onPressed: currentPage > 1 ? () => onPageChanged(1) : null, ), // 이전 페이지로 이동 IconButton( icon: const Icon(Icons.chevron_left), tooltip: '이전', onPressed: currentPage > 1 ? () => onPageChanged(currentPage - 1) : null, ), // 페이지 번호 버튼들 ...pageButtons, // 다음 페이지로 이동 IconButton( icon: const Icon(Icons.chevron_right), tooltip: '다음', onPressed: currentPage < totalPages ? () => onPageChanged(currentPage + 1) : null, ), // 마지막 페이지로 이동 IconButton( icon: const Icon(Icons.last_page), tooltip: '마지막', onPressed: currentPage < totalPages ? () => onPageChanged(totalPages) : null, ), ], ); } }