From 894ffd7c9ea6562b2b33dd7025f729ab1acb8ec2 Mon Sep 17 00:00:00 2001 From: Saif Allah Khaled Date: Thu, 5 Sep 2019 15:47:49 +0200 Subject: [PATCH] enable custom view widgets; --- lib/country_code_picker.dart | 84 ++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index b342ae1..c906672 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -17,6 +17,7 @@ class CountryCodePicker extends StatefulWidget { final InputDecoration searchDecoration; final TextStyle searchStyle; final WidgetBuilder emptySearchBuilder; + final Widget customWidget; /// shows the name of the country instead of the dialcode final bool showOnlyCountryWhenClosed; @@ -43,7 +44,8 @@ class CountryCodePicker extends StatefulWidget { this.emptySearchBuilder, this.showOnlyCountryWhenClosed = false, this.alignLeft = false, - this.showFlag = true + this.showFlag = true, + this.customWidget, }); @override @@ -71,39 +73,49 @@ class _CountryCodePickerState extends State { _CountryCodePickerState(this.elements); @override - Widget build(BuildContext context) => FlatButton( - child: Flex( - direction: Axis.horizontal, - mainAxisSize: MainAxisSize.min, - children: [ - widget.showFlag ? 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: Image.asset( - selectedItem.flagUri, - package: 'country_code_picker', - width: 32.0, - ), - ), - ) : Container(), - Flexible( - fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, - child: Text( - widget.showOnlyCountryWhenClosed - ? selectedItem.toCountryStringOnly() - : selectedItem.toString(), - style: widget.textStyle ?? Theme.of(context).textTheme.button, - ), + Widget build(BuildContext context) { + Widget _widget; + if (widget.customWidget != null) + _widget = widget.customWidget; + else { + _widget = Flex( + direction: Axis.horizontal, + mainAxisSize: MainAxisSize.min, + children: [ + widget.showFlag + ? 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: Image.asset( + selectedItem.flagUri, + package: 'country_code_picker', + width: 32.0, + ), + ), + ) + : Container(), + Flexible( + fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, + child: Text( + widget.showOnlyCountryWhenClosed + ? selectedItem.toCountryStringOnly() + : selectedItem.toString(), + style: widget.textStyle ?? Theme.of(context).textTheme.button, ), - ], - ), - padding: widget.padding, - onPressed: _showSelectionDialog, + ), + ], ); + } + return FlatButton( + child: _widget, + padding: widget.padding, + onPressed: _showSelectionDialog, + ); + } @override initState() { @@ -130,16 +142,12 @@ class _CountryCodePickerState extends State { void _showSelectionDialog() { showDialog( context: context, - builder: (_) => - SelectionDialog( - elements, - favoriteElements, + builder: (_) => SelectionDialog(elements, favoriteElements, showCountryOnly: widget.showCountryOnly, emptySearchBuilder: widget.emptySearchBuilder, searchDecoration: widget.searchDecoration, searchStyle: widget.searchStyle, - showFlag: widget.showFlag - ), + showFlag: widget.showFlag), ).then((e) { if (e != null) { setState(() {