Changed CElement with CountryCode and fix error on favorite null

This commit is contained in:
Salvatore Giordano
2018-11-23 00:51:01 +01:00
parent 7006dab8ef
commit a9b91ba851
6 changed files with 97 additions and 79 deletions

View File

@@ -53,3 +53,7 @@ Update flags dimension to reduce application size
## 1.0.4 ## 1.0.4
Update country name with translated version Update country name with translated version
## 1.1.0
Changed CElement with CountryCode and fix error on favorite null

View File

@@ -1,4 +1,4 @@
[![Pub](https://img.shields.io/badge/Pub-1.0.4-orange.svg)](https://pub.dartlang.org/packages/country_code_picker) [![Pub](https://img.shields.io/badge/Pub-1.1.0-orange.svg)](https://pub.dartlang.org/packages/country_code_picker)
# country_code_picker # country_code_picker
@@ -22,22 +22,23 @@ Just put the component in your application setting the onChanged callback.
initialSelection: 'IT', initialSelection: 'IT',
favorite: ['+39','FR'], favorite: ['+39','FR'],
), ),
)); ),
);
``` ```
Note: Your onChanged function can be any function of the type shown below: Note: Your onChanged function can be any function of the type shown below:
```dart ```dart
(CElement)->dynamic (CountryCode)->dynamic
``` ```
Example: Example:
```dart ```dart
void _onCountryChange(CElement cElement) { void _onCountryChange(CountryCode countryCode) {
//Todo : manipulate the selected country code here //Todo : manipulate the selected country code here
print("New Country selected: " + cElement.toString()); print("New Country selected: " + countryCode.toString());
} }
``` ```

View File

@@ -1,5 +1,10 @@
mixin ToAlias {}
@deprecated
class CElement = CountryCode with ToAlias;
/// Country element. This is the element that contains all the information /// Country element. This is the element that contains all the information
class CElement { class CountryCode {
/// the name of the country /// the name of the country
String name; String name;
@@ -12,7 +17,7 @@ class CElement {
/// the dial code (+39,+93..) /// the dial code (+39,+93..)
String dialCode; String dialCode;
CElement({this.name, this.flagUri, this.code, this.dialCode}); CountryCode({this.name, this.flagUri, this.code, this.dialCode});
@override @override
String toString() => "$dialCode"; String toString() => "$dialCode";

View File

@@ -1,32 +1,33 @@
library country_code_picker; library country_code_picker;
import 'package:country_code_picker/celement.dart'; import 'package:country_code_picker/country_code.dart';
import 'package:country_code_picker/country_codes.dart'; import 'package:country_code_picker/country_codes.dart';
import 'package:country_code_picker/selection_dialog.dart'; import 'package:country_code_picker/selection_dialog.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
export 'celement.dart'; export 'country_code.dart';
class CountryCodePicker extends StatefulWidget { class CountryCodePicker extends StatefulWidget {
final Function(CElement) onChanged; final ValueChanged<CountryCode> onChanged;
final String initialSelection; final String initialSelection;
final List<String> favorite; final List<String> favorite;
final TextStyle textStyle; final TextStyle textStyle;
final EdgeInsetsGeometry padding; final EdgeInsetsGeometry padding;
CountryCodePicker( CountryCodePicker({
{this.onChanged, this.onChanged,
this.initialSelection, this.initialSelection,
this.favorite, this.favorite = const [],
this.textStyle, this.textStyle,
this.padding}); this.padding = const EdgeInsets.all(0.0),
});
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
List<Map> jsonList = codes; List<Map> jsonList = codes;
List<CElement> elements = jsonList List<CountryCode> elements = jsonList
.map((s) => new CElement( .map((s) => new CountryCode(
name: s['name'], name: s['name'],
code: s['code'], code: s['code'],
dialCode: s['dial_code'], dialCode: s['dial_code'],
@@ -39,9 +40,9 @@ class CountryCodePicker extends StatefulWidget {
} }
class _CountryCodePickerState extends State<CountryCodePicker> { class _CountryCodePickerState extends State<CountryCodePicker> {
CElement selectedItem; CountryCode selectedItem;
List<CElement> elements = []; List<CountryCode> elements = [];
List<CElement> favoriteElements = []; List<CountryCode> favoriteElements = [];
_CountryCodePickerState(this.elements); _CountryCodePickerState(this.elements);
@@ -114,7 +115,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
}); });
} }
void _publishSelection(CElement e) { void _publishSelection(CountryCode e) {
if (widget.onChanged != null) { if (widget.onChanged != null) {
widget.onChanged(e); widget.onChanged(e);
} }

View File

@@ -1,12 +1,12 @@
import 'package:country_code_picker/celement.dart'; import 'package:country_code_picker/country_code.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// selection dialog used for selection of the country code /// selection dialog used for selection of the country code
class SelectionDialog extends StatefulWidget { class SelectionDialog extends StatefulWidget {
final List<CElement> elements; final List<CountryCode> elements;
/// elements passed as favorite /// elements passed as favorite
final List<CElement> favoriteElements; final List<CountryCode> favoriteElements;
SelectionDialog(this.elements, this.favoriteElements); SelectionDialog(this.elements, this.favoriteElements);
@@ -16,7 +16,7 @@ class SelectionDialog extends StatefulWidget {
class _SelectionDialogState extends State<SelectionDialog> { class _SelectionDialogState extends State<SelectionDialog> {
/// this is useful for filtering purpose /// this is useful for filtering purpose
List<CElement> showedElements = []; List<CountryCode> showedElements = [];
@override @override
Widget build(BuildContext context) => new SimpleDialog( Widget build(BuildContext context) => new SimpleDialog(
@@ -34,63 +34,70 @@ class _SelectionDialogState extends State<SelectionDialog> {
: new Column( : new Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[] children: <Widget>[]
..addAll(widget.favoriteElements.map((f) { ..addAll(widget.favoriteElements
return new SimpleDialogOption( .map(
child: Flex( (f) => new SimpleDialogOption(
direction: Axis.horizontal, child: Flex(
children: <Widget>[ direction: Axis.horizontal,
Flexible( children: <Widget>[
child: Padding( Flexible(
padding: const EdgeInsets.only(right: 16.0), child: Padding(
child: Image.asset( padding:
f.flagUri, const EdgeInsets.only(right: 16.0),
package: 'country_code_picker', child: Image.asset(
width: 32.0, f.flagUri,
), package: 'country_code_picker',
width: 32.0,
),
),
),
Flexible(
fit: FlexFit.tight,
child: new Text(
f.toLongString(),
overflow: TextOverflow.fade,
),
),
],
), ),
onPressed: () {
_selectItem(f);
},
), ),
Flexible( )
fit: FlexFit.tight, .toList())
child: new Text(
f.toLongString(),
overflow: TextOverflow.fade,
),
),
],
),
onPressed: () {
_selectItem(f);
});
}).toList())
..add(new Divider())), ..add(new Divider())),
]..addAll(showedElements ]..addAll(showedElements
.map((e) => new SimpleDialogOption( .map(
key: Key(e.toLongString()), (e) => new SimpleDialogOption(
child: Flex( key: Key(e.toLongString()),
direction: Axis.horizontal, child: Flex(
children: <Widget>[ direction: Axis.horizontal,
Flexible( children: <Widget>[
child: Padding( Flexible(
padding: const EdgeInsets.only(right: 16.0), child: Padding(
child: Image.asset( padding: const EdgeInsets.only(right: 16.0),
e.flagUri, child: Image.asset(
package: 'country_code_picker', e.flagUri,
width: 32.0, package: 'country_code_picker',
width: 32.0,
),
),
), ),
), Flexible(
fit: FlexFit.tight,
child: Text(
e.toLongString(),
overflow: TextOverflow.fade,
),
),
],
), ),
Flexible( onPressed: () {
fit: FlexFit.tight, _selectItem(e);
child: Text( },
e.toLongString(), ),
overflow: TextOverflow.fade, )
),
),
],
),
onPressed: () {
_selectItem(e);
}))
.toList())); .toList()));
@override @override
@@ -111,7 +118,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
}); });
} }
void _selectItem(CElement e) { void _selectItem(CountryCode e) {
Navigator.pop(context, e); Navigator.pop(context, e);
} }
} }

View File

@@ -1,6 +1,6 @@
name: country_code_picker name: country_code_picker
description: A flutter package for showing a country code selector. description: A flutter package for showing a country code selector.
version: 1.0.4 version: 1.1.0
author: Salvatore-Giordano <svtgiordano@gmail.com> author: Salvatore-Giordano <svtgiordano@gmail.com>
homepage: https://github.com/Salvatore-Giordano/CountryCodePicker homepage: https://github.com/Salvatore-Giordano/CountryCodePicker