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

Update country_code_picker.dart #124

Merged
merged 2 commits into from
Dec 11, 2020
Merged
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
57 changes: 39 additions & 18 deletions lib/country_code_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class CountryCodePicker extends StatefulWidget {
/// Set to true if you want to hide the search part
final bool hideSearch;

/// Set to true if you want to show drop down button
final bool showDropDownButton;

CountryCodePicker({
this.onChanged,
this.onInit,
Expand Down Expand Up @@ -99,6 +102,7 @@ class CountryCodePicker extends StatefulWidget {
this.comparator,
this.countryFilter,
this.hideSearch = false,
this.showDropDownButton = false,
this.dialogSize,
this.dialogBackgroundColor,
this.closeIcon = const Icon(Icons.close),
Expand All @@ -110,20 +114,20 @@ class CountryCodePicker extends StatefulWidget {
List<Map> jsonList = codes;

List<CountryCode> elements =
jsonList.map((json) => CountryCode.fromJson(json)).toList();
jsonList.map((json) => CountryCode.fromJson(json)).toList();

if (comparator != null) {
elements.sort(comparator);
}

if (countryFilter != null && countryFilter.isNotEmpty) {
final uppercaseCustomList =
countryFilter.map((c) => c.toUpperCase()).toList();
countryFilter.map((c) => c.toUpperCase()).toList();
elements = elements
.where((c) =>
uppercaseCustomList.contains(c.code) ||
uppercaseCustomList.contains(c.name) ||
uppercaseCustomList.contains(c.dialCode))
uppercaseCustomList.contains(c.code) ||
uppercaseCustomList.contains(c.name) ||
uppercaseCustomList.contains(c.dialCode))
.toList();
}

Expand Down Expand Up @@ -154,6 +158,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
direction: Axis.horizontal,
mainAxisSize: MainAxisSize.min,
children: <Widget>[

if (widget.showFlagMain != null
? widget.showFlagMain
: widget.showFlag)
Expand All @@ -171,17 +176,33 @@ class CountryCodePickerState extends State<CountryCodePicker> {
),
),
),

if (!widget.hideMainText)
Flexible(
fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose,
child: Text(
widget.showOnlyCountryWhenClosed
? selectedItem.toCountryStringOnly()
: selectedItem.toString(),
style: widget.textStyle ?? Theme.of(context).textTheme.button,
style: widget.textStyle ?? Theme
.of(context)
.textTheme
.button,
overflow: widget.textOverflow,
),
),
if(widget.showDropDownButton)
Flexible(
flex: widget.alignLeft ? 0 : 1,
fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose,
child: Padding(
padding: widget.alignLeft
? const EdgeInsets.only(right: 16.0, left: 8.0)
: const EdgeInsets.only(right: 16.0),
child: Icon(Icons.arrow_drop_down, color: Colors.grey,
size: widget.flagWidth,)
),
),
],
),
);
Expand All @@ -204,9 +225,9 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (oldWidget.initialSelection != widget.initialSelection) {
if (widget.initialSelection != null) {
selectedItem = elements.firstWhere(
(e) =>
(e.code.toUpperCase() ==
widget.initialSelection.toUpperCase()) ||
(e) =>
(e.code.toUpperCase() ==
widget.initialSelection.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name.toUpperCase() == widget.initialSelection.toUpperCase()),
orElse: () => elements[0]);
Expand All @@ -223,8 +244,8 @@ class CountryCodePickerState extends State<CountryCodePicker> {

if (widget.initialSelection != null) {
selectedItem = elements.firstWhere(
(e) =>
(e.code.toUpperCase() == widget.initialSelection.toUpperCase()) ||
(e) =>
(e.code.toUpperCase() == widget.initialSelection.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name.toUpperCase() == widget.initialSelection.toUpperCase()),
orElse: () => elements[0]);
Expand All @@ -234,13 +255,13 @@ class CountryCodePickerState extends State<CountryCodePicker> {

favoriteElements = elements
.where((e) =>
widget.favorite.firstWhere(
(f) =>
e.code.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name.toUpperCase() == f.toUpperCase(),
orElse: () => null) !=
null)
widget.favorite.firstWhere(
(f) =>
e.code.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name.toUpperCase() == f.toUpperCase(),
orElse: () => null) !=
null)
.toList();
}

Expand Down