[go: nahoru, domu]

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

Commit

Permalink
nullalbe with support 70 languages
Browse files Browse the repository at this point in the history
  • Loading branch information
kechankrisna committed Mar 8, 2021
1 parent 0fe203e commit 057bb77
Show file tree
Hide file tree
Showing 77 changed files with 17,717 additions and 2,115 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.0

- support 70 languages
- use modal_bottom_sheet 2.0.0
- nullable integration

## 1.7.0

- Update modal_bottom_sheet to 1.0.0+1
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 70 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,76 @@ class _MyAppState extends State<MyApp> {
Widget build(BuildContext context) {
return new MaterialApp(
supportedLocales: [
Locale('en'),
Locale('it'),
Locale('fr'),
Locale('es'),
Locale('de'),
Locale('pt'),
Locale('ko'),
Locale('zh'),
Locale("af"),
Locale("am"),
Locale("ar"),
Locale("az"),
Locale("be"),
Locale("bg"),
Locale("bn"),
Locale("bs"),
Locale("ca"),
Locale("cs"),
Locale("da"),
Locale("de"),
Locale("el"),
Locale("en"),
Locale("es"),
Locale("et"),
Locale("fa"),
Locale("fi"),
Locale("fr"),
Locale("gl"),
Locale("ha"),
Locale("he"),
Locale("hi"),
Locale("hr"),
Locale("hu"),
Locale("hy"),
Locale("id"),
Locale("is"),
Locale("it"),
Locale("ja"),
Locale("ka"),
Locale("kk"),
Locale("km"),
Locale("ko"),
Locale("ku"),
Locale("ky"),
Locale("lt"),
Locale("lv"),
Locale("mk"),
Locale("ml"),
Locale("mn"),
Locale("ms"),
Locale("nb"),
Locale("nl"),
Locale("nn"),
Locale("no"),
Locale("pl"),
Locale("ps"),
Locale("pt"),
Locale("ro"),
Locale("ru"),
Locale("sd"),
Locale("sk"),
Locale("sl"),
Locale("so"),
Locale("sq"),
Locale("sr"),
Locale("sv"),
Locale("ta"),
Locale("tg"),
Locale("th"),
Locale("tk"),
Locale("tr"),
Locale("tt"),
Locale("uk"),
Locale("ug"),
Locale("ur"),
Locale("uz"),
Locale("vi"),
Locale("zh")
],
localizationsDelegates: [
CountryLocalizations.delegate,
Expand Down
19 changes: 7 additions & 12 deletions lib/country_code.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart' show IterableExtension;
import 'package:country_code_picker/country_codes.dart';
import 'package:country_code_picker/country_localizations.dart';
import 'package:flutter/cupertino.dart';
Expand All @@ -10,16 +11,16 @@ class CElement = CountryCode with ToAlias;
/// Country element. This is the element that contains all the information
class CountryCode {
/// the name of the country
String name;
String? name;

/// the flag of the country
final String flagUri;
final String? flagUri;

/// the country code (IT,AF..)
final String code;
final String? code;

/// the dial code (+39,+93..)
final String dialCode;
final String? dialCode;

CountryCode({
this.name,
Expand All @@ -29,16 +30,10 @@ class CountryCode {
});

factory CountryCode.fromCode(String isoCode) {
final Map<String, String> jsonCode = codes.firstWhere(
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
(code) => code['code'] == isoCode,
orElse: () => null,
);

if (jsonCode == null) {
return null;
}

return CountryCode.fromJson(jsonCode);
return CountryCode.fromJson(jsonCode!);
}

CountryCode localize(BuildContext context) {
Expand Down
76 changes: 38 additions & 38 deletions lib/country_code_picker.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library country_code_picker;

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';
Expand All @@ -9,39 +10,39 @@ import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
export 'country_code.dart';

class CountryCodePicker extends StatefulWidget {
final ValueChanged<CountryCode> onChanged;
final ValueChanged<CountryCode> onInit;
final String initialSelection;
final ValueChanged<CountryCode>? onChanged;
final ValueChanged<CountryCode?>? onInit;
final String? initialSelection;
final List<String> favorite;
final TextStyle textStyle;
final TextStyle? textStyle;
final EdgeInsetsGeometry padding;
final bool showCountryOnly;
final InputDecoration searchDecoration;
final TextStyle searchStyle;
final TextStyle dialogTextStyle;
final WidgetBuilder emptySearchBuilder;
final Function(CountryCode) builder;
final TextStyle? searchStyle;
final TextStyle? dialogTextStyle;
final WidgetBuilder? emptySearchBuilder;
final Function(CountryCode?)? builder;
final bool enabled;
final TextOverflow textOverflow;
final Icon closeIcon;

/// Barrier color of ModalBottomSheet
final Color barrierColor;
final Color? barrierColor;

/// Background color of ModalBottomSheet
final Color backgroundColor;
final Color? backgroundColor;

/// BoxDecoration for dialog
final BoxDecoration boxDecoration;
final BoxDecoration? boxDecoration;

/// the size of the selection dialog
final Size dialogSize;
final Size? dialogSize;

/// Background color of selection dialog
final Color dialogBackgroundColor;
final Color? dialogBackgroundColor;

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

/// shows the name of the country instead of the dialcode
final bool showOnlyCountryWhenClosed;
Expand All @@ -58,15 +59,15 @@ class CountryCodePicker extends StatefulWidget {

final bool hideMainText;

final bool showFlagMain;
final bool? showFlagMain;

final bool showFlagDialog;
final bool? showFlagDialog;

/// Width of the flag images
final double flagWidth;

/// Use this property to change the order of the options
final Comparator<CountryCode> comparator;
final Comparator<CountryCode>? comparator;

/// Set to true if you want to hide the search part
final bool hideSearch;
Expand Down Expand Up @@ -106,23 +107,23 @@ class CountryCodePicker extends StatefulWidget {
this.dialogSize,
this.dialogBackgroundColor,
this.closeIcon = const Icon(Icons.close),
Key key,
Key? key,
}) : super(key: key);

@override
State<StatefulWidget> createState() {
List<Map> jsonList = codes;

List<CountryCode> elements =
jsonList.map((json) => CountryCode.fromJson(json)).toList();
jsonList.map((json) => CountryCode.fromJson(json as Map<String, dynamic>)).toList();

if (comparator != null) {
elements.sort(comparator);
}

if (countryFilter != null && countryFilter.isNotEmpty) {
if (countryFilter != null && countryFilter!.isNotEmpty) {
final uppercaseCustomList =
countryFilter.map((c) => c.toUpperCase()).toList();
countryFilter!.map((c) => c.toUpperCase()).toList();
elements = elements
.where((c) =>
uppercaseCustomList.contains(c.code) ||
Expand All @@ -136,7 +137,7 @@ class CountryCodePicker extends StatefulWidget {
}

class CountryCodePickerState extends State<CountryCodePicker> {
CountryCode selectedItem;
CountryCode? selectedItem;
List<CountryCode> elements = [];
List<CountryCode> favoriteElements = [];

Expand All @@ -148,7 +149,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (widget.builder != null)
_widget = InkWell(
onTap: showCountryCodePickerDialog,
child: widget.builder(selectedItem),
child: widget.builder!(selectedItem),
);
else {
_widget = FlatButton(
Expand All @@ -159,7 +160,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
mainAxisSize: MainAxisSize.min,
children: <Widget>[
if (widget.showFlagMain != null
? widget.showFlagMain
? widget.showFlagMain!
: widget.showFlag)
Flexible(
flex: widget.alignLeft ? 0 : 1,
Expand All @@ -169,7 +170,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
? const EdgeInsets.only(right: 16.0, left: 8.0)
: const EdgeInsets.only(right: 16.0),
child: Image.asset(
selectedItem.flagUri,
selectedItem!.flagUri!,
package: 'country_code_picker',
width: widget.flagWidth,
),
Expand All @@ -180,7 +181,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
fit: widget.alignLeft ? FlexFit.tight : FlexFit.loose,
child: Text(
widget.showOnlyCountryWhenClosed
? selectedItem.toCountryStringOnly()
? selectedItem!.toCountryStringOnly()
: selectedItem.toString(),
style: widget.textStyle ?? Theme.of(context).textTheme.button,
overflow: widget.textOverflow,
Expand Down Expand Up @@ -223,10 +224,10 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (widget.initialSelection != null) {
selectedItem = elements.firstWhere(
(e) =>
(e.code.toUpperCase() ==
widget.initialSelection.toUpperCase()) ||
(e.code!.toUpperCase() ==
widget.initialSelection!.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name.toUpperCase() == widget.initialSelection.toUpperCase()),
(e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()),
orElse: () => elements[0]);
} else {
selectedItem = elements[0];
Expand All @@ -242,22 +243,21 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (widget.initialSelection != null) {
selectedItem = elements.firstWhere(
(e) =>
(e.code.toUpperCase() == widget.initialSelection.toUpperCase()) ||
(e.code!.toUpperCase() == widget.initialSelection!.toUpperCase()) ||
(e.dialCode == widget.initialSelection) ||
(e.name.toUpperCase() == widget.initialSelection.toUpperCase()),
(e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()),
orElse: () => elements[0]);
} else {
selectedItem = elements[0];
}

favoriteElements = elements
.where((e) =>
widget.favorite.firstWhere(
widget.favorite.firstWhereOrNull(
(f) =>
e.code.toUpperCase() == f.toUpperCase() ||
e.code!.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name.toUpperCase() == f.toUpperCase(),
orElse: () => null) !=
e.name!.toUpperCase() == f.toUpperCase()) !=
null)
.toList();
}
Expand Down Expand Up @@ -301,13 +301,13 @@ class CountryCodePickerState extends State<CountryCodePicker> {

void _publishSelection(CountryCode e) {
if (widget.onChanged != null) {
widget.onChanged(e);
widget.onChanged!(e);
}
}

void _onInit(CountryCode e) {
void _onInit(CountryCode? e) {
if (widget.onInit != null) {
widget.onInit(e);
widget.onInit!(e);
}
}
}
Loading

0 comments on commit 057bb77

Please sign in to comment.