You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS键盘问题:使用TextInputType.number时缺失小数点

解决方案

问题根源在于TextInputType.number在iOS平台的默认行为——它会唤起纯数字键盘(不含小数点),而Android平台会自动包含小数点。要让iOS键盘显示小数点,需明确指定支持小数输入的键盘类型:

  • 将keyboardType: TextInputType.number替换为keyboardType: TextInputType.numberWithOptions(decimal: true)

修改后的完整代码片段:

Container(
  width: 65.w,
  height: 6.w,
  child: TextFormField(
    controller: _currentWeightController,
    textAlign: TextAlign.left,
    // 替换为支持小数的数字键盘类型
    keyboardType: TextInputType.numberWithOptions(decimal: true),
    inputFormatters: [
      FilteringTextInputFormatter.allow(RegExp('[0-9.]')),
    ],
    style: TextStyle(
        fontSize: 20,
        color: Colors.black),
  ),
),

补充说明

  • numberWithOptions(decimal: true)会让iOS唤起带小数点的数字键盘,同时Android平台仍保持原有正确表现。
  • 你配置的FilteringTextInputFormatter可以保留,用来限制用户仅输入数字和小数点,和修改后的键盘类型配合能进一步确保输入合法性。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 16:53:11