From 128c62fba23ed31c4a9b7cb18a19a5b759babb3b Mon Sep 17 00:00:00 2001 From: aakrem Date: Wed, 6 Jul 2022 13:03:14 +0200 Subject: [PATCH] exclude countries --- README.md | 1 + lib/country_code_picker.dart | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index f5b73030..954176db 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ Here is a list of properties available to customize your widget: |textOverflow| TextOverflow | the button text overflow behaviour | |dialogSize| Size | the size of the selection dialog | |countryFilter| List | uses a list of strings to filter a sublist of countries | +|excludeCountries| List | uses a list of strings to exclude a sublist of countries | |showOnlyCountryWhenClosed| bool | if true it'll show only the country | |alignLeft| bool | aligns the flag and the Text to the left | |showFlag| bool | shows the flag everywhere | diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index c979e12b..40be298f 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -46,6 +46,9 @@ class CountryCodePicker extends StatefulWidget { /// used to customize the country list final List? countryFilter; + /// used to exclude some countries + final List? excludeCountries; + /// shows the name of the country instead of the dialcode final bool showOnlyCountryWhenClosed; @@ -112,6 +115,7 @@ class CountryCodePicker extends StatefulWidget { this.boxDecoration, this.comparator, this.countryFilter, + this.excludeCountries, this.hideSearch = false, this.showDropDownButton = false, this.dialogSize, @@ -143,6 +147,20 @@ class CountryCodePicker extends StatefulWidget { .toList(); } + if (excludeCountries != null && excludeCountries!.isNotEmpty) { + final uppercaseCustomList = + excludeCountries!.map((c) => c.toUpperCase()).toList(); + + final index = elements.indexWhere(((c) => + uppercaseCustomList.contains(c.code) || + uppercaseCustomList.contains(c.name) || + uppercaseCustomList.contains(c.dialCode))); + + if (index >= 0) { + elements.removeAt(index); + } + } + return CountryCodePickerState(elements); } }