我正在尝试重新排列用户输入的电话号码,并在区号和连字符周围加上括号。例如,用户将输入 9991234567,它会在文本字段内重新排列为 (999) 123-4567。
我正在使用 RegExp 将用户输入的区号和电话号码的 2 部分分开。我正在尝试使用 TextEditingController 在按下 Save 按钮时使用方括号和连字符编辑文本字段,但它似乎不起作用。
_saveButtonPressed() async {
RegExp phone = RegExp(r'(\d{3})(\d{3})(\d{4})');
var matches = phone.allMatches(UserProfile.instance.phone);
var match = matches.elementAt(0);
setState(() {
phoneController.text = '(${match.group(1)}) ${match.group(2)}-${match.group(3)}';
});
}
这是电话号码文本字段的代码。
_makeRowForAttribute(
imageAsset: "assets/images/phone.png",
title: "PHONE NUMBER",
keyboardType: TextInputType.phone,
placeholder: "6131110123",
charLimit: 10,
initialValue: UserProfile.instance.phone,
controller: phoneController,
onSave: (phone) {
UserProfile.instance.phone = phone.toString();
},
),