Merge branch 'customized-picker' into feature/styleable-search-field

This commit is contained in:
Andrew Fulton
2019-04-01 22:12:56 -05:00
committed by GitHub
2 changed files with 65 additions and 37 deletions

View File

@@ -15,6 +15,7 @@ class CountryCodePicker extends StatefulWidget {
final EdgeInsetsGeometry padding;
final bool showCountryOnly;
final InputDecoration searchDecoration;
final WidgetBuilder emptySearchBuilder;
CountryCodePicker({
this.onChanged,
@@ -24,6 +25,7 @@ class CountryCodePicker extends StatefulWidget {
this.padding = const EdgeInsets.all(0.0),
this.showCountryOnly = false,
this.searchDecoration,
this.emptySearchBuilder,
});
@override
@@ -109,9 +111,14 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
void _showSelectionDialog() {
showDialog(
context: context,
builder: (_) => new SelectionDialog(elements, favoriteElements,
builder: (_) =>
SelectionDialog(
elements,
favoriteElements,
showCountryOnly: widget.showCountryOnly,
searchDecoration: widget.searchDecoration,),
emptySearchBuilder: widget.emptySearchBuilder,
searchDecoration: widget.searchDecoration,
),
).then((e) {
if (e != null) {
setState(() {

View File

@@ -6,61 +6,74 @@ class SelectionDialog extends StatefulWidget {
final List<CountryCode> elements;
final bool showCountryOnly;
final InputDecoration searchDecoration;
final WidgetBuilder emptySearchBuilder;
/// elements passed as favorite
final List<CountryCode> favoriteElements;
SelectionDialog(this.elements, this.favoriteElements, {
Key key,
this.showCountryOnly,
this.emptySearchBuilder,
InputDecoration searchDecoration = const InputDecoration(),
}) : this.searchDecoration = searchDecoration.copyWith(prefixIcon: Icon(Icons.search));
}) :
this.searchDecoration = searchDecoration.copyWith(prefixIcon: Icon(Icons.search)),
super(key: key);
@override
State<StatefulWidget> createState() => new _SelectionDialogState();
State<StatefulWidget> createState() => _SelectionDialogState();
}
class _SelectionDialogState extends State<SelectionDialog> {
/// this is useful for filtering purpose
List<CountryCode> showedElements = [];
List<CountryCode> filteredElements;
@override
Widget build(BuildContext context) => new SimpleDialog(
title: new Column(
Widget build(BuildContext context) => SimpleDialog(
title: Column(
children: <Widget>[
new TextField(
TextField(
decoration: widget.searchDecoration,
onChanged: _filterElements,
),
],
),
children: [
widget.favoriteElements.isEmpty
? new Container()
: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[]
..addAll(widget.favoriteElements
.map(
(f) => new SimpleDialogOption(
child: _buildOption(f),
onPressed: () {
_selectItem(f);
},
),
)
.toList())
..add(new Divider())),
]..addAll(showedElements
.map(
(e) => new SimpleDialogOption(
key: Key(e.toLongString()),
child: _buildOption(e),
onPressed: () {
_selectItem(e);
},
),
)
.toList()));
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: ListView(
children: [
widget.favoriteElements.isEmpty
? const DecoratedBox(decoration: BoxDecoration())
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[]
..addAll(widget.favoriteElements
.map(
(f) => SimpleDialogOption(
child: _buildOption(f),
onPressed: () {
_selectItem(f);
},
),
)
.toList())
..add(const Divider())),
]..addAll(filteredElements.isEmpty
? [_buildEmptySearchWidget(context)]
: filteredElements.map(
(e) => SimpleDialogOption(
key: Key(e.toLongString()),
child: _buildOption(e),
onPressed: () {
_selectItem(e);
},
)))
)
),
],
);
Widget _buildOption(CountryCode e) {
return Container(
@@ -92,16 +105,24 @@ class _SelectionDialogState extends State<SelectionDialog> {
);
}
Widget _buildEmptySearchWidget(BuildContext context) {
if (widget.emptySearchBuilder != null) {
return widget.emptySearchBuilder(context);
}
return Center(child: Text('No Country Found'));
}
@override
void initState() {
showedElements = widget.elements;
filteredElements = widget.elements;
super.initState();
}
void _filterElements(String s) {
s = s.toUpperCase();
setState(() {
showedElements = widget.elements
filteredElements = widget.elements
.where((e) =>
e.code.contains(s) ||
e.dialCode.contains(s) ||