123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // ignore_for_file: must_be_immutable
- import 'package:flutter/material.dart';
- class PaginationButtons extends StatelessWidget {
- final int currentPage;
- final int totalPages;
- final Function(int) onPageChanged;
- Color primaryColor = Colors.blue;
- int customPage;
- PaginationButtons({
- required this.currentPage,
- required this.totalPages,
- required this.onPageChanged,
- }) : customPage = currentPage;
- @override
- Widget build(BuildContext context) {
- Size size = MediaQuery.of(context).size;
- bool isBigScreen = size.width >= 900;
- primaryColor = Theme.of(context).primaryColor;
- List<int> pageNumbers = [];
- if (totalPages > 0 && currentPage <= 2) pageNumbers.add(1);
- if (currentPage > 2) pageNumbers.add(currentPage - 1);
- if (currentPage > 1) pageNumbers.add(currentPage);
- if (currentPage < totalPages) pageNumbers.add(currentPage + 1);
- if (isBigScreen && currentPage < totalPages - 1) pageNumbers.add(totalPages);
- return Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- IconButton(
- icon: const Icon(Icons.arrow_back),
- onPressed: currentPage > 1
- ? () => onPageChanged(currentPage - 1)
- : null,
- ),
- ...pageNumbers.map((page) {
- return _buildPaginationButton(page);
- }),
- IconButton(
- icon: const Icon(Icons.arrow_forward),
- onPressed: currentPage < totalPages
- ? () => onPageChanged(currentPage + 1)
- : null,
- ),
- const VerticalDivider(
- color: Colors.black,
- thickness: 1.0,
- ),
- if(isBigScreen)
- Column(
- children: [
- const SizedBox(height: 100),
- Container(
- color: Colors.transparent,
- width: 150,
- height: 150,
- child: TextField(
- decoration: InputDecoration(
- labelText: 'Página',
- hintText: 'número de página',
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(
- 8.0), // Ajusta el radio según tu preferencia
- ),
- contentPadding: EdgeInsets.symmetric(
- vertical: 12.0, horizontal: 16.0),
- ),
- keyboardType: TextInputType.number,
- onChanged: onCustomPageChanged,
- )
- ),
- ]
- ),
- if(isBigScreen)
- Padding(
- padding: EdgeInsets.only(left: 4),
- child: ElevatedButton(
- onPressed: () => onPageChanged(customPage),
- child: Text('Ir'),
- ),
- )
- ]),
- const SizedBox(height: 200),
- ]
- );
- }
- Widget _buildPaginationButton(int page) {
- return Padding(
- padding: const EdgeInsets.all(8.0),
- child: Container(
- width: 60,
- height: 60,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(10.0),
- color: currentPage == page ? primaryColor : Colors.grey,
- ),
- child: TextButton(
- onPressed: () {
- onPageChanged(page);
- },
- child: Text(
- page.toString(),
- style: const TextStyle(
- color: Colors.white,
- ),
- ),
- ),
- ),
- );
- }
- void onCustomPageChanged(String value) {
- // Validar la entrada del usuario y actualizar el estado
- if (value.isNotEmpty) {
- customPage = int.parse(value);
- } else {
- customPage = currentPage;
- }
- }
- }
|