diff --git a/CHANGELOG.md b/CHANGELOG.md index ac11db7..86d2a21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +## [1.1.2] - 11 Oct 2020 + +* Add support for Greek localization. +* At search change contains to startsWith. +* Add country filter option. + - Can be used to uses filter the countries list (optional). + - It takes a list of country code(iso2). + - Can't provide both exclude and countryFilter +```Dart +showCountryPicker( + context: context, + countryFilter: ['AT', 'GB', 'DK', 'DE', 'FR', 'GR'], //It takes a list of country code(iso2). + onSelect: (Country country) => print('Select country: ${country.displayName}'), +); +``` + ## [1.1.1] - 24 Sep 2020 * Search on localizations. diff --git a/README.md b/README.md index 1906214..9bc4bff 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ A flutter package to select a country from a list of countries. Add the package to your pubspec.yaml: ```yaml - country_picker: ^1.1.1 + country_picker: ^1.1.2 ``` In your dart file, import the library: @@ -36,6 +36,7 @@ Add the `CountryLocalizations.delegate` in the list of your app delegates. MaterialApp( supportedLocales: [ const Locale('en'), + const Locale('el'), const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // Generic Simplified Chinese 'zh_Hans' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // Generic traditional Chinese 'zh_Hant' ], @@ -51,7 +52,7 @@ MaterialApp( ### Parameters: * `onSelect`: Called when a country is select. The country picker passes the new value to the callback (required) * `showPhoneCode`: Can be used to to show phone code before the country name. -* `exclude`: Can be used to exclude(remove) one ore more country from the countries list (optional). +* `exclude`: Can be used to exclude(remove) one or more country from the countries list (optional). ```Dart showCountryPicker( context: context, @@ -59,6 +60,9 @@ showCountryPicker( onSelect: (Country country) => print('Select country: ${country.displayName}'), ); ``` +* `countryFilter`: Can be used to uses filter the countries list (optional). + - It takes a list of country code(iso2). + - Can't provide both exclude and countryFilter ## Contributions Contributions of any kind are more than welcome! Feel free to fork and improve country_code_picker in any way you want, make a pull request, or open an issue. diff --git a/example/README.md b/example/README.md index 1906214..9bc4bff 100644 --- a/example/README.md +++ b/example/README.md @@ -11,7 +11,7 @@ A flutter package to select a country from a list of countries. Add the package to your pubspec.yaml: ```yaml - country_picker: ^1.1.1 + country_picker: ^1.1.2 ``` In your dart file, import the library: @@ -36,6 +36,7 @@ Add the `CountryLocalizations.delegate` in the list of your app delegates. MaterialApp( supportedLocales: [ const Locale('en'), + const Locale('el'), const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // Generic Simplified Chinese 'zh_Hans' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // Generic traditional Chinese 'zh_Hant' ], @@ -51,7 +52,7 @@ MaterialApp( ### Parameters: * `onSelect`: Called when a country is select. The country picker passes the new value to the callback (required) * `showPhoneCode`: Can be used to to show phone code before the country name. -* `exclude`: Can be used to exclude(remove) one ore more country from the countries list (optional). +* `exclude`: Can be used to exclude(remove) one or more country from the countries list (optional). ```Dart showCountryPicker( context: context, @@ -59,6 +60,9 @@ showCountryPicker( onSelect: (Country country) => print('Select country: ${country.displayName}'), ); ``` +* `countryFilter`: Can be used to uses filter the countries list (optional). + - It takes a list of country code(iso2). + - Can't provide both exclude and countryFilter ## Contributions Contributions of any kind are more than welcome! Feel free to fork and improve country_code_picker in any way you want, make a pull request, or open an issue. diff --git a/example/lib/main.dart b/example/lib/main.dart index b4c54dc..5a2040e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -15,6 +15,7 @@ class MyApp extends StatelessWidget { ), supportedLocales: [ const Locale('en'), + const Locale('el'), const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans'), // Generic Simplified Chinese 'zh_Hans' const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant'), // Generic traditional Chinese 'zh_Hant' ], @@ -34,9 +35,7 @@ class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - title: Text('Demo for country picker'), - ), + appBar: AppBar(title: Text('Demo for country picker')), body: Center( child: RaisedButton( onPressed: () { diff --git a/example/pubspec.lock b/example/pubspec.lock index 767d880..5217ada 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -49,7 +49,7 @@ packages: path: ".." relative: true source: path - version: "1.1.1" + version: "1.1.2" fake_async: dependency: transitive description: diff --git a/lib/country_picker.dart b/lib/country_picker.dart index 144eeb8..eae996a 100644 --- a/lib/country_picker.dart +++ b/lib/country_picker.dart @@ -26,14 +26,18 @@ void showCountryPicker({ @required BuildContext context, @required ValueChanged onSelect, List exclude, + List countryFilter, bool showPhoneCode = false, }) { assert(context != null); assert(onSelect != null); + assert(exclude == null || countryFilter == null, + 'Cannot provide both exclude and countryFilter'); showCountryListBottomSheet( context: context, onSelect: onSelect, exclude: exclude, + countryFilter: countryFilter, showPhoneCode: showPhoneCode, ); } diff --git a/lib/src/country.dart b/lib/src/country.dart index 8b420ea..b071ab1 100644 --- a/lib/src/country.dart +++ b/lib/src/country.dart @@ -76,13 +76,13 @@ class Country { return data; } - bool contains(String query, CountryLocalizations localizations) => - name.toLowerCase().contains(query.toLowerCase()) || - countryCode.toLowerCase().contains(query.toLowerCase()) || + bool startsWith(String query, CountryLocalizations localizations) => + name.toLowerCase().startsWith(query.toLowerCase()) || + countryCode.toLowerCase().startsWith(query.toLowerCase()) || (localizations ?.countryName(countryCode: countryCode) ?.toLowerCase() - ?.contains(query.toLowerCase()) ?? + ?.startsWith(query.toLowerCase()) ?? false); @override diff --git a/lib/src/country_list_bottom_sheet.dart b/lib/src/country_list_bottom_sheet.dart index 6a29ae0..c1925f1 100644 --- a/lib/src/country_list_bottom_sheet.dart +++ b/lib/src/country_list_bottom_sheet.dart @@ -7,6 +7,7 @@ void showCountryListBottomSheet({ @required BuildContext context, @required ValueChanged onSelect, List exclude, + List countryFilter, bool showPhoneCode = false, }) { assert(context != null); @@ -15,7 +16,8 @@ void showCountryListBottomSheet({ context: context, isScrollControlled: true, backgroundColor: Colors.transparent, - builder: (_) => _builder(context, onSelect, exclude, showPhoneCode), + builder: (_) => + _builder(context, onSelect, exclude, countryFilter, showPhoneCode), ); } @@ -23,6 +25,7 @@ Widget _builder( BuildContext context, ValueChanged onSelect, List exclude, + List countryFilter, bool showPhoneCode, ) { final device = MediaQuery.of(context).size.height; @@ -50,6 +53,7 @@ Widget _builder( child: CountryListView( onSelect: onSelect, exclude: exclude, + countryFilter: countryFilter, showPhoneCode: showPhoneCode, ), ); diff --git a/lib/src/country_list_view.dart b/lib/src/country_list_view.dart index e31971b..f464c2f 100644 --- a/lib/src/country_list_view.dart +++ b/lib/src/country_list_view.dart @@ -16,14 +16,23 @@ class CountryListView extends StatefulWidget { /// An optional [exclude] argument can be used to exclude(remove) one ore more /// country from the countries list. It takes a list of country code(iso2). + /// Note: Can't provide both [exclude] and [countryFilter] final List exclude; + /// An optional [countryFilter] argument can be used to filter the + /// list of countries. It takes a list of country code(iso2). + /// Note: Can't provide both [countryFilter] and [exclude] + final List countryFilter; + const CountryListView({ Key key, @required this.onSelect, this.exclude, + this.countryFilter, this.showPhoneCode = false, }) : assert(onSelect != null), + assert(exclude == null || countryFilter == null, + 'Cannot provide both exclude and countryFilter'), super(key: key); @override @@ -46,6 +55,10 @@ class _CountryListViewState extends State { _countryList.removeWhere( (element) => widget.exclude.contains(element.countryCode)); } + if (widget.countryFilter != null) { + _countryList.removeWhere( + (element) => !widget.countryFilter.contains(element.countryCode)); + } _filteredList = []; _filteredList.addAll(_countryList); @@ -137,8 +150,9 @@ class _CountryListViewState extends State { if (query.isEmpty) { _searchResult.addAll(_countryList); } else { - _searchResult = - _countryList.where((c) => c.contains(query, localizations)).toList(); + _searchResult = _countryList + .where((c) => c.startsWith(query, localizations)) + .toList(); } setState(() => _filteredList = _searchResult); diff --git a/lib/src/country_localizations.dart b/lib/src/country_localizations.dart index 020fbfa..67843ff 100644 --- a/lib/src/country_localizations.dart +++ b/lib/src/country_localizations.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'res/strings/cn.dart'; import 'res/strings/en.dart'; +import 'res/strings/gr.dart'; import 'res/strings/tw.dart'; class CountryLocalizations { @@ -45,6 +46,8 @@ class CountryLocalizations { return cn[countryCode]; } break; + case 'el': + return gr[countryCode]; case 'en': default: return en[countryCode]; @@ -61,6 +64,7 @@ class _CountryLocalizationsDelegate return [ 'en', 'zh', + 'el', ].contains(locale.languageCode); } diff --git a/lib/src/res/strings/gr.dart b/lib/src/res/strings/gr.dart new file mode 100644 index 0000000..48ea042 --- /dev/null +++ b/lib/src/res/strings/gr.dart @@ -0,0 +1,248 @@ +Map gr = { + "AF": "Αφγανιστάν", + "AX": "Ώλαντ", + "AL": "Αλβανία", + "DZ": "Αλγερία", + "AS": "Αμερικανική Σαμόα", + "AD": "Ανδόρρα", + "AO": "Ανγκόλα", + "AI": "Ανγκουίλα", + "AG": "Αντίγκουα και Μπαρμπούντα", + "AR": "Αργεντινή", + "AM": "Αρμενία", + "AW": "Αρούμπα", + "AC": "Νήσος Αναλήψεως", + "AU": "Αυστραλία", + "AT": "Αυστρία", + "AZ": "Αζερμπαϊτζάν", + "BS": "Μπαχάμες", + "BH": "Μπαχρέιν", + "BD": "Μπανγκλαντές", + "BB": "Μπαρμπάντος", + "BY": "Λευκορωσία", + "BE": "Βέλγιο", + "BZ": "Μπελίζ", + "BJ": "Μπενίν", + "BM": "Βερμούδες", + "BT": "Μπουτάν", + "BO": "Βολιβία", + "BA": "Βοσνία-Ερζεγοβίνη", + "BW": "Μποτσουάνα", + "BR": "Βραζιλία", + "IO": "Βρετανικό Έδαφος Ινδικού Ωκεανού", + "VG": "Βρετανικές Παρθένοι Νήσοι", + "BN": "Μπρουνέι", + "BG": "Βουλγαρία", + "BF": "Μπουρκίνα Φάσο", + "BI": "Μπουρούντι", + "KH": "Καμπότζη", + "CM": "Καμερούν", + "CA": "Καναδάς", + "CV": "Πράσινο Ακρωτήρι", + "BQ": "Μποναίρ, Άγιος Ευστάθιος και Σάμπα", + "KY": "Κέιμαν Νήσοι", + "CF": "Κεντροαφρικανική Δημοκρατία", + "TD": "Τσαντ", + "CL": "Χιλή", + "CN": "Κίνα", + "CX": "Νήσος των Χριστουγέννων", + "CC": "Νησιά Κόκος (Keeling)", + "CO": "Κολομβία", + "KM": "Κομόρες", + "CD": "Λαϊκή Δημοκρατία του Κογκό", + "CG": "Κογκό", + "CK": "Νήσοι Κουκ", + "CR": "Κόστα Ρίκα", + "CI": "Ακτή Ελεφαντοστού", + "HR": "Κροατία", + "CU": "Κούβα", + "CW": "Κουρασάο", + "CY": "Κύπρος", + "CZ": "Τσεχία", + "DK": "Δανία", + "DJ": "Τζιμπουτί", + "DM": "Δομινίκα", + "DO": "Δομινικανή Δημοκρατία", + "TL": "Ανατολικό Τιμόρ", + "EC": "Ισημερινός", + "EG": "Αίγυπτος", + "SV": "Ελ Σαλβαδόρ", + "GQ": "Ισημερινή Γουινέα", + "ER": "Ερυθραία", + "EE": "Εσθονία", + "ET": "Αιθιοπία", + "FK": "Νήσοι Φώκλαντ", + "FO": "Νήσοι Φερόες", + "FJ": "Φίτζι", + "FI": "Φινλανδία", + "FR": "Γαλλία", + "GF": "Γαλλική Γουιάνα", + "PF": "Γαλλική Πολυνησία", + "GA": "Γκαμπόν", + "GM": "Γκάμπια", + "GE": "Γεωργία", + "DE": "Γερμανία", + "GH": "Γκάνα", + "GI": "Γιβραλτάρ", + "GR": "Ελλάδα", + "GL": "Γροιλανδία", + "GD": "Γρενάδα", + "GP": "Γουαδελούπη", + "GU": "Γκουάμ", + "GT": "Γουατεμάλα", + "GG": "Γκέρνσεϊ", + "GN": "Γουινέα", + "GW": "Γουινέα-Μπισσάου", + "GY": "Γουιάνα", + "HT": "Αϊτή", + "HM": "Νήσοι Χερντ και Μακντόναλντ", + "HN": "Ονδούρα", + "HK": "Χονγκ Κονγκ", + "HU": "Ουγγαρία", + "IS": "Ισλανδία", + "IN": "Ινδία", + "ID": "Ινδονησία", + "IR": "Ιράν", + "IQ": "Ιράκ", + "IE": "Ιρλανδία", + "IM": "Νήσος Μαν", + "IL": "Ισραήλ", + "IT": "Ιταλία", + "JM": "Τζαμάικα", + "JP": "Ιαπωνία", + "JE": "Τζέρσεϊ", + "JO": "Ιορδανία", + "KZ": "Καζακστάν", + "KE": "Κένυα", + "KI": "Κιριμπάτι", + "XK": "Κόσοβο", + "KW": "Κουβέιτ", + "KG": "Κιργιζία", + "LA": "Λάος", + "LV": "Λεττονία", + "LB": "Λίβανος", + "LS": "Λεσότο", + "LR": "Λιβερία", + "LY": "Λιβύη", + "LI": "Λίχτενσταϊν", + "LT": "Λιθουανία", + "LU": "Λουξεμβούργο", + "MO": "Μακάο", + "MK": "Βόρεια Μακεδονία", + "MG": "Μαδαγασκάρη", + "MW": "Μαλάουι", + "MY": "Μαλαισία", + "MV": "Μαλδίβες", + "ML": "Μάλι", + "MT": "Μάλτα", + "MH": "Νήσοι Μάρσαλ", + "MQ": "Μαρτινίκα", + "MR": "Μαυριτανία", + "MU": "Μαυρίκιος", + "YT": "Μαγιότ", + "MX": "Μεξικό", + "FM": "Ομόσπονδες Πολιτείες της Μικρονησίας", + "MD": "Μολδαβία", + "MC": "Μονακό", + "MN": "Μογγολία", + "ME": "Μαυροβούνιο", + "MS": "Μοντσερράτ", + "MA": "Μαρόκο", + "MZ": "Μοζαμβίκη", + "MM": "Μιανμάρ", + "NA": "Ναμίμπια", + "NR": "Ναουρού", + "NP": "Νεπάλ", + "NL": "Ολλανδία (Κάτω Χώρες)", + "NC": "Νέα Καληδονία", + "NZ": "Νέα Ζηλανδία", + "NI": "Νικαράγουα", + "NE": "Νίγηρας", + "NG": "Νιγηρία", + "NU": "Νιούε", + "NF": "Νησί Νόρφολκ", + "KP": "Βόρεια Κορέα", + "MP": "Βόρειες Μαριάνες Νήσοι", + "NO": "Νορβηγία", + "OM": "Ομάν", + "PK": "Πακιστάν", + "PW": "Παλάου", + "PS": "Δυτική Όχθη", + "PA": "Παναμάς", + "PG": "Παπούα Νέα Γουινέα", + "PY": "Παραγουάη", + "PE": "Περού", + "PH": "Φιλιππίνες", + "PL": "Πολωνία", + "PT": "Πορτογαλία", + "PR": "Πουέρτο Ρίκο", + "QA": "Κατάρ", + "RE": "Ρεϊνιόν", + "RO": "Ρουμανία", + "RU": "Ρωσία", + "RW": "Ρουάντα", + "BL": "Άγιος Βαρθολομαίος", + "SH": "Νήσος Αγίας Ελένης", + "KN": "Άγιος Χριστόφορος και Νέβις", + "LC": "Αγία Λουκία", + "MF": "Άγιος Μαρτίνος (Γαλλία)", + "PM": "Σαιν Πιερ και Μικελόν", + "VC": "Άγιος Βικέντιος και Γρεναδίνες", + "WS": "Σαμόα", + "SM": "Άγιος Μαρίνος", + "ST": "Σάο Τομέ και Πρίνσιπε", + "SA": "Σαουδική Αραβία", + "SN": "Σενεγάλη", + "RS": "Σερβία", + "SC": "Σεϋχέλλες", + "SL": "Σιέρα Λεόνε", + "SG": "Σιγκαπούρη", + "SX": "Άγιος Μαρτίνος (Ολλανδία)", + "SK": "Σλοβακία", + "SI": "Σλοβενία", + "SB": "Νήσοι Σολομώντα", + "SO": "Σομαλία", + "ZA": "Νότια Αφρική", + "GS": "Νήσοι Νότια Γεωργία και Νότιες Σάντουιτς", + "KR": "Νότια Κορέα", + "SS": "Νότιο Σουδάν", + "ES": "Ισπανία", + "LK": "Σρι Λάνκα", + "SD": "Σουδάν", + "SR": "Σουρινάμ", + "SJ": "Σβάλμπαρντ", + "SZ": "Εσουατίνι", + "SE": "Σουηδία", + "CH": "Ελβετία", + "SY": "Συρία", + "TW": "Ταϊβάν", + "TJ": "Τατζικιστάν", + "TZ": "Τανζανία", + "TH": "Ταϊλάνδη", + "TG": "Τόγκο", + "TK": "Τοκελάου", + "TO": "Τόγκα", + "TT": "Τρινιντάντ και Τομπάγκο", + "TN": "Τυνησία", + "TR": "Τουρκία", + "TM": "Τουρκμενιστάν", + "TC": "Τερκς και Κέικος", + "TV": "Τουβαλού", + "VI": "Αμερικανικές Παρθένοι Νήσοι", + "UG": "Ουγκάντα", + "UA": "Ουκρανία", + "AE": "Ηνωμένα Αραβικά Εμιράτα", + "GB": "Ηνωμένο Βασίλειο", + "US": "Ηνωμένες Πολιτείες Αμερικής", + "UY": "Ουρουγουάη", + "UZ": "Ουζμπεκιστάν", + "VU": "Βανουάτου", + "VA": "Βατικανό", + "VE": "Βενεζουέλα", + "VN": "Βιετνάμ", + "WF": "Ουαλίς και Φουτουνά", + "EH": "Δυτική Σαχάρα", + "YE": "Υεμένη", + "ZM": "Ζάμπια", + "ZW": "Ζιμπάμπουε", +}; diff --git a/pubspec.yaml b/pubspec.yaml index eebad4c..ad873e0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: country_picker description: A flutter package to select a country from a list of countries. -version: 1.1.1 +version: 1.1.2 homepage: https://github.com/Daniel-Ioannou repository: https://github.com/Daniel-Ioannou/flutter_country_picker