pagination_buttons.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ignore_for_file: must_be_immutable
  2. import 'package:flutter/material.dart';
  3. class PaginationButtons extends StatelessWidget {
  4. final int currentPage;
  5. final int totalPages;
  6. final Function(int) onPageChanged;
  7. Color primaryColor = Colors.blue;
  8. int customPage;
  9. PaginationButtons({
  10. required this.currentPage,
  11. required this.totalPages,
  12. required this.onPageChanged,
  13. }) : customPage = currentPage;
  14. @override
  15. Widget build(BuildContext context) {
  16. Size size = MediaQuery.of(context).size;
  17. bool isBigScreen = size.width >= 900;
  18. primaryColor = Theme.of(context).primaryColor;
  19. List<int> pageNumbers = [];
  20. if (totalPages > 0 && currentPage <= 2) pageNumbers.add(1);
  21. if (currentPage > 2) pageNumbers.add(currentPage - 1);
  22. if (currentPage > 1) pageNumbers.add(currentPage);
  23. if (currentPage < totalPages) pageNumbers.add(currentPage + 1);
  24. if (isBigScreen && currentPage < totalPages - 1) pageNumbers.add(totalPages);
  25. return Column(
  26. mainAxisAlignment: MainAxisAlignment.start,
  27. crossAxisAlignment: CrossAxisAlignment.start,
  28. children: [
  29. Row(
  30. mainAxisAlignment: MainAxisAlignment.center,
  31. crossAxisAlignment: CrossAxisAlignment.center,
  32. children: [
  33. IconButton(
  34. icon: const Icon(Icons.arrow_back),
  35. onPressed: currentPage > 1
  36. ? () => onPageChanged(currentPage - 1)
  37. : null,
  38. ),
  39. ...pageNumbers.map((page) {
  40. return _buildPaginationButton(page);
  41. }),
  42. IconButton(
  43. icon: const Icon(Icons.arrow_forward),
  44. onPressed: currentPage < totalPages
  45. ? () => onPageChanged(currentPage + 1)
  46. : null,
  47. ),
  48. const VerticalDivider(
  49. color: Colors.black,
  50. thickness: 1.0,
  51. ),
  52. if(isBigScreen)
  53. Column(
  54. children: [
  55. const SizedBox(height: 100),
  56. Container(
  57. color: Colors.transparent,
  58. width: 150,
  59. height: 150,
  60. child: TextField(
  61. decoration: InputDecoration(
  62. labelText: 'Página',
  63. hintText: 'número de página',
  64. border: OutlineInputBorder(
  65. borderRadius: BorderRadius.circular(
  66. 8.0), // Ajusta el radio según tu preferencia
  67. ),
  68. contentPadding: EdgeInsets.symmetric(
  69. vertical: 12.0, horizontal: 16.0),
  70. ),
  71. keyboardType: TextInputType.number,
  72. onChanged: onCustomPageChanged,
  73. )
  74. ),
  75. ]
  76. ),
  77. if(isBigScreen)
  78. Padding(
  79. padding: EdgeInsets.only(left: 4),
  80. child: ElevatedButton(
  81. onPressed: () => onPageChanged(customPage),
  82. child: Text('Ir'),
  83. ),
  84. )
  85. ]),
  86. const SizedBox(height: 200),
  87. ]
  88. );
  89. }
  90. Widget _buildPaginationButton(int page) {
  91. return Padding(
  92. padding: const EdgeInsets.all(8.0),
  93. child: Container(
  94. width: 60,
  95. height: 60,
  96. decoration: BoxDecoration(
  97. borderRadius: BorderRadius.circular(10.0),
  98. color: currentPage == page ? primaryColor : Colors.grey,
  99. ),
  100. child: TextButton(
  101. onPressed: () {
  102. onPageChanged(page);
  103. },
  104. child: Text(
  105. page.toString(),
  106. style: const TextStyle(
  107. color: Colors.white,
  108. ),
  109. ),
  110. ),
  111. ),
  112. );
  113. }
  114. void onCustomPageChanged(String value) {
  115. // Validar la entrada del usuario y actualizar el estado
  116. if (value.isNotEmpty) {
  117. customPage = int.parse(value);
  118. } else {
  119. customPage = currentPage;
  120. }
  121. }
  122. }