From 1e9495cf249a82468f0f45552f341ab8eb884076 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Sat, 4 Apr 2020 14:13:04 +0200 Subject: [PATCH] fix search with localization and move elemen localization --- example/lib/main.dart | 4 ++-- lib/country_code.dart | 8 ++------ lib/country_code_picker.dart | 16 +++++----------- lib/selection_dialog.dart | 4 ++-- 4 files changed, 11 insertions(+), 21 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index c65f5d5..0c0ee7f 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -22,7 +22,7 @@ class _MyAppState extends State { supportedLocales: [ Locale('en'), Locale('it'), - Locale('en'), + Locale('fr'), ], localizationsDelegates: [ CountryLocalizations.delegate, @@ -72,7 +72,7 @@ class _MyAppState extends State { child: Padding( padding: const EdgeInsets.all(8.0), child: CountryCodePicker( - onChanged: print, + onChanged: (e) => print(e.toLongString()), initialSelection: 'TF', showCountryOnly: true, showOnlyCountryWhenClosed: true, diff --git a/lib/country_code.dart b/lib/country_code.dart index 13c1381..627b0c6 100644 --- a/lib/country_code.dart +++ b/lib/country_code.dart @@ -59,13 +59,9 @@ class CountryCode { @override String toString() => "$dialCode"; - String toLongString([BuildContext context]) => - "$dialCode ${toCountryStringOnly(context)}"; + String toLongString() => "$dialCode ${toCountryStringOnly()}"; - String toCountryStringOnly([BuildContext context]) { - if (context != null) { - return CountryLocalizations.of(context)?.translate(code) ?? name; - } + String toCountryStringOnly() { return '$name'; } } diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index f3fb1a1..063244b 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -51,9 +51,6 @@ class CountryCodePicker extends StatefulWidget { /// Use this property to change the order of the options final Comparator comparator; - // Enable filtering countries in the dialog using localizes string - final bool filterByLocale; - CountryCodePicker({ this.onChanged, this.onInit, @@ -77,7 +74,6 @@ class CountryCodePicker extends StatefulWidget { this.textOverflow = TextOverflow.ellipsis, this.comparator, this.customList, - this.filterByLocale = false, }); @override @@ -117,14 +113,11 @@ class _CountryCodePickerState extends State { @override Widget build(BuildContext context) { - if (widget.filterByLocale) { - this.elements = elements.map((e) => e.localize(context)).toList(); - } Widget _widget; if (widget.builder != null) _widget = InkWell( onTap: _showSelectionDialog, - child: widget.builder(selectedItem.localize(context)), + child: widget.builder(selectedItem), ); else { _widget = FlatButton( @@ -153,7 +146,7 @@ class _CountryCodePickerState extends State { fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, child: Text( widget.showOnlyCountryWhenClosed - ? selectedItem.toCountryStringOnly(context) + ? selectedItem.toCountryStringOnly() : selectedItem.toString(), style: widget.textStyle ?? Theme.of(context).textTheme.button, overflow: widget.textOverflow, @@ -170,6 +163,7 @@ class _CountryCodePickerState extends State { void didUpdateWidget(CountryCodePicker oldWidget) { super.didUpdateWidget(oldWidget); + this.elements = elements.map((e) => e.localize(context)).toList(); _onInit(selectedItem); if (oldWidget.initialSelection != widget.initialSelection) { @@ -234,13 +228,13 @@ class _CountryCodePickerState extends State { void _publishSelection(CountryCode e) { if (widget.onChanged != null) { - widget.onChanged(e.localize(context)); + widget.onChanged(e); } } void _onInit(CountryCode e) { if (widget.onInit != null) { - widget.onInit(e.localize(context)); + widget.onInit(e); } } } diff --git a/lib/selection_dialog.dart b/lib/selection_dialog.dart index eb010c4..ac46393 100644 --- a/lib/selection_dialog.dart +++ b/lib/selection_dialog.dart @@ -123,8 +123,8 @@ class _SelectionDialogState extends State { flex: 4, child: Text( widget.showCountryOnly - ? e.toCountryStringOnly(context) - : e.toLongString(context), + ? e.toCountryStringOnly() + : e.toLongString(), overflow: TextOverflow.fade, ), ),