Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
fix search with localization and move elemen localization
Browse files Browse the repository at this point in the history
  • Loading branch information
imtoori committed Apr 4, 2020
1 parent 16f09e5 commit 1e9495c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 21 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _MyAppState extends State<MyApp> {
supportedLocales: [
Locale('en'),
Locale('it'),
Locale('en'),
Locale('fr'),
],
localizationsDelegates: [
CountryLocalizations.delegate,
Expand Down Expand Up @@ -72,7 +72,7 @@ class _MyAppState extends State<MyApp> {
child: Padding(
padding: const EdgeInsets.all(8.0),
child: CountryCodePicker(
onChanged: print,
onChanged: (e) => print(e.toLongString()),
initialSelection: 'TF',
showCountryOnly: true,
showOnlyCountryWhenClosed: true,
Expand Down
8 changes: 2 additions & 6 deletions lib/country_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
16 changes: 5 additions & 11 deletions lib/country_code_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class CountryCodePicker extends StatefulWidget {
/// Use this property to change the order of the options
final Comparator<CountryCode> comparator;

// Enable filtering countries in the dialog using localizes string
final bool filterByLocale;

CountryCodePicker({
this.onChanged,
this.onInit,
Expand All @@ -77,7 +74,6 @@ class CountryCodePicker extends StatefulWidget {
this.textOverflow = TextOverflow.ellipsis,
this.comparator,
this.customList,
this.filterByLocale = false,
});

@override
Expand Down Expand Up @@ -117,14 +113,11 @@ class _CountryCodePickerState extends State<CountryCodePicker> {

@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(
Expand Down Expand Up @@ -153,7 +146,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
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,
Expand All @@ -170,6 +163,7 @@ class _CountryCodePickerState extends State<CountryCodePicker> {
void didUpdateWidget(CountryCodePicker oldWidget) {
super.didUpdateWidget(oldWidget);

this.elements = elements.map((e) => e.localize(context)).toList();
_onInit(selectedItem);

if (oldWidget.initialSelection != widget.initialSelection) {
Expand Down Expand Up @@ -234,13 +228,13 @@ class _CountryCodePickerState extends State<CountryCodePicker> {

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);
}
}
}
4 changes: 2 additions & 2 deletions lib/selection_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class _SelectionDialogState extends State<SelectionDialog> {
flex: 4,
child: Text(
widget.showCountryOnly
? e.toCountryStringOnly(context)
: e.toLongString(context),
? e.toCountryStringOnly()
: e.toLongString(),
overflow: TextOverflow.fade,
),
),
Expand Down

0 comments on commit 1e9495c

Please sign in to comment.