From ded48ce24966901e61c20a12f2301612d8dc6afb Mon Sep 17 00:00:00 2001 From: kechankrisna Date: Tue, 9 Mar 2021 03:05:55 +0700 Subject: [PATCH] switch to a dialog for desktop support --- lib/country_code_picker.dart | 128 ++++++++++++++++++++++++----------- lib/selection_dialog.dart | 4 +- 2 files changed, 89 insertions(+), 43 deletions(-) diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index f3f1b2c..7045116 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -1,5 +1,7 @@ library country_code_picker; +import 'dart:io'; + import 'package:collection/collection.dart' show IterableExtension; import 'package:country_code_picker/country_code.dart'; import 'package:country_code_picker/country_codes.dart'; @@ -114,8 +116,9 @@ class CountryCodePicker extends StatefulWidget { State createState() { List jsonList = codes; - List elements = - jsonList.map((json) => CountryCode.fromJson(json as Map)).toList(); + List elements = jsonList + .map((json) => CountryCode.fromJson(json as Map)) + .toList(); if (comparator != null) { elements.sort(comparator); @@ -227,7 +230,8 @@ class CountryCodePickerState extends State { (e.code!.toUpperCase() == widget.initialSelection!.toUpperCase()) || (e.dialCode == widget.initialSelection) || - (e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()), + (e.name!.toUpperCase() == + widget.initialSelection!.toUpperCase()), orElse: () => elements[0]); } else { selectedItem = elements[0]; @@ -243,7 +247,8 @@ class CountryCodePickerState extends State { if (widget.initialSelection != null) { selectedItem = elements.firstWhere( (e) => - (e.code!.toUpperCase() == widget.initialSelection!.toUpperCase()) || + (e.code!.toUpperCase() == + widget.initialSelection!.toUpperCase()) || (e.dialCode == widget.initialSelection) || (e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()), orElse: () => elements[0]); @@ -253,50 +258,91 @@ class CountryCodePickerState extends State { favoriteElements = elements .where((e) => - widget.favorite.firstWhereOrNull( - (f) => - e.code!.toUpperCase() == f.toUpperCase() || - e.dialCode == f || - e.name!.toUpperCase() == f.toUpperCase()) != + widget.favorite.firstWhereOrNull((f) => + e.code!.toUpperCase() == f.toUpperCase() || + e.dialCode == f || + e.name!.toUpperCase() == f.toUpperCase()) != null) .toList(); } void showCountryCodePickerDialog() { - showMaterialModalBottomSheet( - barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5), - backgroundColor: widget.backgroundColor ?? Colors.transparent, - context: context, - builder: (context) => Center( - child: SelectionDialog( - elements, - favoriteElements, - showCountryOnly: widget.showCountryOnly, - emptySearchBuilder: widget.emptySearchBuilder, - searchDecoration: widget.searchDecoration, - searchStyle: widget.searchStyle, - textStyle: widget.dialogTextStyle, - boxDecoration: widget.boxDecoration, - showFlag: widget.showFlagDialog != null - ? widget.showFlagDialog - : widget.showFlag, - flagWidth: widget.flagWidth, - size: widget.dialogSize, - backgroundColor: widget.dialogBackgroundColor, - barrierColor: widget.barrierColor, - hideSearch: widget.hideSearch, - closeIcon: widget.closeIcon, + if (Platform.isIOS || Platform.isAndroid) { + showMaterialModalBottomSheet( + barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5), + backgroundColor: widget.backgroundColor ?? Colors.transparent, + context: context, + builder: (context) => Center( + child: SelectionDialog( + elements, + favoriteElements, + showCountryOnly: widget.showCountryOnly, + emptySearchBuilder: widget.emptySearchBuilder, + searchDecoration: widget.searchDecoration, + searchStyle: widget.searchStyle, + textStyle: widget.dialogTextStyle, + boxDecoration: widget.boxDecoration, + showFlag: widget.showFlagDialog != null + ? widget.showFlagDialog + : widget.showFlag, + flagWidth: widget.flagWidth, + size: widget.dialogSize, + backgroundColor: widget.dialogBackgroundColor, + barrierColor: widget.barrierColor, + hideSearch: widget.hideSearch, + closeIcon: widget.closeIcon, + ), ), - ), - ).then((e) { - if (e != null) { - setState(() { - selectedItem = e; - }); + ).then((e) { + if (e != null) { + setState(() { + selectedItem = e; + }); - _publishSelection(e); - } - }); + _publishSelection(e); + } + }); + } else { + showDialog( + barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5), + // backgroundColor: widget.backgroundColor ?? Colors.transparent, + context: context, + builder: (context) => Center( + child: Container( + constraints: BoxConstraints(maxHeight: 500, maxWidth: 400), + child: Dialog( + child: SelectionDialog( + elements, + favoriteElements, + showCountryOnly: widget.showCountryOnly, + emptySearchBuilder: widget.emptySearchBuilder, + searchDecoration: widget.searchDecoration, + searchStyle: widget.searchStyle, + textStyle: widget.dialogTextStyle, + boxDecoration: widget.boxDecoration, + showFlag: widget.showFlagDialog != null + ? widget.showFlagDialog + : widget.showFlag, + flagWidth: widget.flagWidth, + size: widget.dialogSize, + backgroundColor: widget.dialogBackgroundColor, + barrierColor: widget.barrierColor, + hideSearch: widget.hideSearch, + closeIcon: widget.closeIcon, + ), + ), + ), + ), + ).then((e) { + if (e != null) { + setState(() { + selectedItem = e; + }); + + _publishSelection(e); + } + }); + } } void _publishSelection(CountryCode e) { diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index 62f8782..2fb323e 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -58,7 +58,7 @@ class _SelectionDialogState extends State { @override Widget build(BuildContext context) => Padding( - padding: const EdgeInsets.all(16.0), + padding: const EdgeInsets.all(0.0), child: Container( clipBehavior: Clip.hardEdge, width: widget.size?.width ?? MediaQuery.of(context).size.width, @@ -67,7 +67,7 @@ class _SelectionDialogState extends State { decoration: widget.boxDecoration ?? BoxDecoration( color: widget.backgroundColor ?? Colors.white, - borderRadius: BorderRadius.all(Radius.circular(25.0)), + borderRadius: BorderRadius.all(Radius.circular(8.0)), boxShadow: [ BoxShadow( color: widget.barrierColor ?? Colors.grey.withOpacity(1),