flutter - 美国电话号码 E.164 的 Flutter TextInputFormatter 不起作用。如何使用美国 (+1) 代码进行格式化
问题描述
我正在尝试使用 Firebase Phone Auth 进行用户登录,但没有成功。我尝试添加不同的 TextInputFormatter 变体并继续收到 firebase 错误“格式不正确,E.164”。
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 = 2) + '1');
if (newValue.selection.end >= 2) selectionIndex += 1;
}
if (newTextLength >= usedSubstringIndex) newText.write(newValue.text.substring(usedSubstringIndex));
return new TextEditingValue(
text: newText.toString(),
selection: new TextSelection.collapsed(offset: selectionIndex),
);
}
}
final _uscodeformatter = NumberTextInputFormatter();
我是 Flutter 的新手,没有看到任何有关这方面的指导文档。当前的 TextInputFormatter 返回+40 43107231
而不是+14043107231
. 我尝试过使用条件语句,例如
if (newTextLength >= 10) {
newText.write('1');
if (newValue.selection.end >= 10) selectionIndex++;
}
解决方案
推荐阅读
- android - Android - 如何保存 ListView 项目列表的 ImageView 布尔值的状态?
- html - CSS中精灵图标的位置
- javascript - 条形图 Dc.Js 显示百分比
- java - 如何在我的应用上显示 Google 点动画?
- python - 如何比较一个类中的 2 个对象
- c - 为什么 lseek 使用偏移量 `-2` 而不是 `-1` 来反向读取?
- excel - EXCEL VBA:如何按单元格中的奇数和偶数对行进行排序
- python - 如何在 Python 中解析 PDB 文件?
- python - 喀拉斯。初始化双向 LSTM。传递词嵌入
- java - 编译失败 - 无法从 https://maven.repository.redhat.com/ga/javax/servlet/javax.servlet-api/maven-metadata.xml 加载 Maven 元数据