@@ -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' ; // Corrected import
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/utils/date_formatter.dart' ;
11
12
import 'package:ht_dashboard/shared/widgets/failure_state_widget.dart' ;
@@ -24,14 +25,12 @@ class HeadlinesPage extends StatefulWidget {
24
25
}
25
26
26
27
class _HeadlinesPageState extends State <HeadlinesPage > {
27
- static const int _rowsPerPage = 10 ;
28
-
29
28
@override
30
29
void initState () {
31
30
super .initState ();
32
31
context.read <ContentManagementBloc >().add (
33
- const LoadHeadlinesRequested (limit: _rowsPerPage ),
34
- );
32
+ const LoadHeadlinesRequested (limit: kDefaultRowsPerPage ),
33
+ );
35
34
}
36
35
37
36
@override
@@ -54,8 +53,8 @@ class _HeadlinesPageState extends State<HeadlinesPage> {
54
53
return FailureStateWidget (
55
54
message: state.errorMessage ?? l10n.unknownError,
56
55
onRetry: () => context.read <ContentManagementBloc >().add (
57
- const LoadHeadlinesRequested (limit: _rowsPerPage ),
58
- ),
56
+ const LoadHeadlinesRequested (limit: kDefaultRowsPerPage ),
57
+ ),
59
58
);
60
59
}
61
60
@@ -87,20 +86,21 @@ class _HeadlinesPageState extends State<HeadlinesPage> {
87
86
source: _HeadlinesDataSource (
88
87
context: context,
89
88
headlines: state.headlines,
89
+ hasMore: state.headlinesHasMore,
90
90
l10n: l10n,
91
91
),
92
- rowsPerPage: _rowsPerPage ,
93
- availableRowsPerPage: const [_rowsPerPage ],
92
+ rowsPerPage: kDefaultRowsPerPage ,
93
+ availableRowsPerPage: const [kDefaultRowsPerPage ],
94
94
onPageChanged: (pageIndex) {
95
- final newOffset = pageIndex * _rowsPerPage ;
95
+ final newOffset = pageIndex * kDefaultRowsPerPage ;
96
96
if (newOffset >= state.headlines.length &&
97
97
state.headlinesHasMore) {
98
98
context.read <ContentManagementBloc >().add (
99
- LoadHeadlinesRequested (
100
- startAfterId: state.headlinesCursor,
101
- limit: _rowsPerPage ,
102
- ),
103
- );
99
+ LoadHeadlinesRequested (
100
+ startAfterId: state.headlinesCursor,
101
+ limit: kDefaultRowsPerPage ,
102
+ ),
103
+ );
104
104
}
105
105
},
106
106
empty: Center (child: Text (l10n.noHeadlinesFound)),
@@ -122,16 +122,22 @@ class _HeadlinesDataSource extends DataTableSource {
122
122
_HeadlinesDataSource ({
123
123
required this .context,
124
124
required this .headlines,
125
+ required this .hasMore,
125
126
required this .l10n,
126
127
});
127
128
128
129
final BuildContext context;
129
130
final List <Headline > headlines;
131
+ final bool hasMore;
130
132
final AppLocalizations l10n;
131
133
132
134
@override
133
135
DataRow ? getRow (int index) {
134
136
if (index >= headlines.length) {
137
+ // This can happen if hasMore is true and the user is on the last page.
138
+ // The table will try to build one extra row that is out of bounds.
139
+ // We return null to signify the end of the available data.
140
+ // The onPageChanged callback will handle fetching more data.
135
141
return null ;
136
142
}
137
143
final headline = headlines[index];
@@ -164,8 +170,8 @@ class _HeadlinesDataSource extends DataTableSource {
164
170
onPressed: () {
165
171
// Dispatch delete event
166
172
context.read <ContentManagementBloc >().add (
167
- DeleteHeadlineRequested (headline.id),
168
- );
173
+ DeleteHeadlineRequested (headline.id),
174
+ );
169
175
},
170
176
),
171
177
],
@@ -176,10 +182,18 @@ class _HeadlinesDataSource extends DataTableSource {
176
182
}
177
183
178
184
@override
179
- bool get isRowCountApproximate => false ;
185
+ bool get isRowCountApproximate => true ;
180
186
181
187
@override
182
- int get rowCount => headlines.length;
188
+ int get rowCount {
189
+ // If we have more items to fetch, we add 1 to the current length.
190
+ // This signals to PaginatedDataTable2 that there is at least one more page,
191
+ // which enables the 'next page' button.
192
+ if (hasMore) {
193
+ return headlines.length + 1 ;
194
+ }
195
+ return headlines.length;
196
+ }
183
197
184
198
@override
185
199
int get selectedRowCount => 0 ;
0 commit comments