add favorite country options
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
## 0.0.1
|
## 0.0.1
|
||||||
|
|
||||||
Initial release
|
Initial release
|
||||||
|
|
||||||
|
## 0.0.2
|
||||||
|
|
||||||
|
Add favorite countries option.
|
||||||
@@ -11,18 +11,18 @@ A flutter package for showing a country code selector.
|
|||||||
Just put the component in your application setting the onChanged callback.
|
Just put the component in your application setting the onChanged callback.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
......
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => new Scaffold(
|
Widget build(BuildContext context) => new Scaffold(
|
||||||
body: new Center(
|
body: new Center(
|
||||||
child: new CountryCodePicker(
|
child: new CountryCodePicker(
|
||||||
onChanged: print,
|
onChanged: print,
|
||||||
|
initialSelection: 'IT',
|
||||||
|
favorites: ['IT','FR'],
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
.....
|
|
||||||
```
|
|
||||||
|
|
||||||
It's also possible to select the initial selection using the country code.
|
```
|
||||||
|
|
||||||
## Known issues
|
## Known issues
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ class CElement {
|
|||||||
String name;
|
String name;
|
||||||
String flag;
|
String flag;
|
||||||
String code;
|
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
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return "$flag $dial_code";
|
return "$flag $dialCode";
|
||||||
}
|
}
|
||||||
|
|
||||||
String toLongString() {
|
String toLongString() {
|
||||||
return "$flag $dial_code $name";
|
return "$flag $dialCode $name";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,10 +6,11 @@ import 'package:country_code_picker/selection_dialog.dart';
|
|||||||
import 'package:country_code_picker/celement.dart';
|
import 'package:country_code_picker/celement.dart';
|
||||||
|
|
||||||
class CountryCodePicker extends StatefulWidget {
|
class CountryCodePicker extends StatefulWidget {
|
||||||
final Function(CElement) onChanged;
|
final Function(String) onChanged;
|
||||||
final String initialSelection;
|
final String initialSelection;
|
||||||
|
final List<String> favorites;
|
||||||
|
|
||||||
CountryCodePicker({this.onChanged, this.initialSelection});
|
CountryCodePicker({this.onChanged, this.initialSelection, this.favorites});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() {
|
State<StatefulWidget> createState() {
|
||||||
@@ -19,7 +20,7 @@ class CountryCodePicker extends StatefulWidget {
|
|||||||
.map((s) => new CElement(
|
.map((s) => new CElement(
|
||||||
name: s['name'],
|
name: s['name'],
|
||||||
code: s['code'],
|
code: s['code'],
|
||||||
dial_code: s['dial_code'],
|
dialCode: s['dial_code'],
|
||||||
flag: s['flag'],
|
flag: s['flag'],
|
||||||
))
|
))
|
||||||
.toList();
|
.toList();
|
||||||
@@ -30,6 +31,7 @@ class CountryCodePicker extends StatefulWidget {
|
|||||||
class _CountryCodePickerState extends State<CountryCodePicker> {
|
class _CountryCodePickerState extends State<CountryCodePicker> {
|
||||||
CElement selectedItem;
|
CElement selectedItem;
|
||||||
List<CElement> elements = [];
|
List<CElement> elements = [];
|
||||||
|
List<CElement> favoriteElements = [];
|
||||||
|
|
||||||
_CountryCodePickerState(this.elements);
|
_CountryCodePickerState(this.elements);
|
||||||
|
|
||||||
@@ -45,23 +47,31 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
|
|||||||
selectedItem = elements.firstWhere(
|
selectedItem = elements.firstWhere(
|
||||||
(e) => e.code.toUpperCase() == widget.initialSelection.toUpperCase(),
|
(e) => e.code.toUpperCase() == widget.initialSelection.toUpperCase(),
|
||||||
orElse: () => elements[0]);
|
orElse: () => elements[0]);
|
||||||
}else{
|
} else {
|
||||||
selectedItem = elements[0];
|
selectedItem = elements[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
favoriteElements = elements
|
||||||
|
.where((e) =>
|
||||||
|
widget.favorites.firstWhere((f) => e.code == f.toUpperCase(),
|
||||||
|
orElse: () => null) !=
|
||||||
|
null)
|
||||||
|
.toList();
|
||||||
|
print(favoriteElements);
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showSelectionDialog() {
|
void _showSelectionDialog() {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
child: new SelectionDialog(elements),
|
child: new SelectionDialog(elements, favoriteElements),
|
||||||
).then((e) {
|
).then((e) {
|
||||||
if (e != null) {
|
if (e != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
selectedItem = e;
|
selectedItem = e;
|
||||||
});
|
});
|
||||||
if (widget.onChanged != null) {
|
if (widget.onChanged != null) {
|
||||||
widget.onChanged(e);
|
widget.onChanged(e.dialCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ import 'package:country_code_picker/celement.dart';
|
|||||||
|
|
||||||
class SelectionDialog extends StatefulWidget {
|
class SelectionDialog extends StatefulWidget {
|
||||||
final List<CElement> elements;
|
final List<CElement> elements;
|
||||||
|
final List<CElement> favoriteElements;
|
||||||
|
|
||||||
SelectionDialog(this.elements);
|
SelectionDialog(this.elements, this.favoriteElements);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() => new _SelectionDialogState();
|
State<StatefulWidget> createState() => new _SelectionDialogState();
|
||||||
@@ -15,17 +16,35 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => new SimpleDialog(
|
Widget build(BuildContext context) => new SimpleDialog(
|
||||||
title: new TextField(
|
title: new Column(
|
||||||
decoration: new InputDecoration(prefixIcon: new Icon(Icons.search)),
|
children: <Widget>[
|
||||||
onChanged: _filterElements,
|
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(
|
.map((e) => new SimpleDialogOption(
|
||||||
child: new Text(e.toLongString()),
|
child: new Text(e.toLongString()),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
_selectItem(e);
|
_selectItem(e);
|
||||||
}))
|
}))
|
||||||
.toList());
|
.toList()));
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -39,7 +58,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
|||||||
showedElements = widget.elements
|
showedElements = widget.elements
|
||||||
.where((e) =>
|
.where((e) =>
|
||||||
e.code.contains(s) ||
|
e.code.contains(s) ||
|
||||||
e.dial_code.contains(s) ||
|
e.dialCode.contains(s) ||
|
||||||
e.name.toUpperCase().contains(s))
|
e.name.toUpperCase().contains(s))
|
||||||
.toList();
|
.toList();
|
||||||
});
|
});
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 176 KiB After Width: | Height: | Size: 166 KiB |
Reference in New Issue
Block a user