From 9faf5a79cf9192eb0dc48bb745cc3036cd0b58b2 Mon Sep 17 00:00:00 2001 From: Nilashish Roy Date: Fri, 7 Feb 2025 23:00:18 +0600 Subject: [PATCH] Fixed the Issue #48. Localization is hardcoded to english since 3.2.0 #48 Added the localization use and boolean --- example/lib/main.dart | 1 + lib/country_code_picker.dart | 46 +++++++++--------------------- lib/src/country_localizations.dart | 23 +++++++++------ 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 9318ecb..30fe4db 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -89,6 +89,7 @@ class MyAppState extends State { ], localizationsDelegates: const [ CountryLocalizations.delegate, + /// CountryLocalizations.getDelegate(enableLocalization: false), // For no localization only english just declare delegate this way. GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], diff --git a/lib/country_code_picker.dart b/lib/country_code_picker.dart index 3d6f320..c4b5260 100644 --- a/lib/country_code_picker.dart +++ b/lib/country_code_picker.dart @@ -152,16 +152,13 @@ class CountryCodePicker extends StatefulWidget { this.closeIcon = const Icon(Icons.close), this.countryList = codes, this.pickerStyle = PickerStyle.dialog, - this.dialogItemPadding = - const EdgeInsets.symmetric(horizontal: 24, vertical: 8), + this.dialogItemPadding = const EdgeInsets.symmetric(horizontal: 24, vertical: 8), this.searchPadding = const EdgeInsets.symmetric(horizontal: 24), this.headerAlignment = MainAxisAlignment.spaceBetween, this.headerText = "Select Country", - this.headerTextStyle = - const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), + this.headerTextStyle = const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), this.hideHeaderText = false, - this.topBarPadding = - const EdgeInsets.symmetric(vertical: 5.0, horizontal: 20), + this.topBarPadding = const EdgeInsets.symmetric(vertical: 5.0, horizontal: 20), Key? key, }) : super(key: key); @@ -170,16 +167,14 @@ class CountryCodePicker extends StatefulWidget { State createState() { List> jsonList = countryList; - List elements = - jsonList.map((json) => CountryCode.fromJson(json)).toList(); + List elements = jsonList.map((json) => CountryCode.fromJson(json)).toList(); if (comparator != null) { elements.sort(comparator); } if (countryFilter != null && countryFilter!.isNotEmpty) { - final uppercaseCustomList = - countryFilter!.map((criteria) => criteria.toUpperCase()).toList(); + final uppercaseCustomList = countryFilter!.map((criteria) => criteria.toUpperCase()).toList(); elements = elements .where((criteria) => uppercaseCustomList.contains(criteria.code) || @@ -205,9 +200,7 @@ class CountryCodePickerState extends State { Widget internalWidget; if (widget.builder != null) { internalWidget = InkWell( - onTap: pickerStyle == PickerStyle.dialog - ? showCountryCodePickerDialog - : showCountryCodePickerBottomSheet, + onTap: pickerStyle == PickerStyle.dialog ? showCountryCodePickerDialog : showCountryCodePickerBottomSheet, child: widget.builder!(selectedItem), ); } else { @@ -223,16 +216,12 @@ class CountryCodePickerState extends State { direction: Axis.horizontal, mainAxisSize: MainAxisSize.min, children: [ - if (widget.showFlagMain != null - ? widget.showFlagMain! - : widget.showFlag) + if (widget.showFlagMain != null ? widget.showFlagMain! : widget.showFlag) Flexible( flex: widget.alignLeft ? 0 : 1, fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, child: Container( - clipBehavior: widget.flagDecoration == null - ? Clip.none - : Clip.hardEdge, + clipBehavior: widget.flagDecoration == null ? Clip.none : Clip.hardEdge, decoration: widget.flagDecoration, margin: widget.margin ?? (widget.alignLeft @@ -249,11 +238,8 @@ class CountryCodePickerState extends State { Flexible( fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose, child: Text( - widget.showOnlyCountryWhenClosed - ? selectedItem!.toCountryStringOnly() - : selectedItem.toString(), - style: widget.textStyle ?? - Theme.of(context).textTheme.labelLarge, + widget.showOnlyCountryWhenClosed ? selectedItem!.toCountryStringOnly() : selectedItem.toString(), + style: widget.textStyle ?? Theme.of(context).textTheme.labelLarge, overflow: widget.textOverflow, ), ), @@ -295,11 +281,9 @@ class CountryCodePickerState extends State { if (widget.initialSelection != null) { selectedItem = elements.firstWhere( (criteria) => - (criteria.code!.toUpperCase() == - widget.initialSelection!.toUpperCase()) || + (criteria.code!.toUpperCase() == widget.initialSelection!.toUpperCase()) || (criteria.dialCode == widget.initialSelection) || - (criteria.name!.toUpperCase() == - widget.initialSelection!.toUpperCase()), + (criteria.name!.toUpperCase() == widget.initialSelection!.toUpperCase()), orElse: () => elements[0]); } else { selectedItem = elements[0]; @@ -315,11 +299,9 @@ class CountryCodePickerState extends State { if (widget.initialSelection != null) { selectedItem = elements.firstWhere( (item) => - (item.code!.toUpperCase() == - widget.initialSelection!.toUpperCase()) || + (item.code!.toUpperCase() == widget.initialSelection!.toUpperCase()) || (item.dialCode == widget.initialSelection) || - (item.name!.toUpperCase() == - widget.initialSelection!.toUpperCase()), + (item.name!.toUpperCase() == widget.initialSelection!.toUpperCase()), orElse: () => elements[0]); } else { selectedItem = elements[0]; diff --git a/lib/src/country_localizations.dart b/lib/src/country_localizations.dart index ec24d2e..f0bceac 100644 --- a/lib/src/country_localizations.dart +++ b/lib/src/country_localizations.dart @@ -15,14 +15,17 @@ class CountryLocalizations { ); } - static const LocalizationsDelegate delegate = - _CountryLocalizationsDelegate(); + static const LocalizationsDelegate delegate = _CountryLocalizationsDelegate(); + + static LocalizationsDelegate getDelegate({bool enableLocalization = true}) { + return _CountryLocalizationsDelegate(enableLocalization: enableLocalization); + } late Map _localizedStrings; Future load() async { - String jsonString = await rootBundle.loadString( - 'packages/country_code_picker/src/i18n/${locale.languageCode}.json'); + String jsonString = + await rootBundle.loadString('packages/country_code_picker/src/i18n/${locale.languageCode}.json'); Map jsonMap = json.decode(jsonString); _localizedStrings = jsonMap.map((key, value) { @@ -37,9 +40,10 @@ class CountryLocalizations { } } -class _CountryLocalizationsDelegate - extends LocalizationsDelegate { - const _CountryLocalizationsDelegate(); +class _CountryLocalizationsDelegate extends LocalizationsDelegate { + final bool enableLocalization; + + const _CountryLocalizationsDelegate({this.enableLocalization = true}); @override bool isSupported(Locale locale) { @@ -119,7 +123,10 @@ class _CountryLocalizationsDelegate @override Future load(Locale locale) async { - CountryLocalizations localizations = CountryLocalizations(const Locale('en'));//locale); + // Use the provided locale if localization is enabled; otherwise, use English. + Locale effectiveLocale = enableLocalization ? locale : const Locale('en'); + + CountryLocalizations localizations = CountryLocalizations(effectiveLocale); await localizations.load(); return localizations; }