Merge branch 'main' into main
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'package:collection/collection.dart' show IterableExtension;
|
||||
import 'package:diacritic/diacritic.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart' show kDebugMode;
|
||||
|
||||
import 'country_codes.dart';
|
||||
import 'country_localizations.dart';
|
||||
@@ -39,6 +41,15 @@ class CountryCode {
|
||||
return CountryCode.fromJson(jsonCode!);
|
||||
}
|
||||
|
||||
static CountryCode? tryFromCountryCode(String countryCode) {
|
||||
try {
|
||||
return CountryCode.fromCountryCode(countryCode);
|
||||
} catch (e) {
|
||||
if (kDebugMode) print('Failed to recognize country from countryCode: $countryCode');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
factory CountryCode.fromDialCode(String dialCode) {
|
||||
final Map<String, String>? jsonCode = codes.firstWhereOrNull(
|
||||
(code) => code['dial_code'] == dialCode,
|
||||
@@ -46,14 +57,24 @@ class CountryCode {
|
||||
return CountryCode.fromJson(jsonCode!);
|
||||
}
|
||||
|
||||
static CountryCode? tryFromDialCode(String dialCode) {
|
||||
try {
|
||||
return CountryCode.fromDialCode(dialCode);
|
||||
} catch (e) {
|
||||
if (kDebugMode) print('Failed to recognize country from dialCode: $dialCode');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
CountryCode localize(BuildContext context) {
|
||||
final nam = CountryLocalizations.of(context)?.translate(code) ?? name;
|
||||
return this
|
||||
..name = CountryLocalizations.of(context)?.translate(code) ?? name;
|
||||
..name = nam == null? name : removeDiacritics(nam);
|
||||
}
|
||||
|
||||
factory CountryCode.fromJson(Map<String, dynamic> json) {
|
||||
return CountryCode(
|
||||
name: json['name'],
|
||||
name: removeDiacritics(json['name']),
|
||||
code: json['code'],
|
||||
dialCode: json['dial_code'],
|
||||
flagUri: 'flags/${json['code'].toLowerCase()}.png',
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -119,7 +119,7 @@ class _CountryLocalizationsDelegate
|
||||
|
||||
@override
|
||||
Future<CountryLocalizations> load(Locale locale) async {
|
||||
CountryLocalizations localizations = CountryLocalizations(locale);
|
||||
CountryLocalizations localizations = CountryLocalizations(const Locale('en'));//locale);
|
||||
await localizations.load();
|
||||
return localizations;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ class SelectionDialog extends StatefulWidget {
|
||||
final InputDecoration searchDecoration;
|
||||
final TextStyle? searchStyle;
|
||||
final TextStyle? textStyle;
|
||||
final TextStyle headerTextStyle;
|
||||
final BoxDecoration? boxDecoration;
|
||||
final WidgetBuilder? emptySearchBuilder;
|
||||
final bool? showFlag;
|
||||
@@ -17,7 +18,12 @@ class SelectionDialog extends StatefulWidget {
|
||||
final Decoration? flagDecoration;
|
||||
final Size? size;
|
||||
final bool hideSearch;
|
||||
final bool hideCloseIcon;
|
||||
final Icon? closeIcon;
|
||||
final bool hideHeaderText;
|
||||
final String? headerText;
|
||||
final EdgeInsets topBarPadding;
|
||||
final MainAxisAlignment headerAlignment;
|
||||
|
||||
/// Background color of SelectionDialog
|
||||
final Color? backgroundColor;
|
||||
@@ -28,15 +34,24 @@ class SelectionDialog extends StatefulWidget {
|
||||
/// elements passed as favorite
|
||||
final List<CountryCode> favoriteElements;
|
||||
|
||||
final EdgeInsetsGeometry dialogItemPadding;
|
||||
|
||||
final EdgeInsetsGeometry searchPadding;
|
||||
|
||||
SelectionDialog(
|
||||
this.elements,
|
||||
this.favoriteElements, {
|
||||
Key? key,
|
||||
this.showCountryOnly,
|
||||
required this.hideHeaderText,
|
||||
this.emptySearchBuilder,
|
||||
required this.headerAlignment,
|
||||
required this.headerTextStyle,
|
||||
InputDecoration searchDecoration = const InputDecoration(),
|
||||
this.searchStyle,
|
||||
this.textStyle,
|
||||
required this.topBarPadding,
|
||||
this.headerText,
|
||||
this.boxDecoration,
|
||||
this.showFlag,
|
||||
this.flagDecoration,
|
||||
@@ -45,10 +60,11 @@ class SelectionDialog extends StatefulWidget {
|
||||
this.backgroundColor,
|
||||
this.barrierColor,
|
||||
this.hideSearch = false,
|
||||
this.hideCloseIcon = false,
|
||||
this.closeIcon,
|
||||
}) : searchDecoration = searchDecoration.prefixIcon == null
|
||||
? searchDecoration.copyWith(prefixIcon: const Icon(Icons.search))
|
||||
: searchDecoration,
|
||||
this.dialogItemPadding = const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
|
||||
this.searchPadding = const EdgeInsets.symmetric(horizontal: 24),
|
||||
}) : searchDecoration = searchDecoration.prefixIcon == null ? searchDecoration.copyWith(prefixIcon: const Icon(Icons.search)) : searchDecoration,
|
||||
super(key: key);
|
||||
|
||||
@override
|
||||
@@ -65,8 +81,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
child: Container(
|
||||
clipBehavior: Clip.hardEdge,
|
||||
width: widget.size?.width ?? MediaQuery.of(context).size.width,
|
||||
height:
|
||||
widget.size?.height ?? MediaQuery.of(context).size.height * 0.85,
|
||||
height: widget.size?.height ?? MediaQuery.of(context).size.height * 0.85,
|
||||
decoration: widget.boxDecoration ??
|
||||
BoxDecoration(
|
||||
color: widget.backgroundColor ?? Colors.white,
|
||||
@@ -84,15 +99,31 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
IconButton(
|
||||
padding: const EdgeInsets.all(0),
|
||||
iconSize: 20,
|
||||
icon: widget.closeIcon!,
|
||||
onPressed: () => Navigator.pop(context),
|
||||
Padding(
|
||||
padding:!widget.hideHeaderText? widget.topBarPadding: EdgeInsets.zero,
|
||||
child: Row(
|
||||
mainAxisAlignment: widget.headerAlignment,
|
||||
children: [
|
||||
!widget.hideHeaderText && widget.headerText != null
|
||||
? Text(
|
||||
widget.headerText!,
|
||||
overflow: TextOverflow.fade,
|
||||
style: widget.headerTextStyle,
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
if (!widget.hideCloseIcon)
|
||||
IconButton(
|
||||
padding: const EdgeInsets.all(0),
|
||||
iconSize: 20,
|
||||
icon: widget.closeIcon!,
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (!widget.hideSearch)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
padding: widget.searchPadding,
|
||||
child: TextField(
|
||||
style: widget.searchStyle,
|
||||
decoration: widget.searchDecoration,
|
||||
@@ -107,28 +138,28 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
...widget.favoriteElements.map(
|
||||
(f) => SimpleDialogOption(
|
||||
child: _buildOption(f),
|
||||
onPressed: () {
|
||||
...widget.favoriteElements.map((f) => InkWell(
|
||||
onTap: () {
|
||||
_selectItem(f);
|
||||
},
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: widget.dialogItemPadding,
|
||||
child: _buildOption(f),
|
||||
))),
|
||||
const Divider(),
|
||||
],
|
||||
),
|
||||
if (filteredElements.isEmpty)
|
||||
_buildEmptySearchWidget(context)
|
||||
else
|
||||
...filteredElements.map(
|
||||
(e) => SimpleDialogOption(
|
||||
child: _buildOption(e),
|
||||
onPressed: () {
|
||||
...filteredElements.map((e) => InkWell(
|
||||
onTap: () {
|
||||
_selectItem(e);
|
||||
},
|
||||
),
|
||||
),
|
||||
child: Padding(
|
||||
padding: widget.dialogItemPadding,
|
||||
child: _buildOption(e),
|
||||
))),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -146,10 +177,11 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
if (widget.showFlag!)
|
||||
Flexible(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(right: 16.0),
|
||||
margin: Directionality.of(context) == TextDirection.ltr // Here Adding padding depending on the locale language direction
|
||||
? const EdgeInsets.only(right: 16.0)
|
||||
: const EdgeInsets.only(left: 16.0),
|
||||
decoration: widget.flagDecoration,
|
||||
clipBehavior:
|
||||
widget.flagDecoration == null ? Clip.none : Clip.hardEdge,
|
||||
clipBehavior: widget.flagDecoration == null ? Clip.none : Clip.hardEdge,
|
||||
child: Image.asset(
|
||||
e.flagUri!,
|
||||
package: 'country_code_picker',
|
||||
@@ -160,9 +192,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
Expanded(
|
||||
flex: 4,
|
||||
child: Text(
|
||||
widget.showCountryOnly!
|
||||
? e.toCountryStringOnly()
|
||||
: e.toLongString(),
|
||||
widget.showCountryOnly! ? e.toCountryStringOnly() : e.toLongString(),
|
||||
overflow: TextOverflow.fade,
|
||||
style: widget.textStyle,
|
||||
),
|
||||
@@ -178,8 +208,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
}
|
||||
|
||||
return Center(
|
||||
child: Text(CountryLocalizations.of(context)?.translate('no_country') ??
|
||||
'No country found'),
|
||||
child: Text(CountryLocalizations.of(context)?.translate('no_country') ?? 'No country found'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -192,12 +221,7 @@ class _SelectionDialogState extends State<SelectionDialog> {
|
||||
void _filterElements(String s) {
|
||||
s = s.toUpperCase();
|
||||
setState(() {
|
||||
filteredElements = widget.elements
|
||||
.where((e) =>
|
||||
e.code!.contains(s) ||
|
||||
e.dialCode!.contains(s) ||
|
||||
e.name!.toUpperCase().contains(s))
|
||||
.toList();
|
||||
filteredElements = widget.elements.where((e) => e.code!.contains(s) || e.dialCode!.contains(s) || e.name!.toUpperCase().contains(s)).toList();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user