@@ -6,6 +6,7 @@ import 'package:ht_dashboard/content_management/bloc/content_management_bloc.dar
6
6
import 'package:ht_dashboard/l10n/app_localizations.dart' ;
7
7
import 'package:ht_dashboard/l10n/l10n.dart' ;
8
8
import 'package:ht_dashboard/router/routes.dart' ;
9
+ import 'package:ht_dashboard/shared/constants/pagination_constants.dart' ;
9
10
import 'package:ht_dashboard/shared/constants/app_spacing.dart' ;
10
11
import 'package:ht_dashboard/shared/widgets/failure_state_widget.dart' ;
11
12
import 'package:ht_dashboard/shared/widgets/loading_state_widget.dart' ;
@@ -23,14 +24,12 @@ class CategoriesPage extends StatefulWidget {
23
24
}
24
25
25
26
class _CategoriesPageState extends State <CategoriesPage > {
26
- static const int _rowsPerPage = 10 ;
27
-
28
27
@override
29
28
void initState () {
30
29
super .initState ();
31
30
context.read <ContentManagementBloc >().add (
32
- const LoadCategoriesRequested (limit: _rowsPerPage ),
33
- );
31
+ const LoadCategoriesRequested (limit: kDefaultRowsPerPage ),
32
+ );
34
33
}
35
34
36
35
@override
@@ -53,8 +52,8 @@ class _CategoriesPageState extends State<CategoriesPage> {
53
52
return FailureStateWidget (
54
53
message: state.errorMessage ?? l10n.unknownError,
55
54
onRetry: () => context.read <ContentManagementBloc >().add (
56
- const LoadCategoriesRequested (limit: _rowsPerPage ),
57
- ),
55
+ const LoadCategoriesRequested (limit: kDefaultRowsPerPage ),
56
+ ),
58
57
);
59
58
}
60
59
@@ -82,20 +81,21 @@ class _CategoriesPageState extends State<CategoriesPage> {
82
81
source: _CategoriesDataSource (
83
82
context: context,
84
83
categories: state.categories,
84
+ hasMore: state.categoriesHasMore,
85
85
l10n: l10n,
86
86
),
87
- rowsPerPage: _rowsPerPage ,
88
- availableRowsPerPage: const [_rowsPerPage ],
87
+ rowsPerPage: kDefaultRowsPerPage ,
88
+ availableRowsPerPage: const [kDefaultRowsPerPage ],
89
89
onPageChanged: (pageIndex) {
90
- final newOffset = pageIndex * _rowsPerPage ;
90
+ final newOffset = pageIndex * kDefaultRowsPerPage ;
91
91
if (newOffset >= state.categories.length &&
92
92
state.categoriesHasMore) {
93
93
context.read <ContentManagementBloc >().add (
94
- LoadCategoriesRequested (
95
- startAfterId: state.categoriesCursor,
96
- limit: _rowsPerPage ,
97
- ),
98
- );
94
+ LoadCategoriesRequested (
95
+ startAfterId: state.categoriesCursor,
96
+ limit: kDefaultRowsPerPage ,
97
+ ),
98
+ );
99
99
}
100
100
},
101
101
empty: Center (child: Text (l10n.noCategoriesFound)),
@@ -117,16 +117,22 @@ class _CategoriesDataSource extends DataTableSource {
117
117
_CategoriesDataSource ({
118
118
required this .context,
119
119
required this .categories,
120
+ required this .hasMore,
120
121
required this .l10n,
121
122
});
122
123
123
124
final BuildContext context;
124
125
final List <Category > categories;
126
+ final bool hasMore;
125
127
final AppLocalizations l10n;
126
128
127
129
@override
128
130
DataRow ? getRow (int index) {
129
131
if (index >= categories.length) {
132
+ // This can happen if hasMore is true and the user is on the last page.
133
+ // The table will try to build one extra row that is out of bounds.
134
+ // We return null to signify the end of the available data.
135
+ // The onPageChanged callback will handle fetching more data.
130
136
return null ;
131
137
}
132
138
final category = categories[index];
@@ -152,8 +158,8 @@ class _CategoriesDataSource extends DataTableSource {
152
158
onPressed: () {
153
159
// Dispatch delete event
154
160
context.read <ContentManagementBloc >().add (
155
- DeleteCategoryRequested (category.id),
156
- );
161
+ DeleteCategoryRequested (category.id),
162
+ );
157
163
},
158
164
),
159
165
],
@@ -164,10 +170,18 @@ class _CategoriesDataSource extends DataTableSource {
164
170
}
165
171
166
172
@override
167
- bool get isRowCountApproximate => false ;
173
+ bool get isRowCountApproximate => true ;
168
174
169
175
@override
170
- int get rowCount => categories.length;
176
+ int get rowCount {
177
+ // If we have more items to fetch, we add 1 to the current length.
178
+ // This signals to PaginatedDataTable2 that there is at least one more page,
179
+ // which enables the 'next page' button.
180
+ if (hasMore) {
181
+ return categories.length + 1 ;
182
+ }
183
+ return categories.length;
184
+ }
171
185
172
186
@override
173
187
int get selectedRowCount => 0 ;
0 commit comments