@@ -30,8 +30,12 @@ class CountryFilterPage extends StatefulWidget {
30
30
/// Manages the local selection state ([_pageSelectedCountries] ) and interacts
31
31
/// with [CountriesFilterBloc] for data fetching and pagination.
32
32
class _CountryFilterPageState extends State <CountryFilterPage > {
33
- /// Stores the countries selected by the user on this page.
34
- /// Initialized from the `extra` parameter passed during navigation.
33
+ /// Stores the countries selected by the user *on this specific page*.
34
+ /// This state is local to the `CountryFilterPage` lifecycle.
35
+ /// It's initialized in `initState` using the list of previously selected
36
+ /// countries 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 <Country > _pageSelectedCountries;
36
40
37
41
/// Scroll controller to detect when the user reaches the end of the list
@@ -41,14 +45,28 @@ class _CountryFilterPageState extends State<CountryFilterPage> {
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 countries 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 =
47
55
GoRouterState .of (context).extra as List <Country >? ;
56
+
57
+ // 2. Initialize the local selection state (`_pageSelectedCountries`) 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.
48
61
_pageSelectedCountries = Set .from (initialSelection ?? []);
49
- // Request initial countries from the BLoC
62
+
63
+ // 3. Trigger the page-specific BLoC (CountriesFilterBloc) to start
64
+ // fetching the list of *all available* countries that the user can
65
+ // potentially select from. The BLoC handles fetching, pagination,
66
+ // loading states, and errors for the *list of options*.
50
67
context.read <CountriesFilterBloc >().add (CountriesFilterRequested ());
51
68
});
69
+ // Add listener for pagination logic.
52
70
_scrollController.addListener (_onScroll);
53
71
}
54
72
@@ -89,6 +107,10 @@ class _CountryFilterPageState extends State<CountryFilterPage> {
89
107
icon: const Icon (Icons .check),
90
108
tooltip: l10n.headlinesFeedFilterApplyButton,
91
109
onPressed: () {
110
+ // When the user taps 'Apply' (checkmark), pop the current route
111
+ // and return the final list of selected countries (`_pageSelectedCountries`)
112
+ // from this page back to the previous page (`HeadlinesFilterPage`).
113
+ // `HeadlinesFilterPage` receives this list in its `onResult` callback.
92
114
context.pop (_pageSelectedCountries.toList ());
93
115
},
94
116
),
@@ -190,10 +212,14 @@ class _CountryFilterPageState extends State<CountryFilterPage> {
190
212
),
191
213
value: isSelected,
192
214
onChanged: (bool ? value) {
215
+ // When a checkbox state changes, update the local selection set
216
+ // (`_pageSelectedCountries`) for this page.
193
217
setState (() {
194
218
if (value == true ) {
219
+ // Add the country if checked.
195
220
_pageSelectedCountries.add (country);
196
221
} else {
222
+ // Remove the country if unchecked.
197
223
_pageSelectedCountries.remove (country);
198
224
}
199
225
});
0 commit comments