diff --git a/CHANGELOG.md b/CHANGELOG.md index ad97d23..d96b850 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.0.4] - 27 Aug 2020 + +* Implement search. + ## [1.0.3] - 22 Aug 2020 * Add show phone code option. diff --git a/README.md b/README.md index e7c33cb..d0c5cdf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Country picker (Developers Preview) +# Country picker [![pub package](https://img.shields.io/pub/v/country_picker.svg)](https://pub.dev/packages/country_picker) @@ -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.0.3 + country_picker: ^1.0.4 ``` In your dart file, import the library: diff --git a/assets/ReadMe Screenshot.png b/assets/ReadMe Screenshot.png index 978eb55..db49f87 100644 Binary files a/assets/ReadMe Screenshot.png and b/assets/ReadMe Screenshot.png differ diff --git a/example/README.md b/example/README.md index f6e1f39..d0c5cdf 100644 --- a/example/README.md +++ b/example/README.md @@ -1,16 +1,46 @@ -# example +# Country picker -A example application for country picker package. +[![pub package](https://img.shields.io/pub/v/country_picker.svg)](https://pub.dev/packages/country_picker) + +A flutter package to select a country from a list of countries. + +n1 ## Getting Started -This project is a starting point for a Flutter application. + Add the package to your pubspec.yaml: + + ```yaml + country_picker: ^1.0.4 + ``` + + In your dart file, import the library: -A few resources to get you started if this is your first Flutter project: + ```Dart + import 'package:country_picker/country_picker.dart'; + ``` + Show country picker using `showCountryPicker`: +```Dart +showCountryPicker( + context: context, + showPhoneCode: true, // optional. Shows phone code before the country name. + onSelect: (Country country) { + print('Select country: ${country.displayName}'); + }, +); +``` -- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) -- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) +### 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). +```Dart +showCountryPicker( + context: context, + exclude: ['KN', 'MF'], //It takes a list of country code(iso2). + onSelect: (Country country) => print('Select country: ${country.displayName}'), +); +``` -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. +## 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/pubspec.lock b/example/pubspec.lock index 6dbb58d..2af5fac 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -56,7 +56,7 @@ packages: path: ".." relative: true source: path - version: "1.0.3" + version: "1.0.4" crypto: dependency: transitive description: diff --git a/lib/src/country.dart b/lib/src/country.dart index c9ffef7..d745e73 100644 --- a/lib/src/country.dart +++ b/lib/src/country.dart @@ -53,4 +53,11 @@ class Country { data['e164_key'] = e164Key; return data; } + + bool contains(String query) => + name.toLowerCase().contains(query.toLowerCase()) || + countryCode.toLowerCase().contains(query.toLowerCase()); + + @override + String toString() => 'Country(countryCode: $countryCode, name: $name)'; } diff --git a/lib/src/country_list_view.dart b/lib/src/country_list_view.dart index 556f66b..3a1dbf7 100644 --- a/lib/src/country_list_view.dart +++ b/lib/src/country_list_view.dart @@ -30,28 +30,59 @@ class CountryListView extends StatefulWidget { } class _CountryListViewState extends State { - List countryList; + List _countryList; + List _filteredList; + TextEditingController _searchController; @override void initState() { super.initState(); - - countryList = + _searchController = TextEditingController(); + _countryList = countryCodes.map((country) => Country.from(json: country)).toList(); if (widget.exclude != null) { - countryList.removeWhere( + _countryList.removeWhere( (element) => widget.exclude.contains(element.countryCode)); } + + _filteredList = []; + _filteredList.addAll(_countryList); } @override Widget build(BuildContext context) { - return ListView( - children: countryList.map((country) => listRow(country)).toList(), + return Column( + children: [ + const SizedBox(height: 12), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10), + child: TextField( + controller: _searchController, + decoration: InputDecoration( + labelText: "Search", + hintText: "Search", + prefixIcon: Icon(Icons.search), + border: OutlineInputBorder( + borderSide: BorderSide( + color: const Color(0xFF8C98A8).withOpacity(0.2), + ), + ), + ), + onChanged: _filterSearchResults, + ), + ), + Expanded( + child: ListView( + children: _filteredList + .map((country) => _listRow(country)) + .toList(), + ), + ), + ], ); } - Widget listRow(Country country) { + Widget _listRow(Country country) { return Material( // Add Material Widget with transparent color // so the ripple effect of InkWell will show on tap @@ -94,4 +125,16 @@ class _CountryListViewState extends State { ), ); } + + void _filterSearchResults(String query) { + List _searchResult = []; + + if (query.isEmpty) { + _searchResult.addAll(_countryList); + } else { + _searchResult = _countryList.where((c) => c.contains(query)).toList(); + } + + setState(() => _filteredList = _searchResult); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 072bab7..8bf914b 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.0.3 +version: 1.0.4 homepage: https://github.com/Daniel-Ioannou repository: https://github.com/Daniel-Ioannou/flutter_country_picker