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
44 changes: 30 additions & 14 deletions lib/country_picker_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class CountryPickerDialog extends StatefulWidget {
final List<Country> filteredCountries;
final PickerDialogStyle? style;
final String languageCode;
final TextStyle? searchStyle;

const CountryPickerDialog({
Key? key,
Expand All @@ -55,11 +56,12 @@ class CountryPickerDialog extends StatefulWidget {
required this.onCountryChanged,
required this.selectedCountry,
required this.filteredCountries,
this.searchStyle,
this.style,
}) : super(key: key);

@override
State<CountryPickerDialog> createState() => _CountryPickerDialogState();
_CountryPickerDialogState createState() => _CountryPickerDialogState();
}

class _CountryPickerDialogState extends State<CountryPickerDialog> {
Expand All @@ -71,7 +73,9 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
_selectedCountry = widget.selectedCountry;
_filteredCountries = widget.filteredCountries.toList()
..sort(
(a, b) => a.localizedName(widget.languageCode).compareTo(b.localizedName(widget.languageCode)),
(a, b) => a
.localizedName(widget.languageCode)
.compareTo(b.localizedName(widget.languageCode)),
);

super.initState();
Expand All @@ -84,19 +88,25 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
const defaultHorizontalPadding = 40.0;
const defaultVerticalPadding = 24.0;
return Dialog(
insetPadding: EdgeInsets.symmetric(
vertical: defaultVerticalPadding,
horizontal: mediaWidth > (width + defaultHorizontalPadding * 2)
? (mediaWidth - width) / 2
: defaultHorizontalPadding),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4),

),
insetPadding: EdgeInsets.symmetric(
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),
child: Column(
children: <Widget>[
Padding(
padding: widget.style?.searchFieldPadding ?? const EdgeInsets.all(0),
padding:
widget.style?.searchFieldPadding ?? const EdgeInsets.all(0),
child: TextField(
style: widget.searchStyle,
cursorColor: widget.style?.searchFieldCursorColor,
decoration: widget.style?.searchFieldInputDecoration ??
InputDecoration(
Expand All @@ -106,7 +116,9 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
onChanged: (value) {
_filteredCountries = widget.countryList.stringSearch(value)
..sort(
(a, b) => a.localizedName(widget.languageCode).compareTo(b.localizedName(widget.languageCode)),
(a, b) => a
.localizedName(widget.languageCode)
.compareTo(b.localizedName(widget.languageCode)),
);
if (mounted) setState(() {});
},
Expand All @@ -128,24 +140,28 @@ class _CountryPickerDialogState extends State<CountryPickerDialog> {
)
: Text(
_filteredCountries[index].flag,
style: const TextStyle(fontSize: 18),
style: const TextStyle(fontSize: 16),
),
contentPadding: widget.style?.listTilePadding,
title: Text(
_filteredCountries[index].localizedName(widget.languageCode),
style: widget.style?.countryNameStyle ?? const TextStyle(fontWeight: FontWeight.w700),
_filteredCountries[index]
.localizedName(widget.languageCode),
style: widget.style?.countryNameStyle ??
const TextStyle(fontWeight: FontWeight.w700),
),
trailing: Text(
'+${_filteredCountries[index].dialCode}',
style: widget.style?.countryCodeStyle ?? const TextStyle(fontWeight: FontWeight.w700),
style: widget.style?.countryCodeStyle ??
const TextStyle(fontWeight: FontWeight.w700),
),
onTap: () {
_selectedCountry = _filteredCountries[index];
widget.onCountryChanged(_selectedCountry);
Navigator.of(context).pop();
},
),
widget.style?.listTileDivider ?? const Divider(thickness: 1),
widget.style?.listTileDivider ??
const Divider(thickness: 1),
],
),
),
Expand Down
48 changes: 24 additions & 24 deletions lib/intl_phone_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import './countries.dart';
import './phone_number.dart';

class IntlPhoneField extends StatefulWidget {
/// The TextFormField key.
final GlobalKey<FormFieldState>? formFieldKey;

/// Whether to hide the text being edited (e.g., for passwords).
final bool obscureText;

Expand Down Expand Up @@ -148,7 +145,7 @@ class IntlPhoneField extends StatefulWidget {
///
/// Specify null to remove the decoration entirely (including the
/// extra padding introduced by the decoration to save space for the labels).
final InputDecoration decoration;
final InputDecoration? decoration;

/// The style to use for the text being edited.
///
Expand Down Expand Up @@ -243,15 +240,11 @@ class IntlPhoneField extends StatefulWidget {
/// If unset, defaults to [EdgeInsets.zero].
final EdgeInsets flagsButtonMargin;

/// Enable the autofill hint for phone number.
//enable the autofill hint for phone number
final bool disableAutoFillHints;

/// If null, default magnification configuration will be used.
final TextMagnifierConfiguration? magnifierConfiguration;

const IntlPhoneField({
Key? key,
this.formFieldKey,
this.initialCountryCode,
this.languageCode = 'en',
this.disableAutoFillHints = false,
Expand All @@ -264,7 +257,7 @@ class IntlPhoneField extends StatefulWidget {
this.keyboardType = TextInputType.phone,
this.controller,
this.focusNode,
this.decoration = const InputDecoration(),
this.decoration,
this.style,
this.dropdownTextStyle,
this.onSubmitted,
Expand All @@ -278,7 +271,8 @@ class IntlPhoneField extends StatefulWidget {
this.inputFormatters,
this.enabled = true,
this.keyboardAppearance,
@Deprecated('Use searchFieldInputDecoration of PickerDialogStyle instead') this.searchText = 'Search country',
@Deprecated('Use searchFieldInputDecoration of PickerDialogStyle instead')
this.searchText = 'Search country',
this.dropdownIconPosition = IconPosition.leading,
this.dropdownIcon = const Icon(Icons.arrow_drop_down),
this.autofocus = false,
Expand All @@ -295,11 +289,10 @@ class IntlPhoneField extends StatefulWidget {
this.showCursor = true,
this.pickerDialogStyle,
this.flagsButtonMargin = EdgeInsets.zero,
this.magnifierConfiguration,
}) : super(key: key);

@override
State<IntlPhoneField> createState() => _IntlPhoneFieldState();
_IntlPhoneFieldState createState() => _IntlPhoneFieldState();
}

class _IntlPhoneFieldState extends State<IntlPhoneField> {
Expand All @@ -319,20 +312,25 @@ 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),
_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}"), "");
number = number.replaceFirst(
RegExp("^${_selectedCountry.fullCountryCode}"), "");
} else {
_selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
_selectedCountry = _countryList.firstWhere(
(item) => item.code == (widget.initialCountryCode ?? 'US'),
orElse: () => _countryList.first);

// remove country code from the initial number value
if (number.startsWith('+')) {
number = number.replaceFirst(RegExp("^\\+${_selectedCountry.fullCountryCode}"), "");
number = number.replaceFirst(
RegExp("^\\+${_selectedCountry.fullCountryCode}"), "");
} else {
number = number.replaceFirst(RegExp("^${_selectedCountry.fullCountryCode}"), "");
number = number.replaceFirst(
RegExp("^${_selectedCountry.fullCountryCode}"), "");
}
}

Expand Down Expand Up @@ -382,9 +380,10 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
@override
Widget build(BuildContext context) {
return TextFormField(
key: widget.formFieldKey,
initialValue: (widget.controller == null) ? number : null,
autofillHints: widget.disableAutoFillHints ? null : [AutofillHints.telephoneNumberNational],
autofillHints: widget.disableAutoFillHints
? null
: [AutofillHints.telephoneNumberNational],
readOnly: widget.readOnly,
obscureText: widget.obscureText,
textAlign: widget.textAlign,
Expand All @@ -398,8 +397,7 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
cursorWidth: widget.cursorWidth,
showCursor: widget.showCursor,
onFieldSubmitted: widget.onSubmitted,
magnifierConfiguration: widget.magnifierConfiguration,
decoration: widget.decoration.copyWith(
decoration:widget.decoration?? widget.decoration?.copyWith(
prefixIcon: _buildFlagsButton(),
counterText: !widget.enabled ? '' : null,
),
Expand All @@ -408,7 +406,8 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
widget.onSaved?.call(
PhoneNumber(
countryISOCode: _selectedCountry.code,
countryCode: '+${_selectedCountry.dialCode}${_selectedCountry.regionCode}',
countryCode:
'+${_selectedCountry.dialCode}${_selectedCountry.regionCode}',
number: value!,
),
);
Expand All @@ -429,7 +428,8 @@ class _IntlPhoneFieldState extends State<IntlPhoneField> {
validator: (value) {
if (value == null || !isNumeric(value)) return validatorMessage;
if (!widget.disableLengthCheck) {
return value.length >= _selectedCountry.minLength && value.length <= _selectedCountry.maxLength
return value.length >= _selectedCountry.minLength &&
value.length <= _selectedCountry.maxLength
? null
: widget.invalidNumberMessage;
}
Expand Down