pagination_buttons.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. //currentPage==1?Container(): _buildPaginationButton(1),
  40. ...pageNumbers.map((page) {
  41. return _buildPaginationButton(page);
  42. }),
  43. IconButton(
  44. icon: const Icon(Icons.arrow_forward),
  45. onPressed: currentPage < totalPages
  46. ? () => onPageChanged(currentPage + 1)
  47. : null,
  48. ),
  49. const VerticalDivider(
  50. color: Colors.black,
  51. thickness: 1.0,
  52. ),
  53. if(isBigScreen)
  54. Column(
  55. children: [
  56. const SizedBox(height: 100),
  57. Container(
  58. color: Colors.transparent,
  59. width: 150,
  60. height: 150,
  61. child: TextField(
  62. decoration: InputDecoration(
  63. labelText: 'Página',
  64. hintText: 'número de página',
  65. border: OutlineInputBorder(
  66. borderRadius: BorderRadius.circular(
  67. 8.0), // Ajusta el radio según tu preferencia
  68. ),
  69. contentPadding: EdgeInsets.symmetric(
  70. vertical: 12.0, horizontal: 16.0),
  71. ),
  72. keyboardType: TextInputType.number,
  73. onChanged: onCustomPageChanged,
  74. )
  75. ),
  76. ]
  77. ),
  78. if(isBigScreen)
  79. Padding(
  80. padding: EdgeInsets.only(left: 4),
  81. child: ElevatedButton(
  82. onPressed: () => onPageChanged(customPage),
  83. child: Text('Ir'),
  84. ),
  85. )
  86. ]),
  87. const SizedBox(height: 200),
  88. ]
  89. );
  90. }
  91. Widget _buildPaginationButton(int page) {
  92. return Padding(
  93. padding: const EdgeInsets.all(8.0),
  94. child: Container(
  95. width: 60,
  96. height: 60,
  97. decoration: BoxDecoration(
  98. borderRadius: BorderRadius.circular(10.0),
  99. color: currentPage == page ? primaryColor : Colors.grey,
  100. ),
  101. child: TextButton(
  102. onPressed: () {
  103. onPageChanged(page);
  104. },
  105. child: Text(
  106. page.toString(),
  107. style: const TextStyle(
  108. color: Colors.white,
  109. ),
  110. ),
  111. ),
  112. ),
  113. );
  114. }
  115. void onCustomPageChanged(String value) {
  116. // Validar la entrada del usuario y actualizar el estado
  117. if (value.isNotEmpty) {
  118. customPage = int.parse(value);
  119. } else {
  120. customPage = currentPage;
  121. }
  122. }
  123. }