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]"),
    ),
  ],
),
相关推荐
健了个平_245 小时前
【iOS】关于传统竖屏 UIKit 项目如何简单适配 iPhone Duo 的第一版
ios·app·swift
iOS开发上架哦9 小时前
抓包怎么修改响应?响应头、响应体、脚本注入实操教程
后端·ios
开心就好20259 小时前
在 Xcode 中运行和调试单元测试的完整指南
后端·ios
庚鑫9 小时前
Xcode 26.3 调试 iOS 27:从设备元数据报错到 GUI Run
ios·objective-c·xcode
穆梓兰煊11 小时前
Flutter端云协同推理架构解析
flutter·github
CocoaKier12 小时前
如何安装旧版的MacOS系统?
macos·ios·mac
2501_9151063212 小时前
iOS开发之Xcode常用调试技巧总结
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
美狐美颜SDK开放平台14 小时前
直播APP平台搭建需要哪些技术?视频美颜SDK只是其中一环
android·ios·音视频·直播美颜sdk·视频美颜sdk
FungLeo14 小时前
成为全栈·Flutter App 篇·内置 WebView:网页历史、App 返回与外链安全
flutter·网络安全·移动开发·webview·路由管理·成为全栈
小π军15 小时前
安卓、iOS、鸿蒙三端的理解
android·ios·harmonyos