From 4eeb84b17d4e61f511661479e2a9a3970b1da2f8 Mon Sep 17 00:00:00 2001 From: Aigars Treijs Date: Mon, 13 May 2024 14:47:25 +0300 Subject: [PATCH] add exclude countries option --- CHANGELOG.md | 6 +++ README.md | 3 +- example/lib/main.dart | 1 + lib/src/services/country_service.dart | 6 +++ .../services/pick_country_lookup_service.dart | 18 ++++---- lib/src/widgets/country_picker_modal.dart | 43 ++++++++++--------- pubspec.yaml | 2 +- 7 files changed, 49 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97f2e37..2516bfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,9 @@ * Code Cleanup: Improved code efficiency by removing unused code and optimizing existing functionalities. * Documentation: Added a practical example to the README to help new users get started more easily. + +## 1.0.0 +* Release + +## 1.1.0 +* Added `excludedCountryCodes` option to exclude specific countries from code execution diff --git a/README.md b/README.md index 2982147..a5ac22b 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ A highly customizable modal widget for Flutter that allows users to pick a count Add this to your package's pubspec.yaml file: ```dart dependencies: - pick_country_picker: ^1.0.0 + pick_country_picker: ^1.1.0 ``` 2. Install it You can install packages from the command line: @@ -81,6 +81,7 @@ CountryPickerModal( }, selectedIcon: Icon(Icons.check_circle, color: Colors.green), overrideCountryCodes: ['US', 'CA', 'GB'], + excludedCountryCodes: ['AX', 'USA', 'MX'], hideSearch: false, hideCloseIcon: false, backButton: Icon(Icons.arrow_back), diff --git a/example/lib/main.dart b/example/lib/main.dart index 3774b4b..74884ed 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -39,6 +39,7 @@ class _HomePageState extends State { hideSearch: true, backButton: Container(), selectedCountryIsoCode: selectedCountry?.iso2Code, + excludedCountryCodes: ['AX', 'US', 'MX'], title: 'Select your country', searchField: Row( mainAxisAlignment: MainAxisAlignment.center, diff --git a/lib/src/services/country_service.dart b/lib/src/services/country_service.dart index 7ab4358..53f7796 100644 --- a/lib/src/services/country_service.dart +++ b/lib/src/services/country_service.dart @@ -11,6 +11,7 @@ class CountryService { List filterAndSortCountries({ List? overrideCountryCodes, List? priorityCountryCodes, + List? excludedCountryCodes, Country? selectedCountry, }) { var countries = List.from(allCountries); @@ -31,6 +32,11 @@ class CountryService { countries = priorityCountries + otherCountries; } + if (excludedCountryCodes != null) { + countries.removeWhere( + (country) => excludedCountryCodes.contains(country.iso2Code)); + } + if (selectedCountry != null) { final selectedIndex = countries .indexWhere( diff --git a/lib/src/services/pick_country_lookup_service.dart b/lib/src/services/pick_country_lookup_service.dart index 438bde4..16d5705 100644 --- a/lib/src/services/pick_country_lookup_service.dart +++ b/lib/src/services/pick_country_lookup_service.dart @@ -5,21 +5,23 @@ import 'package:pick_country_picker/src/data/country_codes.dart'; class PickCountryLookupService { final List _allCountries; - PickCountryLookupService() - : _allCountries = - countryCodes.map((code) => Country.fromJson(code)).toList(); + PickCountryLookupService({List? excludedCountryCodes}) + : _allCountries = countryCodes + .map((code) => Country.fromJson(code)) + .where((country) => + excludedCountryCodes == null || + !excludedCountryCodes.contains(country.iso2Code)) + .toList(); -// Fetch a country by its ISO code + // Fetch a country by its ISO code Country? getCountryByIsoCode(String isoCode) { - return _allCountries - .firstWhereOrNull( + return _allCountries.firstWhereOrNull( (country) => country.iso2Code.toLowerCase() == isoCode.toLowerCase()); } // Fetch a country by its country code Country? getCountryByCountryCode(String countryCode) { - return _allCountries - .firstWhereOrNull((country) => + return _allCountries.firstWhereOrNull((country) => country.countryCode.toLowerCase() == countryCode.toLowerCase()); } diff --git a/lib/src/widgets/country_picker_modal.dart b/lib/src/widgets/country_picker_modal.dart index 2c5fed3..92dad13 100644 --- a/lib/src/widgets/country_picker_modal.dart +++ b/lib/src/widgets/country_picker_modal.dart @@ -5,7 +5,6 @@ import 'package:pick_country_picker/src/services/country_service.dart'; import 'package:pick_country_picker/src/widgets/country_list_widget.dart'; import 'package:pick_country_picker/src/widgets/search_field_widget.dart'; - /// A modal widget that allows the user to pick a country from a list. /// /// Offers extensive customization options, including overriding the default country list, @@ -15,12 +14,11 @@ class CountryPickerModal extends StatefulWidget { /// Passes the newly selected [Country] object to the caller for further processing. final Function(Country) onCountryChanged; -/// An optional ISO 3166-1 alpha-2 code or country code to pre-select a country. + /// An optional ISO 3166-1 alpha-2 code or country code to pre-select a country. /// If provided, the specified country will be highlighted and placed at the top of the list. final Country? selectedCountry; - -/// Optional parameter to specify the country to be pre-selected using its ISO 3166-1 alpha-2 + /// Optional parameter to specify the country to be pre-selected using its ISO 3166-1 alpha-2 /// representation (e.g., 'US' for United States, 'CA' for Canada). final String? selectedCountryCode; @@ -44,6 +42,10 @@ class CountryPickerModal extends StatefulWidget { /// Use this to restrict the selection to a specific set of countries, overriding the default list. final List? overrideCountryCodes; + /// Country ISO codes to exclusively remove from a picker. + /// Use this to restrict the selection to a specific set of countries, overriding the default list. + final List? excludedCountryCodes; + /// Controls whether to display the search field. If true, users can only scroll to find countries. final bool hideSearch; @@ -102,6 +104,7 @@ class CountryPickerModal extends StatefulWidget { this.title = 'Select Country', this.priorityCountryCodes, this.overrideCountryCodes, + this.excludedCountryCodes, this.hideSearch = false, this.hideCloseIcon = false, this.useCupertinoModal = false, @@ -136,10 +139,12 @@ class CountryPickerModalState extends State { void _initializeCountries() { final countryService = CountryService(); - final pickCountryLookupService = PickCountryLookupService(); + final pickCountryLookupService = PickCountryLookupService( + excludedCountryCodes: widget.excludedCountryCodes); _countries = countryService.filterAndSortCountries( overrideCountryCodes: widget.overrideCountryCodes, priorityCountryCodes: widget.priorityCountryCodes, + excludedCountryCodes: widget.excludedCountryCodes, ); // Determine the initial country based on the provided properties @@ -168,8 +173,6 @@ class CountryPickerModalState extends State { _filteredCountries = List.from(_countries); } - - void _filterCountries(String query) { setState(() { _filteredCountries = _countries @@ -217,21 +220,21 @@ class CountryPickerModalState extends State { child: SafeArea( child: Column( children: [ - Padding( - padding: const EdgeInsets.symmetric( - horizontal: 16.0, vertical: 8.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, + Padding( + padding: + const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Expanded(child: _buildSearchField()), - widget.backButton ?? - CupertinoButton( - child: Text(widget.cancelText), - onPressed: () => Navigator.of(context).pop(), - ), - ], - ), + Expanded(child: _buildSearchField()), + widget.backButton ?? + CupertinoButton( + child: Text(widget.cancelText), + onPressed: () => Navigator.of(context).pop(), + ), + ], ), + ), Expanded( child: _buildCountryPickerList(), ), diff --git a/pubspec.yaml b/pubspec.yaml index 1515aab..3ff0cd2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: pick_country_picker description: A highly customizable Flutter package for selecting a country from a modal list. -version: 1.0.0 +version: 1.1.0 homepage: repository: https://github.com/ThickLine/pick_country_picker