fix search with localization and move elemen localization

This commit is contained in:
Salvatore Giordano
2020-04-04 14:13:04 +02:00
parent 16f09e5ecf
commit 1e9495cf24
4 changed files with 11 additions and 21 deletions

View File

@@ -22,7 +22,7 @@ class _MyAppState extends State<MyApp> {
supportedLocales: [ supportedLocales: [
Locale('en'), Locale('en'),
Locale('it'), Locale('it'),
Locale('en'), Locale('fr'),
], ],
localizationsDelegates: [ localizationsDelegates: [
CountryLocalizations.delegate, CountryLocalizations.delegate,
@@ -72,7 +72,7 @@ class _MyAppState extends State<MyApp> {
child: Padding( child: Padding(
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: CountryCodePicker( child: CountryCodePicker(
onChanged: print, onChanged: (e) => print(e.toLongString()),
initialSelection: 'TF', initialSelection: 'TF',
showCountryOnly: true, showCountryOnly: true,
showOnlyCountryWhenClosed: true, showOnlyCountryWhenClosed: true,

View File

@@ -59,13 +59,9 @@ class CountryCode {
@override @override
String toString() => "$dialCode"; String toString() => "$dialCode";
String toLongString([BuildContext context]) => String toLongString() => "$dialCode ${toCountryStringOnly()}";
"$dialCode ${toCountryStringOnly(context)}";
String toCountryStringOnly([BuildContext context]) { String toCountryStringOnly() {
if (context != null) {
return CountryLocalizations.of(context)?.translate(code) ?? name;
}
return '$name'; return '$name';
} }
} }

View File

@@ -51,9 +51,6 @@ class CountryCodePicker extends StatefulWidget {
/// Use this property to change the order of the options /// Use this property to change the order of the options
final Comparator<CountryCode> comparator; final Comparator<CountryCode> comparator;
// Enable filtering countries in the dialog using localizes string
final bool filterByLocale;
CountryCodePicker({ CountryCodePicker({
this.onChanged, this.onChanged,
this.onInit, this.onInit,
@@ -77,7 +74,6 @@ class CountryCodePicker extends StatefulWidget {
this.textOverflow = TextOverflow.ellipsis, this.textOverflow = TextOverflow.ellipsis,
this.comparator, this.comparator,
this.customList, this.customList,
this.filterByLocale = false,
}); });
@override @override
@@ -117,14 +113,11 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (widget.filterByLocale) {
this.elements = elements.map((e) => e.localize(context)).toList();
}
Widget _widget; Widget _widget;
if (widget.builder != null) if (widget.builder != null)
_widget = InkWell( _widget = InkWell(
onTap: _showSelectionDialog, onTap: _showSelectionDialog,
child: widget.builder(selectedItem.localize(context)), child: widget.builder(selectedItem),
); );
else { else {
_widget = FlatButton( _widget = FlatButton(
@@ -153,7 +146,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose,
child: Text( child: Text(
widget.showOnlyCountryWhenClosed widget.showOnlyCountryWhenClosed
? selectedItem.toCountryStringOnly(context) ? 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,
@@ -170,6 +163,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
void didUpdateWidget(CountryCodePicker oldWidget) { void didUpdateWidget(CountryCodePicker oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
this.elements = elements.map((e) => e.localize(context)).toList();
_onInit(selectedItem); _onInit(selectedItem);
if (oldWidget.initialSelection != widget.initialSelection) { if (oldWidget.initialSelection != widget.initialSelection) {
@@ -234,13 +228,13 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
void _publishSelection(CountryCode e) { void _publishSelection(CountryCode e) {
if (widget.onChanged != null) { if (widget.onChanged != null) {
widget.onChanged(e.localize(context)); widget.onChanged(e);
} }
} }
void _onInit(CountryCode e) { void _onInit(CountryCode e) {
if (widget.onInit != null) { if (widget.onInit != null) {
widget.onInit(e.localize(context)); widget.onInit(e);
} }
} }
} }

View File

@@ -123,8 +123,8 @@ class _SelectionDialogState extends State<SelectionDialog> {
flex: 4, flex: 4,
child: Text( child: Text(
widget.showCountryOnly widget.showCountryOnly
? e.toCountryStringOnly(context) ? e.toCountryStringOnly()
: e.toLongString(context), : e.toLongString(),
overflow: TextOverflow.fade, overflow: TextOverflow.fade,
), ),
), ),