flutter - 为什么当我在输入格式的 TextFormField 中键入 "-"或 ""两次时,整个文本都会消失?

标签 flutter dart

我认为这个问题的主要原因是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/

相关文章:

android-studio - Android SDK 工具(已过时)

dart - 在 Flutter 中从文件中读取二维码

animation - Flutter 设置开始动画值

dart - 使用 Angular Dart 中的日志记录包的记录器服务

android - 未处理的异常:DioError PONSE]:Http状态错误[500]

flutter - 标记未在 map 上添加

flutter - 方法 '[]' 在 null 上调用,但在 App 中有效

flutter - flutter 中的异步验证仍然存在问题

oop - 允许在非抽象类中声明抽象方法的目的是什么?

dart - TabController 构造函数中的 `vsync` 属性