Skip to content
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
Binary file modified assets/flags/sy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 14 additions & 22 deletions lib/country_picker_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
void initState() {
_selectedCountry = widget.selectedCountry;
_filteredCountries = widget.filteredCountries.toList()
..sort(
(a, b) => a.localizedName(widget.languageCode).compareTo(b.localizedName(widget.languageCode)),
);
..sort((a, b) => a.localizedName(widget.languageCode).compareTo(b.localizedName(widget.languageCode)));

super.initState();
}
Expand All @@ -85,10 +83,11 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
const defaultVerticalPadding = 24.0;
return Dialog(
insetPadding: EdgeInsets.symmetric(
vertical: defaultVerticalPadding,
horizontal: mediaWidth > (width + defaultHorizontalPadding * 2)
? (mediaWidth - width) / 2
: defaultHorizontalPadding),
vertical: defaultVerticalPadding,
horizontal: mediaWidth > (width + defaultHorizontalPadding * 2)
? (mediaWidth - width) / 2
: defaultHorizontalPadding,
),
backgroundColor: widget.style?.backgroundColor,
child: Container(
padding: widget.style?.padding ?? const EdgeInsets.all(10),
Expand All @@ -98,11 +97,9 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
padding: widget.style?.searchFieldPadding ?? const EdgeInsets.all(0),
child: TextField(
cursorColor: widget.style?.searchFieldCursorColor,
decoration: widget.style?.searchFieldInputDecoration ??
InputDecoration(
suffixIcon: const Icon(Icons.search),
labelText: widget.searchText,
),
decoration:
widget.style?.searchFieldInputDecoration ??
InputDecoration(suffixIcon: const Icon(Icons.search), labelText: widget.searchText),
onChanged: (value) {
_filteredCountries = widget.countryList.stringSearch(value)
..sort(
Expand All @@ -120,16 +117,11 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
itemBuilder: (ctx, index) => Column(
children: <Widget>[
ListTile(
leading: kIsWeb
? Image.asset(
'assets/flags/${_filteredCountries[index].code.toLowerCase()}.png',
package: 'intl_phone_field',
width: 32,
)
: Text(
_filteredCountries[index].flag,
style: const TextStyle(fontSize: 18),
),
leading: Image.asset(
'assets/flags/${_filteredCountries[index].code.toLowerCase()}.png',
package: 'intl_phone_field',
width: 32,
),
contentPadding: widget.style?.listTilePadding,
title: Text(
_filteredCountries[index].localizedName(widget.languageCode),
Expand Down
72 changes: 31 additions & 41 deletions lib/intl_phone_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import './phone_number.dart';
class IntlPhoneField extends StatefulWidget {
/// The TextFormField key.
final GlobalKey<FormFieldState>? formFieldKey;
final String? Function(String?)? myValidator;

/// Whether to hide the text being edited (e.g., for passwords).
final bool obscureText;
Expand Down Expand Up @@ -283,6 +284,7 @@ class IntlPhoneField extends StatefulWidget {
this.dropdownIcon = const Icon(Icons.arrow_drop_down),
this.autofocus = false,
this.textInputAction,
this.myValidator,
this.autovalidateMode = AutovalidateMode.onUserInteraction,
this.showCountryFlag = true,
this.cursorColor,
Expand Down Expand Up @@ -319,14 +321,18 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
if (widget.initialCountryCode == null && number.startsWith('+')) {
number = number.substring(1);
// parse initial value
_selectedCountry = countries.firstWhere((country) => number.startsWith(country.fullCountryCode),
orElse: () => _countryList.first);
_selectedCountry = countries.firstWhere(
(country) => number.startsWith(country.fullCountryCode),
orElse: () => _countryList.first,
);

// remove country code from the initial number value
number = number.replaceFirst(RegExp("^${_selectedCountry.fullCountryCode}"), "");
} else {
_selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
orElse: () => _countryList.first);
_selectedCountry = _countryList.firstWhere(
(item) => item.code == (widget.initialCountryCode ?? 'US'),
orElse: () => _countryList.first,
);

// remove country code from the initial number value
if (number.startsWith('+')) {
Expand Down Expand Up @@ -399,10 +405,7 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
showCursor: widget.showCursor,
onFieldSubmitted: widget.onSubmitted,
magnifierConfiguration: widget.magnifierConfiguration,
decoration: widget.decoration.copyWith(
prefixIcon: _buildFlagsButton(),
counterText: !widget.enabled ? '' : null,
),
decoration: widget.decoration.copyWith(prefixIcon: _buildFlagsButton(), counterText: !widget.enabled ? '' : null),
style: widget.style,
onSaved: (value) {
widget.onSaved?.call(
Expand All @@ -426,16 +429,18 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {

widget.onChanged?.call(phoneNumber);
},
validator: (value) {
if (value == null || !isNumeric(value)) return validatorMessage;
if (!widget.disableLengthCheck) {
return value.length >= _selectedCountry.minLength && value.length <= _selectedCountry.maxLength
? null
: widget.invalidNumberMessage;
}

return validatorMessage;
},
validator:
widget.myValidator ??
(value) {
if (value == null || !isNumeric(value)) return validatorMessage;
if (!widget.disableLengthCheck) {
return value.length >= _selectedCountry.minLength && value.length <= _selectedCountry.maxLength
? null
: widget.invalidNumberMessage;
}

return validatorMessage;
},
maxLength: widget.disableLengthCheck ? null : _selectedCountry.maxLength,
keyboardType: widget.keyboardType,
inputFormatters: widget.inputFormatters,
Expand All @@ -461,34 +466,22 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(
width: 4,
),
const SizedBox(width: 4),
if (widget.enabled &&
widget.showDropdownIcon &&
widget.dropdownIconPosition == IconPosition.leading) ...[
widget.dropdownIcon,
const SizedBox(width: 4),
],
if (widget.showCountryFlag) ...[
kIsWeb
? Image.asset(
'assets/flags/${_selectedCountry.code.toLowerCase()}.png',
package: 'intl_phone_field',
width: 32,
)
: Text(
_selectedCountry.flag,
style: const TextStyle(fontSize: 18),
),
Image.asset(
'assets/flags/${_selectedCountry.code.toLowerCase()}.png',
package: 'intl_phone_field',
width: 32,
),
const SizedBox(width: 8),
],
FittedBox(
child: Text(
'+${_selectedCountry.dialCode}',
style: widget.dropdownTextStyle,
),
),
FittedBox(child: Text('+${_selectedCountry.dialCode}', style: widget.dropdownTextStyle)),
if (widget.enabled &&
widget.showDropdownIcon &&
widget.dropdownIconPosition == IconPosition.trailing) ...[
Expand All @@ -505,7 +498,4 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
}
}

enum IconPosition {
leading,
trailing,
}
enum IconPosition { leading, trailing }