refactored selectiondialog _buildoption

OnlyCountrymode now also displays only country when closed
width doesn't change on search
This commit is contained in:
Benni Heiss
2019-03-26 11:20:17 +01:00
parent 13b14fa296
commit 71ca7a0b34
3 changed files with 36 additions and 55 deletions

View File

@@ -26,6 +26,7 @@ class _MyAppState extends State<MyApp> {
onChanged: print, onChanged: print,
// Initial selection and favorite can be one of code ('IT') OR dial_code('+39') // Initial selection and favorite can be one of code ('IT') OR dial_code('+39')
initialSelection: 'IT', initialSelection: 'IT',
// showCountryOnly: true,
favorite: ['+39', 'FR']), favorite: ['+39', 'FR']),
))); )));
} }

View File

@@ -1,4 +1,3 @@
library country_code_picker; library country_code_picker;
import 'package:country_code_picker/country_code.dart'; import 'package:country_code_picker/country_code.dart';
@@ -67,7 +66,9 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
), ),
Flexible( Flexible(
child: Text( child: Text(
selectedItem.toString(), widget.showCountryOnly
? selectedItem.toCountryStringOnly()
: selectedItem.toString(),
style: widget.textStyle ?? Theme.of(context).textTheme.button, style: widget.textStyle ?? Theme.of(context).textTheme.button,
), ),
), ),
@@ -106,7 +107,8 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
void _showSelectionDialog() { void _showSelectionDialog() {
showDialog( showDialog(
context: context, context: context,
builder: (_) => new SelectionDialog(elements, favoriteElements, showCountryOnly: widget.showCountryOnly), builder: (_) => new SelectionDialog(elements, favoriteElements,
showCountryOnly: widget.showCountryOnly),
).then((e) { ).then((e) {
if (e != null) { if (e != null) {
setState(() { setState(() {

View File

@@ -4,13 +4,12 @@ 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<CountryCode> elements; final List<CountryCode> elements;
bool showCountryOnly; final bool showCountryOnly;
/// elements passed as favorite /// elements passed as favorite
final List<CountryCode> favoriteElements; final List<CountryCode> favoriteElements;
SelectionDialog(this.elements, this.favoriteElements, SelectionDialog(this.elements, this.favoriteElements, {this.showCountryOnly});
{this.showCountryOnly});
@override @override
State<StatefulWidget> createState() => new _SelectionDialogState(); State<StatefulWidget> createState() => new _SelectionDialogState();
@@ -39,31 +38,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
..addAll(widget.favoriteElements ..addAll(widget.favoriteElements
.map( .map(
(f) => new SimpleDialogOption( (f) => new SimpleDialogOption(
child: Flex( child: _buildOption(f),
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(
widget.showCountryOnly
? f.toCountryStringOnly()
: f.toLongString(),
overflow: TextOverflow.fade,
),
),
],
),
onPressed: () { onPressed: () {
_selectItem(f); _selectItem(f);
}, },
@@ -75,30 +50,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
.map( .map(
(e) => new SimpleDialogOption( (e) => new SimpleDialogOption(
key: Key(e.toLongString()), key: Key(e.toLongString()),
child: Flex( child: _buildOption(e),
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(
widget.showCountryOnly
? e.toCountryStringOnly()
: e.toLongString(),
overflow: TextOverflow.fade,
),
),
],
),
onPressed: () { onPressed: () {
_selectItem(e); _selectItem(e);
}, },
@@ -106,6 +58,32 @@ class _SelectionDialogState extends State<SelectionDialog> {
) )
.toList())); .toList()));
Widget _buildOption(CountryCode e) {
return 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(
flex: 10,
fit: FlexFit.tight,
child: Text(
widget.showCountryOnly ? e.toCountryStringOnly() : e.toLongString(),
overflow: TextOverflow.fade,
),
),
],
);
}
@override @override
void initState() { void initState() {
showedElements = widget.elements; showedElements = widget.elements;