add favorite country options

This commit is contained in:
Salvatore Giordano
2018-04-01 11:05:15 +02:00
parent 3ba8c12a19
commit 1394a9ceaf
6 changed files with 54 additions and 21 deletions

View File

@@ -1,3 +1,7 @@
## 0.0.1
Initial release
## 0.0.2
Add favorite countries option.

View File

@@ -11,18 +11,18 @@ A flutter package for showing a country code selector.
Just put the component in your application setting the onChanged callback.
```dart
......
@override
Widget build(BuildContext context) => new Scaffold(
body: new Center(
child: new CountryCodePicker(
onChanged: print,
initialSelection: 'IT',
favorites: ['IT','FR'],
),
));
.....
```
It's also possible to select the initial selection using the country code.
```
## Known issues

View File

@@ -2,16 +2,16 @@ class CElement {
String name;
String flag;
String code;
String dial_code;
String dialCode;
CElement({this.name, this.flag, this.code, this.dial_code});
CElement({this.name, this.flag, this.code, this.dialCode});
@override
String toString() {
return "$flag $dial_code";
return "$flag $dialCode";
}
String toLongString() {
return "$flag $dial_code $name";
return "$flag $dialCode $name";
}
}

View File

@@ -6,10 +6,11 @@ import 'package:country_code_picker/selection_dialog.dart';
import 'package:country_code_picker/celement.dart';
class CountryCodePicker extends StatefulWidget {
final Function(CElement) onChanged;
final Function(String) onChanged;
final String initialSelection;
final List<String> favorites;
CountryCodePicker({this.onChanged, this.initialSelection});
CountryCodePicker({this.onChanged, this.initialSelection, this.favorites});
@override
State<StatefulWidget> createState() {
@@ -19,7 +20,7 @@ class CountryCodePicker extends StatefulWidget {
.map((s) => new CElement(
name: s['name'],
code: s['code'],
dial_code: s['dial_code'],
dialCode: s['dial_code'],
flag: s['flag'],
))
.toList();
@@ -30,6 +31,7 @@ class CountryCodePicker extends StatefulWidget {
class _CountryCodePickerState extends State<CountryCodePicker> {
CElement selectedItem;
List<CElement> elements = [];
List<CElement> favoriteElements = [];
_CountryCodePickerState(this.elements);
@@ -45,23 +47,31 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
selectedItem = elements.firstWhere(
(e) => e.code.toUpperCase() == widget.initialSelection.toUpperCase(),
orElse: () => elements[0]);
}else{
} else {
selectedItem = elements[0];
}
favoriteElements = elements
.where((e) =>
widget.favorites.firstWhere((f) => e.code == f.toUpperCase(),
orElse: () => null) !=
null)
.toList();
print(favoriteElements);
super.initState();
}
void _showSelectionDialog() {
showDialog(
context: context,
child: new SelectionDialog(elements),
child: new SelectionDialog(elements, favoriteElements),
).then((e) {
if (e != null) {
setState(() {
selectedItem = e;
});
if (widget.onChanged != null) {
widget.onChanged(e);
widget.onChanged(e.dialCode);
}
}
});

View File

@@ -3,8 +3,9 @@ import 'package:country_code_picker/celement.dart';
class SelectionDialog extends StatefulWidget {
final List<CElement> elements;
final List<CElement> favoriteElements;
SelectionDialog(this.elements);
SelectionDialog(this.elements, this.favoriteElements);
@override
State<StatefulWidget> createState() => new _SelectionDialogState();
@@ -15,17 +16,35 @@ class _SelectionDialogState extends State<SelectionDialog> {
@override
Widget build(BuildContext context) => new SimpleDialog(
title: new TextField(
decoration: new InputDecoration(prefixIcon: new Icon(Icons.search)),
onChanged: _filterElements,
title: new Column(
children: <Widget>[
new TextField(
decoration: new InputDecoration(prefixIcon: new Icon(Icons.search)),
onChanged: _filterElements,
),
],
),
children: showedElements
children: [
widget.favoriteElements.isEmpty
? new Container()
: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[]
..addAll(widget.favoriteElements.map((f) {
return new SimpleDialogOption(
child: new Text(f.toLongString()),
onPressed: () {
_selectItem(f);
});
}).toList())
..add(new Divider())),
]..addAll(showedElements
.map((e) => new SimpleDialogOption(
child: new Text(e.toLongString()),
onPressed: () {
_selectItem(e);
}))
.toList());
.toList()));
@override
void initState() {
@@ -39,7 +58,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
showedElements = widget.elements
.where((e) =>
e.code.contains(s) ||
e.dial_code.contains(s) ||
e.dialCode.contains(s) ||
e.name.toUpperCase().contains(s))
.toList();
});

Binary file not shown.

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 166 KiB