Skip to content

Commit

Permalink
add exclude countries option
Browse files Browse the repository at this point in the history
  • Loading branch information
ThickLine committed May 13, 2024
1 parent 20335fc commit 4eeb84b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class _HomePageState extends State<HomePage> {
hideSearch: true,
backButton: Container(),
selectedCountryIsoCode: selectedCountry?.iso2Code,
excludedCountryCodes: ['AX', 'US', 'MX'],
title: 'Select your country',
searchField: Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down
6 changes: 6 additions & 0 deletions lib/src/services/country_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CountryService {
List<Country> filterAndSortCountries({
List<String>? overrideCountryCodes,
List<String>? priorityCountryCodes,
List<String>? excludedCountryCodes,
Country? selectedCountry,
}) {
var countries = List<Country>.from(allCountries);
Expand All @@ -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(
Expand Down
18 changes: 10 additions & 8 deletions lib/src/services/pick_country_lookup_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import 'package:pick_country_picker/src/data/country_codes.dart';
class PickCountryLookupService {
final List<Country> _allCountries;

PickCountryLookupService()
: _allCountries =
countryCodes.map((code) => Country.fromJson(code)).toList();
PickCountryLookupService({List<String>? 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());
}

Expand Down
43 changes: 23 additions & 20 deletions lib/src/widgets/country_picker_modal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand All @@ -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<String>? 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<String>? excludedCountryCodes;

/// Controls whether to display the search field. If true, users can only scroll to find countries.
final bool hideSearch;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -136,10 +139,12 @@ class CountryPickerModalState extends State<CountryPickerModal> {

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
Expand Down Expand Up @@ -168,8 +173,6 @@ class CountryPickerModalState extends State<CountryPickerModal> {
_filteredCountries = List<Country>.from(_countries);
}



void _filterCountries(String query) {
setState(() {
_filteredCountries = _countries
Expand Down Expand Up @@ -217,21 +220,21 @@ class CountryPickerModalState extends State<CountryPickerModal> {
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(),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 4eeb84b

Please sign in to comment.