flutter TextField限制中文,ios自带中文输入法变英文输入问题解决

由于业务需求,要限制TextField只能输入中文,但是测试在iOS测试机发现自带中文输入法会变英文输入问题,安卓没有问题,并且只有iOS自带输入法有问题,搜狗等输入法没问题。我们目前使用flutter2.5.3版本,高版本应该不存在这个问题。

TextField限制中文代码片段:

复制代码
TextField(
  inputFormatters: [
    FilteringTextInputFormatter.allow(RegExp('[a-zA-Z]|[\u4e00-\u9fa5]|[0-9]')),
  ],
)

解决方案,自定义过滤器:

复制代码
//自定义过滤器
class CustomizedTextInputFormatter extends TextInputFormatter {
  final Pattern filterPattern;

  CustomizedTextInputFormatter({this.filterPattern})
      : assert(filterPattern != null);

  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    if (newValue.isComposingRangeValid) return newValue;
    return FilteringTextInputFormatter.allow(filterPattern)
        .formatEditUpdate(oldValue, newValue);
  }
}

TextField(
  inputFormatters: [
    //使用自定义过滤器,限制中文输入
    CustomizedTextInputFormatter(
      filterPattern: RegExp("[a-zA-Z]|[\u4e00-\u9fa5]|[0-9]"),
    ),
  ],
),
相关推荐
Magnetic_h2 小时前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
00后程序员张4 小时前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
前端小超超4 小时前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa
2501_915106325 小时前
Xcode 上传 ipa 全流程详解 App Store 上架流程、uni-app 生成 ipa 文件上传与审核指南
android·macos·ios·小程序·uni-app·iphone·xcode
kymjs张涛8 小时前
零一开源|前沿技术周刊 #16
ios·apple·hacker news
2301_821046528 小时前
Python与Go结合
ios·iphone
他们都不看好你,偏偏你最不争气9 小时前
【iOS】AFNetworking
开发语言·macos·ios·objective-c
zhanggui11 小时前
iOS Debug Symbols
ios·xcode·debug symbox
ALLIN12 小时前
Flutter 三种方式实现页面切换后保持原页面状态
flutter
Dabei12 小时前
Flutter 国际化
flutter