Skip to content

Commit 9432f2f

Browse files
committed
docs: clarify country filter page logic
- Explain init and selection state - Describe data passing via `extra` - Detail the "Apply" button action
1 parent 1c5ac59 commit 9432f2f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

lib/headlines-feed/view/country_filter_page.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class CountryFilterPage extends StatefulWidget {
3030
/// Manages the local selection state ([_pageSelectedCountries]) and interacts
3131
/// with [CountriesFilterBloc] for data fetching and pagination.
3232
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.
3539
late Set<Country> _pageSelectedCountries;
3640

3741
/// Scroll controller to detect when the user reaches the end of the list
@@ -41,14 +45,28 @@ class _CountryFilterPageState extends State<CountryFilterPage> {
4145
@override
4246
void initState() {
4347
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).
4550
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.
4654
final initialSelection =
4755
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.
4861
_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*.
5067
context.read<CountriesFilterBloc>().add(CountriesFilterRequested());
5168
});
69+
// Add listener for pagination logic.
5270
_scrollController.addListener(_onScroll);
5371
}
5472

@@ -89,6 +107,10 @@ class _CountryFilterPageState extends State<CountryFilterPage> {
89107
icon: const Icon(Icons.check),
90108
tooltip: l10n.headlinesFeedFilterApplyButton,
91109
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.
92114
context.pop(_pageSelectedCountries.toList());
93115
},
94116
),
@@ -190,10 +212,14 @@ class _CountryFilterPageState extends State<CountryFilterPage> {
190212
),
191213
value: isSelected,
192214
onChanged: (bool? value) {
215+
// When a checkbox state changes, update the local selection set
216+
// (`_pageSelectedCountries`) for this page.
193217
setState(() {
194218
if (value == true) {
219+
// Add the country if checked.
195220
_pageSelectedCountries.add(country);
196221
} else {
222+
// Remove the country if unchecked.
197223
_pageSelectedCountries.remove(country);
198224
}
199225
});

0 commit comments

Comments
 (0)