Skip to content
This repository was archived by the owner on Jul 20, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ Just add the `CountryLocalizations.delegate` in the list of your app delegates
Locale("ur"),
Locale("uz"),
Locale("vi"),
Locale("zh")
Locale("zh"),
Locale("zh-CN"),
Locale("zh-TW")
],
localizationsDelegates: [
CountryLocalizations.delegate,
Expand All @@ -137,32 +139,32 @@ Just add the `CountryLocalizations.delegate` in the list of your app delegates

Here is a list of properties available to customize your widget:

| Name | Type | Description |
|-----|-----|------|
|onChanged| ValueChanged<CountryCode> | callback invoked when the selection changes |
|onInit| ValueChanged<CountryCode> | callback invoked during initialization of the widget |
|initialSelection| String | used to set the initial selected value |
|favorite| List<String> | used to populate the favorite country list |
|textStyle| TextStyle | TextStyle applied to the widget button |
|padding| EdgeInsetsGeometry | the padding applied to the button |
|showCountryOnly| bool | true if you want to see only the countries in the selection dialog |
|searchDecoration| InputDecoration | decoration applied to the TextField search widget |
|searchStyle| TextStyle | style applied to the TextField search widget text |
|emptySearchBuilder| WidgetBuilder | use this to customize the widget used when the search returns 0 elements |
|builder| Function(CountryCode) | use this to build a custom widget instead of the default FlatButton |
|enabled| bool | set to false to disable the widget |
|textOverflow| TextOverflow | the button text overflow behaviour |
|dialogSize| Size | the size of the selection dialog |
|countryFilter| List<String> | uses a list of strings to filter a sublist of countries |
|showOnlyCountryWhenClosed| bool | if true it'll show only the country |
|alignLeft| bool | aligns the flag and the Text to the left |
|showFlag| bool | shows the flag everywhere |
|showFlagMain| bool | shows the flag only when closed |
|showFlagDialog| bool | shows the flag only in dialog |
|flagWidth| double | the width of the flags |
|flagDecoration| Decoration | used for styling the flags |
|comparator| Comparator<CountryCode> | use this to sort the countries in the selection dialog |
|hideSearch| bool | if true the search feature will be disabled |
| Name | Type | Description |
| ------------------------- | ------------------------- | ------------------------------------------------------------------------ |
| onChanged | ValueChanged<CountryCode> | callback invoked when the selection changes |
| onInit | ValueChanged<CountryCode> | callback invoked during initialization of the widget |
| initialSelection | String | used to set the initial selected value |
| favorite | List<String> | used to populate the favorite country list |
| textStyle | TextStyle | TextStyle applied to the widget button |
| padding | EdgeInsetsGeometry | the padding applied to the button |
| showCountryOnly | bool | true if you want to see only the countries in the selection dialog |
| searchDecoration | InputDecoration | decoration applied to the TextField search widget |
| searchStyle | TextStyle | style applied to the TextField search widget text |
| emptySearchBuilder | WidgetBuilder | use this to customize the widget used when the search returns 0 elements |
| builder | Function(CountryCode) | use this to build a custom widget instead of the default FlatButton |
| enabled | bool | set to false to disable the widget |
| textOverflow | TextOverflow | the button text overflow behaviour |
| dialogSize | Size | the size of the selection dialog |
| countryFilter | List<String> | uses a list of strings to filter a sublist of countries |
| showOnlyCountryWhenClosed | bool | if true it'll show only the country |
| alignLeft | bool | aligns the flag and the Text to the left |
| showFlag | bool | shows the flag everywhere |
| showFlagMain | bool | shows the flag only when closed |
| showFlagDialog | bool | shows the flag only in dialog |
| flagWidth | double | the width of the flags |
| flagDecoration | Decoration | used for styling the flags |
| comparator | Comparator<CountryCode> | use this to sort the countries in the selection dialog |
| hideSearch | bool | if true the search feature will be disabled |

## Contributions

Expand Down
4 changes: 3 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class _MyAppState extends State<MyApp> {
Locale("ur"),
Locale("uz"),
Locale("vi"),
Locale("zh")
Locale("zh"),
Locale("zh", "CN"),
Locale("zh", "TW")
],
localizationsDelegates: [
CountryLocalizations.delegate,
Expand Down
10 changes: 6 additions & 4 deletions lib/country_code_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:collection/collection.dart' show IterableExtension;
import 'package:country_code_picker/country_code.dart';
import 'package:country_code_picker/country_codes.dart';
import 'package:country_code_picker/selection_dialog.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:universal_platform/universal_platform.dart';
Expand Down Expand Up @@ -43,6 +42,8 @@ class CountryCodePicker extends StatefulWidget {
/// Background color of selection dialog
final Color? dialogBackgroundColor;

final Color? dialogBorderColor;

/// used to customize the country list
final List<String>? countryFilter;

Expand Down Expand Up @@ -116,6 +117,7 @@ class CountryCodePicker extends StatefulWidget {
this.showDropDownButton = false,
this.dialogSize,
this.dialogBackgroundColor,
this.dialogBorderColor,
this.closeIcon = const Icon(Icons.close),
this.countryList = codes,
Key? key,
Expand Down Expand Up @@ -199,8 +201,8 @@ class CountryCodePickerState extends State<CountryCodePicker> {
widget.showOnlyCountryWhenClosed
? selectedItem!.toCountryStringOnly()
: selectedItem.toString(),
style:
widget.textStyle ?? Theme.of(context).textTheme.button,
style: widget.textStyle ??
Theme.of(context).textTheme.bodyMedium,
overflow: widget.textOverflow,
),
),
Expand Down Expand Up @@ -285,7 +287,6 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (!UniversalPlatform.isAndroid && !UniversalPlatform.isIOS) {
showDialog(
barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5),
// backgroundColor: widget.backgroundColor ?? Colors.transparent,
context: context,
builder: (context) => Center(
child: Container(
Expand All @@ -306,6 +307,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
flagWidth: widget.flagWidth,
size: widget.dialogSize,
backgroundColor: widget.dialogBackgroundColor,
borderColor: widget.dialogBorderColor,
barrierColor: widget.barrierColor,
hideSearch: widget.hideSearch,
closeIcon: widget.closeIcon,
Expand Down
170 changes: 95 additions & 75 deletions lib/country_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ class CountryLocalizations {
);
}

static const LocalizationsDelegate<CountryLocalizations> delegate =
static const _CountryLocalizationsDelegate delegate =
_CountryLocalizationsDelegate();

late Map<String, String> _localizedStrings;

Future<bool> load() async {
String jsonString = await rootBundle.loadString(
'packages/country_code_picker/i18n/${locale.languageCode}.json');
String fileName = delegate.resolveLocale(locale);
String jsonString = await rootBundle
.loadString('packages/country_code_picker/i18n/$fileName.json');
Map<String, dynamic> jsonMap = json.decode(jsonString);

_localizedStrings = jsonMap.map((key, value) {
Expand All @@ -43,78 +44,22 @@ class _CountryLocalizationsDelegate

@override
bool isSupported(Locale locale) {
return [
"af",
"am",
"ar",
"az",
"be",
"bg",
"bn",
"bs",
"ca",
"cs",
"da",
"de",
"el",
"en",
"es",
"et",
"fa",
"fi",
"fr",
"gl",
"ha",
"he",
"hi",
"hr",
"hu",
"hy",
"id",
"is",
"it",
"ja",
"ka",
"kk",
"km",
"ko",
"ku",
"ky",
"lt",
"lv",
"mk",
"ml",
"mn",
"ms",
"nb",
"nl",
"nn",
"no",
"pl",
"ps",
"pt",
"ro",
"ru",
"sd",
"sk",
"sl",
"so",
"sq",
"sr",
"sv",
"ta",
"tg",
"th",
"tk",
"tr",
"tt",
"uk",
"ug",
"ur",
"uz",
"vi",
"zh",
].contains(locale.languageCode);
final codeWithCountry = '${locale.languageCode}' +
(locale.countryCode == null ? '' : '-${locale.countryCode}');
return supportedLocales.contains(codeWithCountry) ||
supportedLocales.contains(locale.languageCode);
}

String resolveLocale(Locale locale) {
final codeWithCountry = '${locale.languageCode}' +
(locale.countryCode == null ? '' : '-${locale.countryCode}');
if (supportedLocales.contains(codeWithCountry)) {
return codeWithCountry;
}
if (supportedLocales.contains(locale.languageCode)) {
return locale.languageCode;
}
return "en";
}

@override
Expand All @@ -126,4 +71,79 @@ class _CountryLocalizationsDelegate

@override
bool shouldReload(_CountryLocalizationsDelegate old) => false;

static const supportedLocales = [
"af",
"am",
"ar",
"az",
"be",
"bg",
"bn",
"bs",
"ca",
"cs",
"da",
"de",
"el",
"en",
"es",
"et",
"fa",
"fi",
"fr",
"gl",
"ha",
"he",
"hi",
"hr",
"hu",
"hy",
"id",
"is",
"it",
"ja",
"ka",
"kk",
"km",
"ko",
"ku",
"ky",
"lt",
"lv",
"mk",
"ml",
"mn",
"ms",
"nb",
"nl",
"nn",
"no",
"pl",
"ps",
"pt",
"ro",
"ru",
"sd",
"sk",
"sl",
"so",
"sq",
"sr",
"sv",
"ta",
"tg",
"th",
"tk",
"tr",
"tt",
"uk",
"ug",
"ur",
"uz",
"vi",
"zh",
"zh-CN",
"zh-TW",
];
}
Loading