Update country_code_picker.dart

This commit is contained in:
Sherif Hasan
2020-11-05 14:07:24 +02:00
committed by GitHub
parent c283da595f
commit a99c34e2ac

View File

@@ -68,6 +68,9 @@ class CountryCodePicker extends StatefulWidget {
/// Set to true if you want to hide the search part /// Set to true if you want to hide the search part
final bool hideSearch; final bool hideSearch;
/// Set to true if you want to show drop down button
final bool showDropDownButton;
CountryCodePicker({ CountryCodePicker({
this.onChanged, this.onChanged,
this.onInit, this.onInit,
@@ -96,6 +99,7 @@ class CountryCodePicker extends StatefulWidget {
this.comparator, this.comparator,
this.countryFilter, this.countryFilter,
this.hideSearch = false, this.hideSearch = false,
this.showDropDownButton = false,
this.dialogSize, this.dialogSize,
this.closeIcon = const Icon(Icons.close), this.closeIcon = const Icon(Icons.close),
Key key, Key key,
@@ -106,7 +110,7 @@ class CountryCodePicker extends StatefulWidget {
List<Map> jsonList = codes; List<Map> jsonList = codes;
List<CountryCode> elements = List<CountryCode> elements =
jsonList.map((json) => CountryCode.fromJson(json)).toList(); jsonList.map((json) => CountryCode.fromJson(json)).toList();
if (comparator != null) { if (comparator != null) {
elements.sort(comparator); elements.sort(comparator);
@@ -114,12 +118,12 @@ class CountryCodePicker extends StatefulWidget {
if (countryFilter != null && countryFilter.isNotEmpty) { if (countryFilter != null && countryFilter.isNotEmpty) {
final uppercaseCustomList = final uppercaseCustomList =
countryFilter.map((c) => c.toUpperCase()).toList(); countryFilter.map((c) => c.toUpperCase()).toList();
elements = elements elements = elements
.where((c) => .where((c) =>
uppercaseCustomList.contains(c.code) || uppercaseCustomList.contains(c.code) ||
uppercaseCustomList.contains(c.name) || uppercaseCustomList.contains(c.name) ||
uppercaseCustomList.contains(c.dialCode)) uppercaseCustomList.contains(c.dialCode))
.toList(); .toList();
} }
@@ -150,6 +154,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
direction: Axis.horizontal, direction: Axis.horizontal,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
if (widget.showFlagMain != null if (widget.showFlagMain != null
? widget.showFlagMain ? widget.showFlagMain
: widget.showFlag) : widget.showFlag)
@@ -167,6 +172,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
), ),
), ),
), ),
if (!widget.hideMainText) if (!widget.hideMainText)
Flexible( Flexible(
fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose,
@@ -174,10 +180,25 @@ class CountryCodePickerState extends State<CountryCodePicker> {
widget.showOnlyCountryWhenClosed widget.showOnlyCountryWhenClosed
? selectedItem.toCountryStringOnly() ? selectedItem.toCountryStringOnly()
: selectedItem.toString(), : selectedItem.toString(),
style: widget.textStyle ?? Theme.of(context).textTheme.button, style: widget.textStyle ?? Theme
.of(context)
.textTheme
.button,
overflow: widget.textOverflow, 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,)
),
),
], ],
), ),
); );
@@ -200,9 +221,9 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (oldWidget.initialSelection != widget.initialSelection) { if (oldWidget.initialSelection != widget.initialSelection) {
if (widget.initialSelection != null) { if (widget.initialSelection != null) {
selectedItem = elements.firstWhere( selectedItem = elements.firstWhere(
(e) => (e) =>
(e.code.toUpperCase() == (e.code.toUpperCase() ==
widget.initialSelection.toUpperCase()) || widget.initialSelection.toUpperCase()) ||
(e.dialCode == widget.initialSelection) || (e.dialCode == widget.initialSelection) ||
(e.name.toUpperCase() == widget.initialSelection.toUpperCase()), (e.name.toUpperCase() == widget.initialSelection.toUpperCase()),
orElse: () => elements[0]); orElse: () => elements[0]);
@@ -219,8 +240,8 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (widget.initialSelection != null) { if (widget.initialSelection != null) {
selectedItem = elements.firstWhere( selectedItem = elements.firstWhere(
(e) => (e) =>
(e.code.toUpperCase() == widget.initialSelection.toUpperCase()) || (e.code.toUpperCase() == widget.initialSelection.toUpperCase()) ||
(e.dialCode == widget.initialSelection) || (e.dialCode == widget.initialSelection) ||
(e.name.toUpperCase() == widget.initialSelection.toUpperCase()), (e.name.toUpperCase() == widget.initialSelection.toUpperCase()),
orElse: () => elements[0]); orElse: () => elements[0]);
@@ -230,13 +251,13 @@ class CountryCodePickerState extends State<CountryCodePicker> {
favoriteElements = elements favoriteElements = elements
.where((e) => .where((e) =>
widget.favorite.firstWhere( widget.favorite.firstWhere(
(f) => (f) =>
e.code.toUpperCase() == f.toUpperCase() || e.code.toUpperCase() == f.toUpperCase() ||
e.dialCode == f || e.dialCode == f ||
e.name.toUpperCase() == f.toUpperCase(), e.name.toUpperCase() == f.toUpperCase(),
orElse: () => null) != orElse: () => null) !=
null) null)
.toList(); .toList();
} }
@@ -245,25 +266,26 @@ class CountryCodePickerState extends State<CountryCodePicker> {
barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5), barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5),
backgroundColor: widget.backgroundColor ?? Colors.transparent, backgroundColor: widget.backgroundColor ?? Colors.transparent,
context: context, context: context,
builder: (context) => Center( builder: (context) =>
child: SelectionDialog( Center(
elements, child: SelectionDialog(
favoriteElements, elements,
showCountryOnly: widget.showCountryOnly, favoriteElements,
emptySearchBuilder: widget.emptySearchBuilder, showCountryOnly: widget.showCountryOnly,
searchDecoration: widget.searchDecoration, emptySearchBuilder: widget.emptySearchBuilder,
searchStyle: widget.searchStyle, searchDecoration: widget.searchDecoration,
textStyle: widget.dialogTextStyle, searchStyle: widget.searchStyle,
boxDecoration: widget.boxDecoration, textStyle: widget.dialogTextStyle,
showFlag: widget.showFlagDialog != null boxDecoration: widget.boxDecoration,
? widget.showFlagDialog showFlag: widget.showFlagDialog != null
: widget.showFlag, ? widget.showFlagDialog
flagWidth: widget.flagWidth, : widget.showFlag,
size: widget.dialogSize, flagWidth: widget.flagWidth,
hideSearch: widget.hideSearch, size: widget.dialogSize,
closeIcon: widget.closeIcon, hideSearch: widget.hideSearch,
), closeIcon: widget.closeIcon,
), ),
),
).then((e) { ).then((e) {
if (e != null) { if (e != null) {
setState(() { setState(() {