From 1efbaf51d7db5f902b59c4d26192ab0f0a10c68a Mon Sep 17 00:00:00 2001 From: Arash Date: Mon, 20 Jan 2025 18:10:36 +0330 Subject: [PATCH 1/4] Add county code in format phone number --- demo/lib/main.dart | 11 +++++++++++ lib/src/formatting/phone_number_formatter.dart | 7 +++++++ lib/src/phone_number.dart | 4 ++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/demo/lib/main.dart b/demo/lib/main.dart index 840679e..92a33df 100644 --- a/demo/lib/main.dart +++ b/demo/lib/main.dart @@ -86,6 +86,17 @@ class _MyHomePageState extends State { format: NsnFormat.international)) : const Text('-'), ), + ListTile( + title: const Text('Formatted with country code'), + trailing: phoneNumber != null + ? Text( + phoneNumber.formatNsn( + format: NsnFormat.international, + showCountyCode: true, + ), + ) + : const Text('-'), + ), ListTile( title: const Text('Iso code'), trailing: phoneNumber != null diff --git a/lib/src/formatting/phone_number_formatter.dart b/lib/src/formatting/phone_number_formatter.dart index 2c3529e..52a67fe 100644 --- a/lib/src/formatting/phone_number_formatter.dart +++ b/lib/src/formatting/phone_number_formatter.dart @@ -18,6 +18,7 @@ class PhoneNumberFormatter { String nsn, IsoCode isoCode, [ NsnFormat format = NsnFormat.national, + bool showCountyCode = false, ]) { if (nsn.isEmpty) { return nsn; @@ -51,7 +52,13 @@ class PhoneNumberFormatter { pattern: formatingRule.pattern, transformRule: transformRule, ); + formatted = _removeMissingDigits(formatted, missingDigits); + + if (showCountyCode) { + formatted = '+${MetadataFinder.findMetadataForIsoCode(isoCode).countryCode} $formatted'; + } + return formatted; } diff --git a/lib/src/phone_number.dart b/lib/src/phone_number.dart index c7e5ee6..32acd73 100644 --- a/lib/src/phone_number.dart +++ b/lib/src/phone_number.dart @@ -64,8 +64,8 @@ class PhoneNumber { ); /// formats the nsn, if no [isoCode] is provided the phone number region is used. - String formatNsn({IsoCode? isoCode, NsnFormat format = NsnFormat.national}) => - PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode, format); + String formatNsn({IsoCode? isoCode, NsnFormat format = NsnFormat.national, bool showCountyCode = false}) => + PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode, format, showCountyCode); @Deprecated('Use [formatNsn] instead') String getFormattedNsn({IsoCode? isoCode}) => formatNsn(isoCode: isoCode); From d62fd98e605e2967a66ad0e8aec2b12de589b9fc Mon Sep 17 00:00:00 2001 From: Arash Date: Tue, 21 Jan 2025 10:00:15 +0330 Subject: [PATCH 2/4] Add county code in format phone number --- demo/lib/main.dart | 3 +-- lib/src/formatting/phone_number_formatter.dart | 5 ----- lib/src/phone_number.dart | 11 +++++++++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/demo/lib/main.dart b/demo/lib/main.dart index 92a33df..f95bb01 100644 --- a/demo/lib/main.dart +++ b/demo/lib/main.dart @@ -90,9 +90,8 @@ class _MyHomePageState extends State { title: const Text('Formatted with country code'), trailing: phoneNumber != null ? Text( - phoneNumber.formatNsn( + phoneNumber.format( format: NsnFormat.international, - showCountyCode: true, ), ) : const Text('-'), diff --git a/lib/src/formatting/phone_number_formatter.dart b/lib/src/formatting/phone_number_formatter.dart index 52a67fe..ee7e0b6 100644 --- a/lib/src/formatting/phone_number_formatter.dart +++ b/lib/src/formatting/phone_number_formatter.dart @@ -18,7 +18,6 @@ class PhoneNumberFormatter { String nsn, IsoCode isoCode, [ NsnFormat format = NsnFormat.national, - bool showCountyCode = false, ]) { if (nsn.isEmpty) { return nsn; @@ -55,10 +54,6 @@ class PhoneNumberFormatter { formatted = _removeMissingDigits(formatted, missingDigits); - if (showCountyCode) { - formatted = '+${MetadataFinder.findMetadataForIsoCode(isoCode).countryCode} $formatted'; - } - return formatted; } diff --git a/lib/src/phone_number.dart b/lib/src/phone_number.dart index 32acd73..b43f4d6 100644 --- a/lib/src/phone_number.dart +++ b/lib/src/phone_number.dart @@ -64,8 +64,15 @@ class PhoneNumber { ); /// formats the nsn, if no [isoCode] is provided the phone number region is used. - String formatNsn({IsoCode? isoCode, NsnFormat format = NsnFormat.national, bool showCountyCode = false}) => - PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode, format, showCountyCode); + String formatNsn({IsoCode? isoCode, NsnFormat format = NsnFormat.national}) => + PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode, format); + + String format({IsoCode? isoCode, NsnFormat format = NsnFormat.national}) { + final countryCode = '+${MetadataFinder.findMetadataForIsoCode(isoCode ?? this.isoCode).countryCode}'; + final formatted = formatNsn(isoCode: isoCode, format: format); + + return '$countryCode $formatted'; + } @Deprecated('Use [formatNsn] instead') String getFormattedNsn({IsoCode? isoCode}) => formatNsn(isoCode: isoCode); From faf810ca639994a9469a415f8edad411391c82b7 Mon Sep 17 00:00:00 2001 From: Arash Date: Tue, 21 Jan 2025 21:25:38 +0330 Subject: [PATCH 3/4] Add county code in format phone number --- lib/src/phone_number.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/src/phone_number.dart b/lib/src/phone_number.dart index b43f4d6..430d728 100644 --- a/lib/src/phone_number.dart +++ b/lib/src/phone_number.dart @@ -68,10 +68,7 @@ class PhoneNumber { PhoneNumberFormatter.formatNsn(nsn, isoCode ?? this.isoCode, format); String format({IsoCode? isoCode, NsnFormat format = NsnFormat.national}) { - final countryCode = '+${MetadataFinder.findMetadataForIsoCode(isoCode ?? this.isoCode).countryCode}'; - final formatted = formatNsn(isoCode: isoCode, format: format); - - return '$countryCode $formatted'; + return '+$countryCode ${formatNsn(isoCode: isoCode, format: format)}'; } @Deprecated('Use [formatNsn] instead') From 50a3367c09d10eea7041963c877d241fa17f997f Mon Sep 17 00:00:00 2001 From: Arash Date: Thu, 23 Jan 2025 18:02:42 +0330 Subject: [PATCH 4/4] Add county code in format phone number --- example/lib/main.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index efec3d1..ac7908c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -36,10 +36,12 @@ void main(List arguments) { print(''); print('Formatting:'); final phoneNumber = - PhoneNumber.parse('2025550119', destinationCountry: IsoCode.US); + PhoneNumber.parse('+12025550119', destinationCountry: IsoCode.US); final formattedNsn = phoneNumber.formatNsn(); - print('formatted: $formattedNsn'); // (202) 555-0119 + print('formatted nsn: $formattedNsn'); // (202) 555-0119 print('international: ${phoneNumber.international}'); + final formatted = phoneNumber.format(); + print('formatted with country code: $formatted'); // +1 (202) 555-0119 // Ranges print(''); print('Ranges:');