我认为这个问题的主要原因是FilteringTextInputFormatter中的RegExp。但我不知道为什么以及如何解决它......
TextFormField(
maxLength: 12,
buildCounter: (
BuildContext context, {
required int currentLength,
required bool isFocused,
required int? maxLength,
}) {
return null;
},
cursorColor: Colors.black,
decoration: InputDecoration(
labelText: 'Just Put numbers',
labelStyle: TextStyle(color: Colors.black),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.black,
),
),
),
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'^[0-9\s-]+$')),
],
)
我使用此代码是因为我需要使用“xxx-xxxx-xxxx”等数字模式填充 TextFormField
,而不仅仅是任何文本。
FilteringTextInputFormatter.allow( RegExp(r'^[0-9\s-]+$')),
最佳答案
这可能是由 system shortcuts 引起的在 iOS 上。 默认情况下
- 双空格
。
- 双连字符
--
将替换为单个较长的破折号–
因此,您的正则表达式将不再匹配。
还有FilteringTextInputFormatter
将用其 replacementString
替换所有内容,默认情况下为空字符串。
关于flutter - 为什么当我在输入格式的 TextFormField 中键入 "-"或 ""两次时,整个文本都会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77511321/