Upgrade follow latest
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
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';
|
||||
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';
|
||||
|
||||
export 'country_code.dart';
|
||||
import 'src/country_code.dart';
|
||||
import 'src/country_codes.dart';
|
||||
import 'src/selection_dialog.dart';
|
||||
|
||||
export 'src/country_code.dart';
|
||||
export 'src/country_codes.dart';
|
||||
export 'src/country_localizations.dart';
|
||||
export 'src/selection_dialog.dart';
|
||||
|
||||
class CountryCodePicker extends StatefulWidget {
|
||||
final ValueChanged<CountryCode>? onChanged;
|
||||
@@ -84,7 +85,7 @@ class CountryCodePicker extends StatefulWidget {
|
||||
/// with customized codes.
|
||||
final List<Map<String, String>> countryList;
|
||||
|
||||
CountryCodePicker({
|
||||
const CountryCodePicker({
|
||||
this.onChanged,
|
||||
this.onInit,
|
||||
this.initialSelection,
|
||||
@@ -122,6 +123,7 @@ class CountryCodePicker extends StatefulWidget {
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
// ignore: no_logic_in_create_state
|
||||
State<StatefulWidget> createState() {
|
||||
List<Map<String, String>> jsonList = countryList;
|
||||
|
||||
@@ -134,12 +136,12 @@ class CountryCodePicker extends StatefulWidget {
|
||||
|
||||
if (countryFilter != null && countryFilter!.isNotEmpty) {
|
||||
final uppercaseCustomList =
|
||||
countryFilter!.map((c) => c.toUpperCase()).toList();
|
||||
countryFilter!.map((criteria) => criteria.toUpperCase()).toList();
|
||||
elements = elements
|
||||
.where((c) =>
|
||||
uppercaseCustomList.contains(c.code) ||
|
||||
uppercaseCustomList.contains(c.name) ||
|
||||
uppercaseCustomList.contains(c.dialCode))
|
||||
.where((criteria) =>
|
||||
uppercaseCustomList.contains(criteria.code) ||
|
||||
uppercaseCustomList.contains(criteria.name) ||
|
||||
uppercaseCustomList.contains(criteria.dialCode))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -156,14 +158,14 @@ class CountryCodePickerState extends State<CountryCodePicker> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget _widget;
|
||||
if (widget.builder != null)
|
||||
_widget = InkWell(
|
||||
Widget internalWidget;
|
||||
if (widget.builder != null) {
|
||||
internalWidget = InkWell(
|
||||
onTap: showCountryCodePickerDialog,
|
||||
child: widget.builder!(selectedItem),
|
||||
);
|
||||
else {
|
||||
_widget = TextButton(
|
||||
} else {
|
||||
internalWidget = TextButton(
|
||||
onPressed: widget.enabled ? showCountryCodePickerDialog : null,
|
||||
child: Padding(
|
||||
padding: widget.padding,
|
||||
@@ -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.labelLarge,
|
||||
overflow: widget.textOverflow,
|
||||
),
|
||||
),
|
||||
@@ -223,14 +225,14 @@ class CountryCodePickerState extends State<CountryCodePicker> {
|
||||
),
|
||||
);
|
||||
}
|
||||
return _widget;
|
||||
return internalWidget;
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
|
||||
this.elements = elements.map((e) => e.localize(context)).toList();
|
||||
elements = elements.map((element) => element.localize(context)).toList();
|
||||
_onInit(selectedItem);
|
||||
}
|
||||
|
||||
@@ -241,11 +243,11 @@ class CountryCodePickerState extends State<CountryCodePicker> {
|
||||
if (oldWidget.initialSelection != widget.initialSelection) {
|
||||
if (widget.initialSelection != null) {
|
||||
selectedItem = elements.firstWhere(
|
||||
(e) =>
|
||||
(e.code!.toUpperCase() ==
|
||||
(criteria) =>
|
||||
(criteria.code!.toUpperCase() ==
|
||||
widget.initialSelection!.toUpperCase()) ||
|
||||
(e.dialCode == widget.initialSelection) ||
|
||||
(e.name!.toUpperCase() ==
|
||||
(criteria.dialCode == widget.initialSelection) ||
|
||||
(criteria.name!.toUpperCase() ==
|
||||
widget.initialSelection!.toUpperCase()),
|
||||
orElse: () => elements[0]);
|
||||
} else {
|
||||
@@ -261,74 +263,33 @@ class CountryCodePickerState extends State<CountryCodePicker> {
|
||||
|
||||
if (widget.initialSelection != null) {
|
||||
selectedItem = elements.firstWhere(
|
||||
(e) =>
|
||||
(e.code!.toUpperCase() ==
|
||||
(item) =>
|
||||
(item.code!.toUpperCase() ==
|
||||
widget.initialSelection!.toUpperCase()) ||
|
||||
(e.dialCode == widget.initialSelection) ||
|
||||
(e.name!.toUpperCase() == widget.initialSelection!.toUpperCase()),
|
||||
(item.dialCode == widget.initialSelection) ||
|
||||
(item.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()) !=
|
||||
.where((item) =>
|
||||
widget.favorite.firstWhereOrNull((criteria) =>
|
||||
item.code!.toUpperCase() == criteria.toUpperCase() ||
|
||||
item.dialCode == criteria ||
|
||||
item.name!.toUpperCase() == criteria.toUpperCase()) !=
|
||||
null)
|
||||
.toList();
|
||||
}
|
||||
|
||||
void showCountryCodePickerDialog() {
|
||||
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(
|
||||
constraints: BoxConstraints(maxHeight: 500, maxWidth: 400),
|
||||
child: Dialog(
|
||||
child: SelectionDialog(
|
||||
elements,
|
||||
favoriteElements,
|
||||
showCountryOnly: widget.showCountryOnly,
|
||||
emptySearchBuilder: widget.emptySearchBuilder,
|
||||
searchDecoration: widget.searchDecoration,
|
||||
searchStyle: widget.searchStyle,
|
||||
textStyle: widget.dialogTextStyle,
|
||||
boxDecoration: widget.boxDecoration,
|
||||
showFlag: widget.showFlagDialog != null
|
||||
? widget.showFlagDialog
|
||||
: widget.showFlag,
|
||||
flagWidth: widget.flagWidth,
|
||||
size: widget.dialogSize,
|
||||
backgroundColor: widget.dialogBackgroundColor,
|
||||
barrierColor: widget.barrierColor,
|
||||
hideSearch: widget.hideSearch,
|
||||
closeIcon: widget.closeIcon,
|
||||
flagDecoration: widget.flagDecoration,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
).then((e) {
|
||||
if (e != null) {
|
||||
setState(() {
|
||||
selectedItem = e;
|
||||
});
|
||||
|
||||
_publishSelection(e);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
showMaterialModalBottomSheet(
|
||||
barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5),
|
||||
backgroundColor: widget.backgroundColor ?? Colors.transparent,
|
||||
context: context,
|
||||
builder: (context) => Center(
|
||||
void showCountryCodePickerDialog() async {
|
||||
final item = await showDialog(
|
||||
barrierColor: widget.barrierColor ?? Colors.grey.withOpacity(0.5),
|
||||
context: context,
|
||||
builder: (context) => Center(
|
||||
child: Dialog(
|
||||
child: SelectionDialog(
|
||||
elements,
|
||||
favoriteElements,
|
||||
@@ -338,39 +299,37 @@ class CountryCodePickerState extends State<CountryCodePicker> {
|
||||
searchStyle: widget.searchStyle,
|
||||
textStyle: widget.dialogTextStyle,
|
||||
boxDecoration: widget.boxDecoration,
|
||||
showFlag: widget.showFlagDialog != null
|
||||
? widget.showFlagDialog
|
||||
: widget.showFlag,
|
||||
showFlag: widget.showFlagDialog ?? widget.showFlag,
|
||||
flagWidth: widget.flagWidth,
|
||||
flagDecoration: widget.flagDecoration,
|
||||
size: widget.dialogSize,
|
||||
backgroundColor: widget.dialogBackgroundColor,
|
||||
barrierColor: widget.barrierColor,
|
||||
hideSearch: widget.hideSearch,
|
||||
closeIcon: widget.closeIcon,
|
||||
flagDecoration: widget.flagDecoration,
|
||||
),
|
||||
),
|
||||
).then((e) {
|
||||
if (e != null) {
|
||||
setState(() {
|
||||
selectedItem = e;
|
||||
});
|
||||
),
|
||||
);
|
||||
|
||||
_publishSelection(e);
|
||||
}
|
||||
if (item != null) {
|
||||
setState(() {
|
||||
selectedItem = item;
|
||||
});
|
||||
|
||||
_publishSelection(item);
|
||||
}
|
||||
}
|
||||
|
||||
void _publishSelection(CountryCode e) {
|
||||
void _publishSelection(CountryCode countryCode) {
|
||||
if (widget.onChanged != null) {
|
||||
widget.onChanged!(e);
|
||||
widget.onChanged!(countryCode);
|
||||
}
|
||||
}
|
||||
|
||||
void _onInit(CountryCode? e) {
|
||||
void _onInit(CountryCode? countryCode) {
|
||||
if (widget.onInit != null) {
|
||||
widget.onInit!(e);
|
||||
widget.onInit!(countryCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
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';
|
||||
|
||||
mixin ToAlias {}
|
||||
import 'country_codes.dart';
|
||||
import 'country_localizations.dart';
|
||||
|
||||
@deprecated
|
||||
class CElement = CountryCode with ToAlias;
|
||||
mixin ToAlias {}
|
||||
|
||||
/// Country element. This is the element that contains all the information
|
||||
class CountryCode {
|
||||
@@ -50,8 +48,7 @@ class CountryCode {
|
||||
|
||||
CountryCode localize(BuildContext context) {
|
||||
return this
|
||||
..name =
|
||||
CountryLocalizations.of(context)?.translate(this.code) ?? this.name;
|
||||
..name = CountryLocalizations.of(context)?.translate(code) ?? name;
|
||||
}
|
||||
|
||||
factory CountryCode.fromJson(Map<String, dynamic> json) {
|
||||
@@ -22,7 +22,7 @@ class CountryLocalizations {
|
||||
|
||||
Future<bool> load() async {
|
||||
String jsonString = await rootBundle.loadString(
|
||||
'packages/country_code_picker/i18n/${locale.languageCode}.json');
|
||||
'packages/country_code_picker/src/i18n/${locale.languageCode}.json');
|
||||
Map<String, dynamic> jsonMap = json.decode(jsonString);
|
||||
|
||||
_localizedStrings = jsonMap.map((key, value) {
|
||||
@@ -119,7 +119,7 @@ class _CountryLocalizationsDelegate
|
||||
|
||||
@override
|
||||
Future<CountryLocalizations> load(Locale locale) async {
|
||||
CountryLocalizations localizations = new CountryLocalizations(locale);
|
||||
CountryLocalizations localizations = CountryLocalizations(locale);
|
||||
await localizations.load();
|
||||
return localizations;
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import 'package:country_code_picker/country_code.dart';
|
||||
import 'package:country_code_picker/country_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'country_code.dart';
|
||||
import 'country_localizations.dart';
|
||||
|
||||
/// selection dialog used for selection of the country code
|
||||
class SelectionDialog extends StatefulWidget {
|
||||
final List<CountryCode> elements;
|
||||
@@ -45,8 +46,8 @@ class SelectionDialog extends StatefulWidget {
|
||||
this.barrierColor,
|
||||
this.hideSearch = false,
|
||||
this.closeIcon,
|
||||
}) : this.searchDecoration = searchDecoration.prefixIcon == null
|
||||
? searchDecoration.copyWith(prefixIcon: Icon(Icons.search))
|
||||
}) : searchDecoration = searchDecoration.prefixIcon == null
|
||||
? searchDecoration.copyWith(prefixIcon: const Icon(Icons.search))
|
||||
: searchDecoration,
|
||||
super(key: key);
|
||||
|
||||
@@ -69,13 +70,13 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
decoration: widget.boxDecoration ??
|
||||
BoxDecoration(
|
||||
color: widget.backgroundColor ?? Colors.white,
|
||||
borderRadius: BorderRadius.all(Radius.circular(8.0)),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(8.0)),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: widget.barrierColor ?? Colors.grey.withOpacity(1),
|
||||
spreadRadius: 5,
|
||||
blurRadius: 7,
|
||||
offset: Offset(0, 3), // changes position of shadow
|
||||
offset: const Offset(0, 3), // changes position of shadow
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -137,7 +138,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
);
|
||||
|
||||
Widget _buildOption(CountryCode e) {
|
||||
return Container(
|
||||
return SizedBox(
|
||||
width: 400,
|
||||
child: Flex(
|
||||
direction: Axis.horizontal,
|
||||
Reference in New Issue
Block a user