diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c4f0ba..ce24fa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ ## 0.0.1 Initial release + +## 0.0.2 + +Add favorite countries option. \ No newline at end of file diff --git a/README.md b/README.md index 042f1b7..97a0b12 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/celement.dart b/lib/celement.dart index c6d9ee5..9740b93 100644 --- a/lib/celement.dart +++ b/lib/celement.dart @@ -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"; } } \ No newline at end of file diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index f77be98..33a7624 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -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 favorites; - CountryCodePicker({this.onChanged, this.initialSelection}); + CountryCodePicker({this.onChanged, this.initialSelection, this.favorites}); @override State 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 { CElement selectedItem; List elements = []; + List favoriteElements = []; _CountryCodePickerState(this.elements); @@ -45,23 +47,31 @@ class _CountryCodePickerState extends State { 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); } } }); diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index f0f7b1a..968d49e 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -3,8 +3,9 @@ import 'package:country_code_picker/celement.dart'; class SelectionDialog extends StatefulWidget { final List elements; + final List favoriteElements; - SelectionDialog(this.elements); + SelectionDialog(this.elements, this.favoriteElements); @override State createState() => new _SelectionDialogState(); @@ -15,17 +16,35 @@ class _SelectionDialogState extends State { @override Widget build(BuildContext context) => new SimpleDialog( - title: new TextField( - decoration: new InputDecoration(prefixIcon: new Icon(Icons.search)), - onChanged: _filterElements, + title: new Column( + children: [ + 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: [] + ..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 { showedElements = widget.elements .where((e) => e.code.contains(s) || - e.dial_code.contains(s) || + e.dialCode.contains(s) || e.name.toUpperCase().contains(s)) .toList(); }); diff --git a/screenshots/screen2.png b/screenshots/screen2.png index ebe2d72..e0ddfb9 100644 Binary files a/screenshots/screen2.png and b/screenshots/screen2.png differ