I want to make my TextFormField formats input number in proper phone number format.
I'm have tried using below TextInputFormatter
class NumberTextInputFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
final int newTextLength = newValue.text.length;
int selectionIndex = newValue.selection.end;
int usedSubstringIndex = 0;
final StringBuffer newText = new StringBuffer();
if (newTextLength >= 1) {
newText.write('+');
if (newValue.selection.end >= 1) selectionIndex++;
}
if (newTextLength >= 3) {
newText.write(newValue.text.substring(0, usedSubstringIndex = 1) + ' ');
if (newValue.selection.end >= 2) selectionIndex += 1;
}
if (newTextLength >= 3) {
newText.write( ' ');
if (newValue.selection.end >= 6) selectionIndex += 1;
}
if (newTextLength >= usedSubstringIndex)
newText.write(newValue.text.substring(usedSubstringIndex));
return new TextEditingValue(
text: newText.toString(),
selection: new TextSelection.collapsed(offset: selectionIndex),
);
}
}
but i'm not getting expected output
My expected number format "+1 123-456-7890"
If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.