changeNationalNumber method
dynamic
changeNationalNumber(
- String? text
)
Implementation
changeNationalNumber(String? text) {
text = text ?? '';
final oldFormattedText = _value.formatNsn();
var newFormattedText = text;
bool isDeleting = text.length < oldFormattedText.length;
// if starts with + then we parse the whole number
final startsWithPlus =
text.startsWith(RegExp('[${AllowedCharacters.plus}]'));
if (startsWithPlus) {
final phoneNumber = _tryParseWithPlus(text);
// if we could parse the phone number we can change the value inside
// the national number field to remove the "+ country dial code"
if (phoneNumber != null) {
_value = phoneNumber;
newFormattedText = _value.formatNsn();
}
} else if (isDeleting &&
text.startsWith(
RegExp('^\\([${AllowedCharacters.digits}]+(?!.*\\))'))) {
// Handle case where the phone number contains an area code such as (416), and user has begun to delete it, i.e. the text input is now (416.
// We need to skip parsing/formatting here, else the parentheses will be added back
} else {
final phoneNumber = PhoneNumber.parse(
text,
destinationCountry: _value.isoCode,
);
_value = phoneNumber;
newFormattedText = phoneNumber.formatNsn();
}
_changeFormattedNationalNumber(newFormattedText);
notifyListeners();
}