Skip to content

Commit

Permalink
V1.0.4
Browse files Browse the repository at this point in the history
Merge pull request #4 from Daniel-Ioannou/v1.0.4
  • Loading branch information
Daniel-Ioannou authored Aug 27, 2020
2 parents 19d2cc5 + b11b7e1 commit 1f9d0c9
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.0.4] - 27 Aug 2020

* Implement search.

## [1.0.3] - 22 Aug 2020

* Add show phone code option.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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:
Expand Down
Binary file modified assets/ReadMe Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 39 additions & 9 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -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.

<img height="600" alt="n1" src="https://raw.githubusercontent.com/Daniel-Ioannou/flutter_country_picker/master/assets/ReadMe%20Screenshot.png">

## 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: <String>['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.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.3"
version: "1.0.4"
crypto:
dependency: transitive
description:
Expand Down
7 changes: 7 additions & 0 deletions lib/src/country.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
}
57 changes: 50 additions & 7 deletions lib/src/country_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,59 @@ class CountryListView extends StatefulWidget {
}

class _CountryListViewState extends State<CountryListView> {
List<Country> countryList;
List<Country> _countryList;
List<Country> _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 = <Country>[];
_filteredList.addAll(_countryList);
}

@override
Widget build(BuildContext context) {
return ListView(
children: countryList.map<Widget>((country) => listRow(country)).toList(),
return Column(
children: <Widget>[
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<Widget>((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
Expand Down Expand Up @@ -94,4 +125,16 @@ class _CountryListViewState extends State<CountryListView> {
),
);
}

void _filterSearchResults(String query) {
List<Country> _searchResult = <Country>[];

if (query.isEmpty) {
_searchResult.addAll(_countryList);
} else {
_searchResult = _countryList.where((c) => c.contains(query)).toList();
}

setState(() => _filteredList = _searchResult);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 1f9d0c9

Please sign in to comment.