Flutter中如何禁用TextFormField的自动文本替换功能?
解决Flutter输入框无法禁用iOS系统文本替换的问题
你设置的autocorrect: false和enableSuggestions: false仅能禁用键盘的自动纠正与输入建议,无法管控iOS系统「通用→键盘→文本替换」中的系统级替换规则。要彻底禁用这类替换,需添加iOS专属的键盘配置:
解决方案
在TextFormField中新增ios参数,配置iOS专属键盘属性以关闭系统智能替换功能:
Form( child: TextFormField( validator: (value) { if (widget.arguments.isNumber) { return Validators.weightValidator(value, context); } return Validators.emptyValidator(value, context); }, keyboardType: widget.arguments.isNumber ? const TextInputType.numberWithOptions(decimal: true) : TextInputType.text, obscureText: false, autocorrect: false, enableSuggestions: false, // 添加iOS专属配置 ios: const IOSTextFieldKeyboardSettings( autocorrectionType: UITextAutocorrectionType.no, smartDashesType: UITextSmartDashesType.disabled, smartQuotesType: UITextSmartQuotesType.disabled, enableAutocorrection: false, ), controller: _controller, decoration: InputDecoration( hintText: "kg", fillColor: Colors.white, filled: true, focusedBorder: OutlineInputBorder( borderSide: const BorderSide(color: Colors.white), borderRadius: BorderRadius.circular(20), ), enabledBorder: UnderlineInputBorder( borderSide: const BorderSide(color: Colors.white), borderRadius: BorderRadius.circular(20), ), errorBorder: UnderlineInputBorder( borderSide: const BorderSide(color: Colors.white), borderRadius: BorderRadius.circular(20), ), focusedErrorBorder: UnderlineInputBorder( borderSide: const BorderSide(color: Colors.white), borderRadius: BorderRadius.circular(20), ), ), ), key: _formKey, ),
原理说明
iOS系统的文本替换属于系统级智能输入特性,独立于键盘的自动纠正功能。通过IOSTextFieldKeyboardSettings可以直接对应原生UITextField的相关属性,彻底关闭系统层面的文本替换、智能引号/短横线等自动替换行为。
内容的提问来源于stack exchange,提问作者mr_sh
相关产品推荐
相关产品推荐

