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

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

View File

@@ -1,32 +1,33 @@
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/selection_dialog.dart';
import 'package:flutter/material.dart';
export 'celement.dart';
export 'country_code.dart';
class CountryCodePicker extends StatefulWidget {
final Function(CElement) onChanged;
final ValueChanged<CountryCode> onChanged;
final String initialSelection;
final List<String> favorite;
final TextStyle textStyle;
final EdgeInsetsGeometry padding;
CountryCodePicker(
{this.onChanged,
this.initialSelection,
this.favorite,
this.textStyle,
this.padding});
CountryCodePicker({
this.onChanged,
this.initialSelection,
this.favorite = const [],
this.textStyle,
this.padding = const EdgeInsets.all(0.0),
});
@override
State<StatefulWidget> createState() {
List<Map> jsonList = codes;
List<CElement> elements = jsonList
.map((s) => new CElement(
List<CountryCode> elements = jsonList
.map((s) => new CountryCode(
name: s['name'],
code: s['code'],
dialCode: s['dial_code'],
@@ -39,9 +40,9 @@ class CountryCodePicker extends StatefulWidget {
}
class _CountryCodePickerState extends State<CountryCodePicker> {
CElement selectedItem;
List<CElement> elements = [];
List<CElement> favoriteElements = [];
CountryCode selectedItem;
List<CountryCode> elements = [];
List<CountryCode> favoriteElements = [];
_CountryCodePickerState(this.elements);
@@ -114,7 +115,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
});
}
void _publishSelection(CElement e) {
void _publishSelection(CountryCode e) {
if (widget.onChanged != null) {
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';
/// selection dialog used for selection of the country code
class SelectionDialog extends StatefulWidget {
final List<CElement> elements;
final List<CountryCode> elements;
/// elements passed as favorite
final List<CElement> favoriteElements;
final List<CountryCode> favoriteElements;
SelectionDialog(this.elements, this.favoriteElements);
@@ -16,7 +16,7 @@ class SelectionDialog extends StatefulWidget {
class _SelectionDialogState extends State<SelectionDialog> {
/// this is useful for filtering purpose
List<CElement> showedElements = [];
List<CountryCode> showedElements = [];
@override
Widget build(BuildContext context) => new SimpleDialog(
@@ -34,63 +34,70 @@ class _SelectionDialogState extends State<SelectionDialog> {
: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[]
..addAll(widget.favoriteElements.map((f) {
return new SimpleDialogOption(
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Image.asset(
f.flagUri,
package: 'country_code_picker',
width: 32.0,
),
..addAll(widget.favoriteElements
.map(
(f) => new SimpleDialogOption(
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Flexible(
child: Padding(
padding:
const EdgeInsets.only(right: 16.0),
child: Image.asset(
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,
child: new Text(
f.toLongString(),
overflow: TextOverflow.fade,
),
),
],
),
onPressed: () {
_selectItem(f);
});
}).toList())
)
.toList())
..add(new Divider())),
]..addAll(showedElements
.map((e) => new SimpleDialogOption(
key: Key(e.toLongString()),
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Image.asset(
e.flagUri,
package: 'country_code_picker',
width: 32.0,
.map(
(e) => new SimpleDialogOption(
key: Key(e.toLongString()),
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Flexible(
child: Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Image.asset(
e.flagUri,
package: 'country_code_picker',
width: 32.0,
),
),
),
),
Flexible(
fit: FlexFit.tight,
child: Text(
e.toLongString(),
overflow: TextOverflow.fade,
),
),
],
),
Flexible(
fit: FlexFit.tight,
child: Text(
e.toLongString(),
overflow: TextOverflow.fade,
),
),
],
),
onPressed: () {
_selectItem(e);
}))
onPressed: () {
_selectItem(e);
},
),
)
.toList()));
@override
@@ -111,7 +118,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
});
}
void _selectItem(CElement e) {
void _selectItem(CountryCode e) {
Navigator.pop(context, e);
}
}