diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index fa9d6c5..e62cdbb 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -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 { 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(() { diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index feceafd..a786f80 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -6,61 +6,74 @@ class SelectionDialog extends StatefulWidget { final List elements; final bool showCountryOnly; final InputDecoration searchDecoration; + final WidgetBuilder emptySearchBuilder; /// elements passed as favorite final List 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 createState() => new _SelectionDialogState(); + State createState() => _SelectionDialogState(); } class _SelectionDialogState extends State { /// this is useful for filtering purpose - List showedElements = []; + List filteredElements; @override - Widget build(BuildContext context) => new SimpleDialog( - title: new Column( + Widget build(BuildContext context) => SimpleDialog( + title: Column( children: [ - new TextField( + TextField( decoration: widget.searchDecoration, onChanged: _filterElements, ), ], ), children: [ - widget.favoriteElements.isEmpty - ? new Container() - : new Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [] - ..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: [] + ..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 { ); } + 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) ||