Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for custom input formatters #353

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"libphonenumber","path":"/Users/nathaniel.ogunye/.pub-cache/hosted/pub.dartlang.org/libphonenumber-2.0.2/","native_build":true,"dependencies":[]},{"name":"libphonenumber_plugin","path":"/Users/nathaniel.ogunye/.pub-cache/hosted/pub.dartlang.org/libphonenumber_plugin-0.2.3/","native_build":true,"dependencies":["libphonenumber"]}],"android":[{"name":"libphonenumber","path":"/Users/nathaniel.ogunye/.pub-cache/hosted/pub.dartlang.org/libphonenumber-2.0.2/","native_build":true,"dependencies":[]},{"name":"libphonenumber_plugin","path":"/Users/nathaniel.ogunye/.pub-cache/hosted/pub.dartlang.org/libphonenumber_plugin-0.2.3/","native_build":true,"dependencies":["libphonenumber"]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"libphonenumber_web","path":"/Users/nathaniel.ogunye/.pub-cache/hosted/pub.dartlang.org/libphonenumber_web-0.2.0+1/","dependencies":[]}]},"dependencyGraph":[{"name":"libphonenumber","dependencies":[]},{"name":"libphonenumber_plugin","dependencies":["libphonenumber_web","libphonenumber"]},{"name":"libphonenumber_web","dependencies":[]}],"date_created":"2022-09-21 11:56:42.923067","version":"3.3.0"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"libphonenumber","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/libphonenumber-2.0.2/","native_build":true,"dependencies":[]},{"name":"libphonenumber_plugin","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/libphonenumber_plugin-0.2.3/","native_build":true,"dependencies":["libphonenumber"]}],"android":[{"name":"libphonenumber","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/libphonenumber-2.0.2/","native_build":true,"dependencies":[]},{"name":"libphonenumber_plugin","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/libphonenumber_plugin-0.2.3/","native_build":true,"dependencies":["libphonenumber"]}],"macos":[],"linux":[],"windows":[],"web":[{"name":"libphonenumber_web","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/libphonenumber_web-0.2.0+1/","dependencies":[]}]},"dependencyGraph":[{"name":"libphonenumber","dependencies":[]},{"name":"libphonenumber_plugin","dependencies":["libphonenumber_web","libphonenumber"]},{"name":"libphonenumber_web","dependencies":[]}],"date_created":"2022-10-11 12:06:09.311884","version":"3.3.0"}
5 changes: 5 additions & 0 deletions lib/src/models/country_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Country {
/// The flagUri which links to the flag for the [Country] in the library assets
final String flagUri;

final String? numCode;

/// The nameTranslation for translation
final Map<String, String>? nameTranslations;

Expand All @@ -27,6 +29,7 @@ class Country {
required this.alpha3Code,
required this.dialCode,
required this.flagUri,
this.numCode,
this.nameTranslations,
});

Expand All @@ -36,6 +39,7 @@ class Country {
name: data['en_short_name'],
alpha2Code: data['alpha_2_code'],
alpha3Code: data['alpha_3_code'],
numCode: data['num_code'],
dialCode: data['dial_code'],
flagUri: 'assets/flags/${data['alpha_2_code'].toLowerCase()}.png',
nameTranslations: data['nameTranslations'] != null
Expand All @@ -61,5 +65,6 @@ class Country {
'alpha2: $alpha2Code, '
'alpha3: $alpha3Code, '
'dialCode: $dialCode '
'numCode: $numCode '
'}';
}
4 changes: 3 additions & 1 deletion lib/src/utils/formatter/as_you_type_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ class AsYouTypeFormatter extends TextInputFormatter {
Future<String?> formatAsYouType({required String input}) async {
try {
String? formattedPhoneNumber = await PhoneNumberUtil.formatAsYouType(
phoneNumber: input, isoCode: isoCode);
phoneNumber: input,
isoCode: isoCode,
);
return formattedPhoneNumber;
} on Exception {
return '';
Expand Down
24 changes: 22 additions & 2 deletions lib/src/utils/phone_number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PhoneNumber extends Equatable {

/// Country [isoCode] of the phone number
final String? isoCode;

final String? numCode;
/// [_hash] is used to compare instances of [PhoneNumber] object.
final int _hash;

Expand All @@ -46,12 +46,32 @@ class PhoneNumber extends Equatable {
PhoneNumber({
this.phoneNumber,
this.dialCode,
this.numCode,
this.isoCode,
}) : _hash = 1000 + Random().nextInt(99999 - 1000);

String? getFormattedNumCode() {
final phoneWithoutDialCode = phoneNumber?.trim()
.replaceAll(RegExp('[^0-9]'), '')
.replaceFirst(dialCode?.replaceAll(RegExp('[^0-9]'), '') ?? '', '');
if ((phoneWithoutDialCode?.length ?? 0) < (dialCode?.length ?? 0)) {
return phoneWithoutDialCode
?.substring(0, phoneWithoutDialCode.length);
}
return phoneWithoutDialCode
?.substring(0, numCode?.length ?? 0);
}

String? getFormattedPhone() {
return phoneNumber?.trim()
.replaceAll(RegExp('[^0-9]'), '')
.replaceFirst(dialCode?.replaceAll(RegExp('[^0-9]'), '') ?? '', '')
.replaceFirst(getFormattedNumCode() ?? '', '');
}

@override
String toString() {
return 'PhoneNumber(phoneNumber: $phoneNumber, dialCode: $dialCode, isoCode: $isoCode)';
return 'PhoneNumber(phoneNumber: $phoneNumber, dialCode: $dialCode, isoCode: $isoCode, numCode: $numCode)';
}

/// Returns [PhoneNumber] which contains region information about
Expand Down
26 changes: 20 additions & 6 deletions lib/src/widgets/input_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ class InternationalPhoneNumberInput extends StatefulWidget {
final TextAlign textAlign;
final TextAlignVertical textAlignVertical;
final EdgeInsets scrollPadding;

final List<TextInputFormatter>? inputFormatters;
final FocusNode? focusNode;
final Iterable<String>? autofillHints;

final ValueChanged<Country?>? onCountryChanged;
final List<String>? countries;

InternationalPhoneNumberInput(
{Key? key,
this.selectorConfig = const SelectorConfig(),
required this.onInputChanged,
this.onCountryChanged,
this.onInputValidated,
this.onSubmit,
this.onFieldSubmitted,
Expand Down Expand Up @@ -124,6 +125,7 @@ class InternationalPhoneNumberInput extends StatefulWidget {
this.focusNode,
this.cursorColor,
this.autofillHints,
this.inputFormatters,
this.countries})
: super(key: key);

Expand Down Expand Up @@ -235,10 +237,14 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
'${this.country?.dialCode}$parsedPhoneNumberString';

if (widget.onInputChanged != null) {
widget.onInputChanged!(PhoneNumber(
widget.onInputChanged!(
PhoneNumber(
phoneNumber: phoneNumber,
numCode: this.country?.numCode,
isoCode: this.country?.alpha2Code,
dialCode: this.country?.dialCode));
dialCode: this.country?.dialCode,
),
);
}

if (widget.onInputValidated != null) {
Expand All @@ -247,10 +253,14 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
this.isNotValid = true;
} else {
if (widget.onInputChanged != null) {
widget.onInputChanged!(PhoneNumber(
widget.onInputChanged!(
PhoneNumber(
phoneNumber: phoneNumber,
numCode: this.country?.numCode,
isoCode: this.country?.alpha2Code,
dialCode: this.country?.dialCode));
dialCode: this.country?.dialCode,
),
);
}

if (widget.onInputValidated != null) {
Expand Down Expand Up @@ -338,6 +348,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {

/// Changes Selector Button Country and Validate Change.
void onCountryChanged(Country? country) {
widget.onCountryChanged?.call(country);
setState(() {
this.country = country;
});
Expand All @@ -356,6 +367,7 @@ class _InputWidgetState extends State<InternationalPhoneNumberInput> {
PhoneNumber(
phoneNumber: phoneNumber,
isoCode: this.country?.alpha2Code,
numCode: this.country?.numCode,
dialCode: this.country?.dialCode),
);
}
Expand Down Expand Up @@ -451,6 +463,8 @@ class _InputWidgetView
},
)
: FilteringTextInputFormatter.digitsOnly,
if (widget.inputFormatters != null)
...widget.inputFormatters!
],
onChanged: state.onChanged,
),
Expand Down