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

1.7.5 #156

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

1.7.5 #156

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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
- switch to dialog on desktop app support
- added flagDecoration


## 1.7.5

- support version<=2.0
- include latest 2.0.2 modify content

## 1.7.0

- Update modal_bottom_sheet to 1.0.0+1
Expand Down
29 changes: 19 additions & 10 deletions lib/country_code.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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 @@ -11,16 +10,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 @@ -35,17 +34,27 @@ class CountryCode {
}

factory CountryCode.fromCountryCode(String countryCode) {
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
final Map<String, String> jsonCode = codes.firstWhere(
(code) => code['code'] == countryCode,
orElse: () => null,
);
return CountryCode.fromJson(jsonCode!);

if (jsonCode == null) {
return null;
}
return CountryCode.fromJson(jsonCode);
}

factory CountryCode.fromDialCode(String dialCode) {
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
final Map<String, String> jsonCode = codes.firstWhere(
(code) => code['dial_code'] == dialCode,
orElse: () => null,
);
return CountryCode.fromJson(jsonCode!);

if (jsonCode == null) {
return null;
}
return CountryCode.fromJson(jsonCode);
}

CountryCode localize(BuildContext context) {
Expand All @@ -72,7 +81,7 @@ class CountryCode {
return '$_cleanName';
}

String? get _cleanName {
String get _cleanName {
return name?.replaceAll(RegExp(r'[[\]]'), '').split(',').first;
}
}
81 changes: 40 additions & 41 deletions lib/country_code_picker.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -12,39 +11,39 @@ import 'package:universal_platform/universal_platform.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 @@ -61,15 +60,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 All @@ -78,7 +77,7 @@ class CountryCodePicker extends StatefulWidget {
final bool showDropDownButton;

/// [BoxDecoration] for the flag image
final Decoration? flagDecoration;
final Decoration flagDecoration;

/// An optional argument for injecting a list of countries
/// with customized codes.
Expand Down Expand Up @@ -118,7 +117,7 @@ class CountryCodePicker extends StatefulWidget {
this.dialogBackgroundColor,
this.closeIcon = const Icon(Icons.close),
this.countryList = codes,
Key? key,
Key key,
}) : super(key: key);

@override
Expand All @@ -132,9 +131,9 @@ class CountryCodePicker extends StatefulWidget {
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 @@ -148,7 +147,7 @@ class CountryCodePicker extends StatefulWidget {
}

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

Expand All @@ -160,7 +159,7 @@ class CountryCodePickerState extends State<CountryCodePicker> {
if (widget.builder != null)
_widget = InkWell(
onTap: showCountryCodePickerDialog,
child: widget.builder!(selectedItem),
child: widget.builder(selectedItem),
);
else {
_widget = TextButton(
Expand All @@ -172,7 +171,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 @@ -186,7 +185,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 @@ -197,7 +196,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,
Expand Down Expand Up @@ -242,11 +241,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 @@ -262,21 +260,22 @@ 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.firstWhereOrNull((f) =>
e.code!.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name!.toUpperCase() == f.toUpperCase()) !=
widget.favorite.firstWhere(
(f) =>
e.code.toUpperCase() == f.toUpperCase() ||
e.dialCode == f ||
e.name.toUpperCase() == f.toUpperCase(),
orElse: () => null) !=
null)
.toList();
}
Expand Down Expand Up @@ -364,13 +363,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);
}
}
}
8 changes: 4 additions & 4 deletions lib/country_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CountryLocalizations {

CountryLocalizations(this.locale);

static CountryLocalizations? of(BuildContext context) {
static CountryLocalizations of(BuildContext context) {
return Localizations.of<CountryLocalizations>(
context,
CountryLocalizations,
Expand All @@ -18,7 +18,7 @@ class CountryLocalizations {
static const LocalizationsDelegate<CountryLocalizations> delegate =
_CountryLocalizationsDelegate();

late Map<String, String> _localizedStrings;
Map<String, String> _localizedStrings;

Future<bool> load() async {
String jsonString = await rootBundle.loadString(
Expand All @@ -32,8 +32,8 @@ class CountryLocalizations {
return true;
}

String? translate(String? key) {
return _localizedStrings[key!];
String translate(String key) {
return _localizedStrings[key];
}
}

Expand Down
Loading