Skip to content

Commit

Permalink
V1.1.2
Browse files Browse the repository at this point in the history
Merge pull request #8 from Daniel-Ioannou/v1.1.2
  • Loading branch information
Daniel-Ioannou committed Oct 11, 2020
2 parents a4e0080 + 6ba9fde commit 701a88b
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 16 deletions.
16 changes: 16 additions & 0 deletions 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: <String>['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.
Expand Down
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -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:
Expand All @@ -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'
],
Expand All @@ -51,14 +52,17 @@ 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,
exclude: <String>['KN', 'MF'], //It takes a list of country code(iso2).
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.
8 changes: 6 additions & 2 deletions example/README.md
Expand Up @@ -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:
Expand All @@ -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'
],
Expand All @@ -51,14 +52,17 @@ 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,
exclude: <String>['KN', 'MF'], //It takes a list of country code(iso2).
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.
5 changes: 2 additions & 3 deletions example/lib/main.dart
Expand Up @@ -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'
],
Expand All @@ -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: () {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Expand Up @@ -49,7 +49,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.1"
version: "1.1.2"
fake_async:
dependency: transitive
description:
Expand Down
4 changes: 4 additions & 0 deletions lib/country_picker.dart
Expand Up @@ -26,14 +26,18 @@ void showCountryPicker({
@required BuildContext context,
@required ValueChanged<Country> onSelect,
List<String> exclude,
List<String> 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,
);
}
8 changes: 4 additions & 4 deletions lib/src/country.dart
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/src/country_list_bottom_sheet.dart
Expand Up @@ -7,6 +7,7 @@ void showCountryListBottomSheet({
@required BuildContext context,
@required ValueChanged<Country> onSelect,
List<String> exclude,
List<String> countryFilter,
bool showPhoneCode = false,
}) {
assert(context != null);
Expand All @@ -15,14 +16,16 @@ void showCountryListBottomSheet({
context: context,
isScrollControlled: true,
backgroundColor: Colors.transparent,
builder: (_) => _builder(context, onSelect, exclude, showPhoneCode),
builder: (_) =>
_builder(context, onSelect, exclude, countryFilter, showPhoneCode),
);
}

Widget _builder(
BuildContext context,
ValueChanged<Country> onSelect,
List<String> exclude,
List<String> countryFilter,
bool showPhoneCode,
) {
final device = MediaQuery.of(context).size.height;
Expand Down Expand Up @@ -50,6 +53,7 @@ Widget _builder(
child: CountryListView(
onSelect: onSelect,
exclude: exclude,
countryFilter: countryFilter,
showPhoneCode: showPhoneCode,
),
);
Expand Down
18 changes: 16 additions & 2 deletions lib/src/country_list_view.dart
Expand Up @@ -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<String> 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<String> 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
Expand All @@ -46,6 +55,10 @@ class _CountryListViewState extends State<CountryListView> {
_countryList.removeWhere(
(element) => widget.exclude.contains(element.countryCode));
}
if (widget.countryFilter != null) {
_countryList.removeWhere(
(element) => !widget.countryFilter.contains(element.countryCode));
}

_filteredList = <Country>[];
_filteredList.addAll(_countryList);
Expand Down Expand Up @@ -137,8 +150,9 @@ class _CountryListViewState extends State<CountryListView> {
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);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/country_localizations.dart
Expand Up @@ -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 {
Expand Down Expand Up @@ -45,6 +46,8 @@ class CountryLocalizations {
return cn[countryCode];
}
break;
case 'el':
return gr[countryCode];
case 'en':
default:
return en[countryCode];
Expand All @@ -61,6 +64,7 @@ class _CountryLocalizationsDelegate
return [
'en',
'zh',
'el',
].contains(locale.languageCode);
}

Expand Down

0 comments on commit 701a88b

Please sign in to comment.