我希望使用
我已经尝试使用以下
TextFormField
将输入数字格式化为正确的电话号码格式。我已经尝试使用以下
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),
);
}
}
但我没有得到预期的输出
我的预期号码格式为"+1 123-456-7890"
如果需要更多信息,请告诉我。谢谢您的努力,将不胜感激。