@@ -30,8 +30,12 @@ class CategoryFilterPage extends StatefulWidget {
30
30
/// Manages the local selection state ([_pageSelectedCategories] ) and interacts
31
31
/// with [CategoriesFilterBloc] for data fetching and pagination.
32
32
class _CategoryFilterPageState extends State <CategoryFilterPage > {
33
- /// Stores the categories selected by the user on this page.
34
- /// Initialized from the `extra` parameter passed during navigation.
33
+ /// Stores the categories selected by the user *on this specific page*.
34
+ /// This state is local to the `CategoryFilterPage` lifecycle.
35
+ /// It's initialized in `initState` using the list of previously selected
36
+ /// categories passed via the `extra` parameter during navigation from
37
+ /// `HeadlinesFilterPage` . This ensures the checkboxes reflect the state
38
+ /// from the main filter page when this page loads.
35
39
late Set <Category > _pageSelectedCategories;
36
40
37
41
/// Scroll controller to detect when the user reaches the end of the list
@@ -41,15 +45,28 @@ class _CategoryFilterPageState extends State<CategoryFilterPage> {
41
45
@override
42
46
void initState () {
43
47
super .initState ();
44
- // Initialize local selections from the data passed via 'extra'
45
- // Need to access GoRouterState *after* the first frame
48
+ // Initialization needs to happen after the first frame to safely access
49
+ // GoRouterState.of(context).
46
50
WidgetsBinding .instance.addPostFrameCallback ((_) {
51
+ // 1. Retrieve the list of categories that were already selected on the
52
+ // previous page (HeadlinesFilterPage). This list is passed dynamically
53
+ // via the `extra` parameter in the `context.pushNamed` call.
47
54
final initialSelection =
48
55
GoRouterState .of (context).extra as List <Category >? ;
56
+
57
+ // 2. Initialize the local selection state (`_pageSelectedCategories`) for this
58
+ // page. Use a Set for efficient add/remove/contains operations.
59
+ // This ensures the checkboxes on this page are initially checked
60
+ // correctly based on the selections made previously.
49
61
_pageSelectedCategories = Set .from (initialSelection ?? []);
50
- // Request initial categories from the BLoC
62
+
63
+ // 3. Trigger the page-specific BLoC (CategoriesFilterBloc) to start
64
+ // fetching the list of *all available* categories that the user can
65
+ // potentially select from. The BLoC handles fetching, pagination,
66
+ // loading states, and errors for the *list of options*.
51
67
context.read <CategoriesFilterBloc >().add (CategoriesFilterRequested ());
52
68
});
69
+ // Add listener for pagination logic.
53
70
_scrollController.addListener (_onScroll);
54
71
}
55
72
@@ -92,7 +109,10 @@ class _CategoryFilterPageState extends State<CategoryFilterPage> {
92
109
icon: const Icon (Icons .check),
93
110
tooltip: l10n.headlinesFeedFilterApplyButton,
94
111
onPressed: () {
95
- // Pop with the final selection from this page
112
+ // When the user taps 'Apply' (checkmark), pop the current route
113
+ // and return the final list of selected categories (`_pageSelectedCategories`)
114
+ // from this page back to the previous page (`HeadlinesFilterPage`).
115
+ // `HeadlinesFilterPage` receives this list in its `onResult` callback.
96
116
context.pop (_pageSelectedCategories.toList ());
97
117
},
98
118
),
@@ -205,10 +225,14 @@ class _CategoryFilterPageState extends State<CategoryFilterPage> {
205
225
: null ,
206
226
value: isSelected,
207
227
onChanged: (bool ? value) {
228
+ // When a checkbox state changes, update the local selection set
229
+ // (`_pageSelectedCategories`) for this page.
208
230
setState (() {
209
231
if (value == true ) {
232
+ // Add the category if checked.
210
233
_pageSelectedCategories.add (category);
211
234
} else {
235
+ // Remove the category if unchecked.
212
236
_pageSelectedCategories.remove (category);
213
237
}
214
238
});
0 commit comments