pile_nav_new/lib/pages/history/component/custom_pager.dart
2024-08-29 17:45:39 +08:00

122 lines
3.9 KiB
Dart

import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
class PageNumber extends StatefulWidget {
const PageNumber({
super.key,
required PaginatorController controller,
}) : _controller = controller;
final PaginatorController _controller;
@override
PageNumberState createState() => PageNumberState();
}
class PageNumberState extends State<PageNumber> {
void update() {
setState(() {});
}
@override
void initState() {
super.initState();
widget._controller.addListener(update);
}
@override
void dispose() {
widget._controller.removeListener(update);
super.dispose();
}
@override
Widget build(BuildContext context) {
// Checking instance id to see if build is called
// on different ones
// Due to some reasons when using this widget
// with AsyncPaginatedDatatable2 the widget is instatiotaed once
// though it's state is created 3 times upon first loading
// of the Custom pager example
// print(identityHashCode(this));
return Text(widget._controller.isAttached
? 'Page: ${1 + ((widget._controller.currentRowIndex + 1) / widget._controller.rowsPerPage).floor()} of '
'${(widget._controller.rowCount / widget._controller.rowsPerPage).ceil()}'
: 'Page: x of y');
}
}
class CustomPager extends StatefulWidget {
const CustomPager(this.controller, {super.key});
final PaginatorController controller;
@override
CustomPagerState createState() => CustomPagerState();
}
class CustomPagerState extends State<CustomPager> {
static const List<int> _availableSizes = [10, 20, 30, 50, 100];
@override
void initState() {
super.initState();
widget.controller.addListener(() {
// setState(() {});
});
}
@override
Widget build(BuildContext context) {
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
// skip this build pass
if (!widget.controller.isAttached) return const SizedBox();
return Container(
height: 40,
margin: const EdgeInsets.fromLTRB(5, 0, 0, 0),
decoration: BoxDecoration(
color: isDarkMode
? Colors.black38
: const Color.fromARGB(255, 230, 228, 228),
borderRadius: BorderRadius.circular(3),
),
child: Theme(
data: Theme.of(context).copyWith(
iconTheme: IconThemeData(
color: isDarkMode ? Colors.white : Colors.black),
textTheme: TextTheme(
titleMedium: TextStyle(
color: isDarkMode ? Colors.white : Colors.black))),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
onPressed: () => widget.controller.goToFirstPage(),
icon: const Icon(Icons.skip_previous)),
IconButton(
onPressed: () => widget.controller.goToPreviousPage(),
icon: const Icon(Icons.chevron_left_sharp)),
DropdownButton<int>(
onChanged: (v) => widget.controller.setRowsPerPage(v!),
value: _availableSizes.contains(widget.controller.rowsPerPage)
? widget.controller.rowsPerPage
: _availableSizes[0],
dropdownColor: Colors.grey,
items: _availableSizes
.map((s) => DropdownMenuItem<int>(
value: s,
child: Text(s.toString()),
))
.toList()),
IconButton(
onPressed: () => widget.controller.goToNextPage(),
icon: const Icon(Icons.chevron_right_sharp)),
IconButton(
onPressed: () => widget.controller.goToLastPage(),
icon: const Icon(Icons.skip_next))
],
)),
);
}
}