-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpagination_controller.dart
49 lines (42 loc) · 1.01 KB
/
pagination_controller.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'package:flutter/material.dart';
import 'package:insta_digin/utils/custom_print.dart';
class PaginationController {
int currentPage;
int totalPages;
ScrollController? scrollController;
Function fetchDataOnNewPage;
PaginationController({
this.currentPage = 1,
this.totalPages = 1,
required this.fetchDataOnNewPage,
}) {
try {
scrollController?.dispose();
scrollController = ScrollController();
attachListenerOnScrollController();
} catch (e) {
printErr("Scroll controller: $e");
}
}
void reset() {
currentPage = 1;
totalPages = 1;
}
void attachListenerOnScrollController() {
scrollController!.addListener(() async {
if (scrollController!.position.pixels ==
scrollController!.position.maxScrollExtent) {
fetchDataOnNewPage();
}
});
}
void incrementCurrentPage() {
currentPage++;
}
void decrementCurrentPage() {
currentPage--;
}
bool isValidPage() {
return currentPage <= totalPages;
}
}