@@ -30,8 +30,12 @@ class SourceFilterPage extends StatefulWidget {
30
30
/// Manages the local selection state ([_pageSelectedSources] ) and interacts
31
31
/// with [SourcesFilterBloc] for data fetching and pagination.
32
32
class _SourceFilterPageState extends State <SourceFilterPage > {
33
- /// Stores the sources selected by the user on this page.
34
- /// Initialized from the `extra` parameter passed during navigation.
33
+ /// Stores the sources selected by the user *on this specific page*.
34
+ /// This state is local to the `SourceFilterPage` lifecycle.
35
+ /// It's initialized in `initState` using the list of previously selected
36
+ /// sources 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 <Source > _pageSelectedSources;
36
40
37
41
/// Scroll controller to detect when the user reaches the end of the list
@@ -41,13 +45,27 @@ class _SourceFilterPageState extends State<SourceFilterPage> {
41
45
@override
42
46
void initState () {
43
47
super .initState ();
44
- // Initialize local selections from the data passed via 'extra'
48
+ // Initialization needs to happen after the first frame to safely access
49
+ // GoRouterState.of(context).
45
50
WidgetsBinding .instance.addPostFrameCallback ((_) {
51
+ // 1. Retrieve the list of sources 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.
46
54
final initialSelection = GoRouterState .of (context).extra as List <Source >? ;
55
+
56
+ // 2. Initialize the local selection state (`_pageSelectedSources`) for this
57
+ // page. Use a Set for efficient add/remove/contains operations.
58
+ // This ensures the checkboxes on this page are initially checked
59
+ // correctly based on the selections made previously.
47
60
_pageSelectedSources = Set .from (initialSelection ?? []);
48
- // Request initial sources from the BLoC
61
+
62
+ // 3. Trigger the page-specific BLoC (SourcesFilterBloc) to start
63
+ // fetching the list of *all available* sources that the user can
64
+ // potentially select from. The BLoC handles fetching, pagination,
65
+ // loading states, and errors for the *list of options*.
49
66
context.read <SourcesFilterBloc >().add (SourcesFilterRequested ());
50
67
});
68
+ // Add listener for pagination logic.
51
69
_scrollController.addListener (_onScroll);
52
70
}
53
71
@@ -88,6 +106,10 @@ class _SourceFilterPageState extends State<SourceFilterPage> {
88
106
icon: const Icon (Icons .check),
89
107
tooltip: l10n.headlinesFeedFilterApplyButton,
90
108
onPressed: () {
109
+ // When the user taps 'Apply' (checkmark), pop the current route
110
+ // and return the final list of selected sources (`_pageSelectedSources`)
111
+ // from this page back to the previous page (`HeadlinesFilterPage`).
112
+ // `HeadlinesFilterPage` receives this list in its `onResult` callback.
91
113
context.pop (_pageSelectedSources.toList ());
92
114
},
93
115
),
@@ -185,10 +207,14 @@ class _SourceFilterPageState extends State<SourceFilterPage> {
185
207
: null ,
186
208
value: isSelected,
187
209
onChanged: (bool ? value) {
210
+ // When a checkbox state changes, update the local selection set
211
+ // (`_pageSelectedSources`) for this page.
188
212
setState (() {
189
213
if (value == true ) {
214
+ // Add the source if checked.
190
215
_pageSelectedSources.add (source);
191
216
} else {
217
+ // Remove the source if unchecked.
192
218
_pageSelectedSources.remove (source);
193
219
}
194
220
});
0 commit comments