import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'main_card.dart'; class Pagination extends StatefulWidget { final ValueChanged onValueChanged; final int total; final int totalPages; final int currentPage; const Pagination( {super.key, required this.onValueChanged, required this.total, required this.totalPages, required this.currentPage}); @override State createState() => _PaginationState(); } class _PaginationState extends State { TextEditingController _pageController = TextEditingController(); int _currentPage = 1; // 当前页码 int _totalPages = 0; // 假设总页数为 5// 下拉框选项 int _total = 0; @override void initState() { super.initState(); _pageController.text = _currentPage.toString(); // 添加监听器 _pageController.addListener(() { print('监听器'); if (_pageController.text.isNotEmpty) { print(_pageController.text); print(_pageController.text.runtimeType); int newPage = int.tryParse(_pageController.text)!; if (newPage != null && newPage != _currentPage) { setState(() { _currentPage = newPage; _onPageChanged(_currentPage); // 进行查询 // _getOrders(_currentPage); // 如果需要获取订单可以调用这个 }); } } }); } @override void dispose() { _pageController.dispose(); // 记得在 dispose 中销毁控制器 super.dispose(); } void _prev() { if (_currentPage > 1) { setState(() { _onPageChanged(_currentPage - 1); _currentPage--; }); // _getOrders(_currentPage); } } void _next() { if (_currentPage < _totalPages) { setState(() { _onPageChanged(_currentPage + 1); _currentPage++; }); // _getOrders(_currentPage); } } void _onPageChanged(int newPage) { setState(() { _currentPage = newPage; _pageController.text = _currentPage.toString(); // 更新输入框内容 }); // 调用回调方法,将新的页码传回 widget.onValueChanged(_currentPage); } @override Widget build(BuildContext context) { _currentPage = widget.currentPage; _totalPages = widget.totalPages; _total = widget.total; return Container( constraints: BoxConstraints(minHeight: 50), decoration: BoxDecoration( color: Colors.white, border: Border(top: BorderSide(color: Colors.grey, width: 0.1)), // boxShadow: [ // BoxShadow( // color: Colors.grey.withOpacity(0.5), // spreadRadius: 2, // blurRadius: 5, // offset: Offset(0, 3), // changes position of shadow 3 // ), // ], ), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Container( constraints: BoxConstraints( minHeight: 40, minWidth: 40, ), decoration: BoxDecoration( border: Border.all(color: Colors.grey, width: 1), borderRadius: BorderRadius.circular(5), ), child: Material( color: Colors.transparent, child: InkWell( child: Icon(Icons.keyboard_arrow_left), onTap: _currentPage > 1 ? _prev : null, ), ), ), SizedBox(width: 5), Container( child: Row( children: [ Text('第'), SizedBox(width: 5), Container( constraints: BoxConstraints( maxWidth: 50, maxHeight: 40, minWidth: 40), decoration: BoxDecoration( border: Border.all(color: Colors.grey, width: 1), borderRadius: BorderRadius.circular(5), ), child: Center( child: TextField( textAlign: TextAlign.center, controller: _pageController, onChanged: (value) { if (value.isNotEmpty) { int page = int.parse(value); if (page > 0 && page <= _totalPages) { setState(() { _currentPage = page; }); // _getOrders(_currentPage, 20); } } }, style: TextStyle(fontSize: 10), decoration: InputDecoration( isCollapsed: true, border: InputBorder.none, contentPadding: EdgeInsets.symmetric( vertical: 0, horizontal: 2), // 调整内边距 ), ), )), SizedBox(width: 5), Text('页'), Text('/$_totalPages 页'), ], ), ), // Text('第 $_currentPage 页 / $_totalPages 页'), SizedBox(width: 10), Text('共 ${_total} 条'), SizedBox(width: 10), Container( constraints: BoxConstraints( minHeight: 40, minWidth: 40, ), decoration: BoxDecoration( border: Border.all(color: Colors.grey, width: 1), borderRadius: BorderRadius.circular(5), ), child: Material( color: Colors.transparent, child: InkWell( child: Icon(Icons.keyboard_arrow_right), onTap: _currentPage < _totalPages ? _next : null, ), ), ), ], ), ); } }