Skip to content
This repository was archived by the owner on Jul 20, 2022. It is now read-only.

fixed search filter in favorites. issue #159 #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 106 additions & 98 deletions lib/selection_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ class SelectionDialog extends StatefulWidget {
final List<CountryCode> favoriteElements;

SelectionDialog(
this.elements,
this.favoriteElements, {
Key? key,
this.showCountryOnly,
this.emptySearchBuilder,
InputDecoration searchDecoration = const InputDecoration(),
this.searchStyle,
this.textStyle,
this.boxDecoration,
this.showFlag,
this.flagDecoration,
this.flagWidth = 32,
this.size,
this.backgroundColor,
this.barrierColor,
this.hideSearch = false,
this.closeIcon,
}) : this.searchDecoration = searchDecoration.prefixIcon == null
? searchDecoration.copyWith(prefixIcon: Icon(Icons.search))
: searchDecoration,
this.elements,
this.favoriteElements, {
Key? key,
this.showCountryOnly,
this.emptySearchBuilder,
InputDecoration searchDecoration = const InputDecoration(),
this.searchStyle,
this.textStyle,
this.boxDecoration,
this.showFlag,
this.flagDecoration,
this.flagWidth = 32,
this.size,
this.backgroundColor,
this.barrierColor,
this.hideSearch = false,
this.closeIcon,
}) : this.searchDecoration = searchDecoration.prefixIcon == null
? searchDecoration.copyWith(prefixIcon: Icon(Icons.search))
: searchDecoration,
super(key: key);

@override
Expand All @@ -57,84 +57,85 @@ class SelectionDialog extends StatefulWidget {
class _SelectionDialogState extends State<SelectionDialog> {
/// this is useful for filtering purpose
late List<CountryCode> filteredElements;
late List<CountryCode> filteredFavoriteElements;

@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.all(0.0),
child: Container(
clipBehavior: Clip.hardEdge,
width: widget.size?.width ?? MediaQuery.of(context).size.width,
height:
widget.size?.height ?? MediaQuery.of(context).size.height * 0.85,
decoration: widget.boxDecoration ??
BoxDecoration(
color: widget.backgroundColor ?? Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8.0)),
boxShadow: [
BoxShadow(
color: widget.barrierColor ?? Colors.grey.withOpacity(1),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
],
padding: const EdgeInsets.all(0.0),
child: Container(
clipBehavior: Clip.hardEdge,
width: widget.size?.width ?? MediaQuery.of(context).size.width,
height:
widget.size?.height ?? MediaQuery.of(context).size.height * 0.85,
decoration: widget.boxDecoration ??
BoxDecoration(
color: widget.backgroundColor ?? Colors.white,
borderRadius: BorderRadius.all(Radius.circular(8.0)),
boxShadow: [
BoxShadow(
color: widget.barrierColor ?? Colors.grey.withOpacity(1),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3), // changes position of shadow
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
IconButton(
padding: const EdgeInsets.all(0),
iconSize: 20,
icon: widget.closeIcon!,
onPressed: () => Navigator.pop(context),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
IconButton(
padding: const EdgeInsets.all(0),
iconSize: 20,
icon: widget.closeIcon!,
onPressed: () => Navigator.pop(context),
),
if (!widget.hideSearch)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: TextField(
style: widget.searchStyle,
decoration: widget.searchDecoration,
onChanged: _applyFilter,
),
if (!widget.hideSearch)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: TextField(
style: widget.searchStyle,
decoration: widget.searchDecoration,
onChanged: _filterElements,
),
),
Expanded(
child: ListView(
),
Expanded(
child: ListView(
children: [
filteredFavoriteElements.isEmpty
? const DecoratedBox(decoration: BoxDecoration())
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
widget.favoriteElements.isEmpty
? const DecoratedBox(decoration: BoxDecoration())
: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
...widget.favoriteElements.map(
(f) => SimpleDialogOption(
child: _buildOption(f),
onPressed: () {
_selectItem(f);
},
),
),
const Divider(),
],
),
if (filteredElements.isEmpty)
_buildEmptySearchWidget(context)
else
...filteredElements.map(
(e) => SimpleDialogOption(
child: _buildOption(e),
onPressed: () {
_selectItem(e);
},
),
...filteredFavoriteElements.map(
(f) => SimpleDialogOption(
child: _buildOption(f),
onPressed: () {
_selectItem(f);
},
),
),
const Divider(),
],
),
),
],
if (filteredElements.isEmpty)
_buildEmptySearchWidget(context)
else
...filteredElements.map(
(e) => SimpleDialogOption(
child: _buildOption(e),
onPressed: () {
_selectItem(e);
},
),
),
],
),
),
),
);
],
),
),
);

Widget _buildOption(CountryCode e) {
return Container(
Expand All @@ -148,7 +149,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
margin: const EdgeInsets.only(right: 16.0),
decoration: widget.flagDecoration,
clipBehavior:
widget.flagDecoration == null ? Clip.none : Clip.hardEdge,
widget.flagDecoration == null ? Clip.none : Clip.hardEdge,
child: Image.asset(
e.flagUri!,
package: 'country_code_picker',
Expand Down Expand Up @@ -184,22 +185,29 @@ class _SelectionDialogState extends State<SelectionDialog> {

@override
void initState() {
filteredElements = widget.elements;
_applyFilter("");

super.initState();
}

void _filterElements(String s) {
s = s.toUpperCase();
void _applyFilter(String s) {
setState(() {
filteredElements = widget.elements
.where((e) =>
e.code!.contains(s) ||
e.dialCode!.contains(s) ||
e.name!.toUpperCase().contains(s))
.toList();
filteredElements = _filterList(s, widget.elements);
filteredFavoriteElements = _filterList(s, widget.favoriteElements);
});
}

List<CountryCode> _filterList(String s, List<CountryCode> list) {
s = s.toUpperCase();
List<CountryCode> filteredList = list
.where((e) =>
e.code!.contains(s) ||
e.dialCode!.contains(s) ||
e.name!.toUpperCase().contains(s))
.toList();
return filteredList;
}

void _selectItem(CountryCode e) {
Navigator.pop(context, e);
}
Expand Down