flutter TextField 限制只允许输入数字 小数点

也许你迷茫,但是我想说,在你迷茫的同时,保持本心,过好今天就好。


核心代码片段

    //只允许输入小数
    inputFormatters: [
      FilteringTextInputFormatter.allow(RegExp("[0-9.]")), 
    ],
    //键盘类型
    keyboardType: TextInputType.numberWithOptions(decimal: true),

TextField 中使用完整代码片段

TextField(
    ///键盘回车键的样式
    textInputAction: TextInputAction.next,
    //只允许输入小数
    inputFormatters: [
      FilteringTextInputFormatter.allow(RegExp("[0-9.]")), 
    ],
    //键盘类型
    keyboardType: TextInputType.numberWithOptions(decimal: true),
    //边框
    decoration: InputDecoration(
        hintText: '请输入',
        border: InputBorder.none),
    //不自动获取焦点
    autofocus: false,
)

这里采用的思路是 限制手机键盘的弹出类型为数字键盘,是带小数点的数据键盘,但是在Android手机上,会有其他特殊字符的出现,所以也需要使用正则来限制一下文本框内可输入的内容

在这里插入图片描述

inputFormatters
用于限制输入的内容,接收一个TextInputFormatter 类型的集合。


完毕


小编也写了几本书,如果你有兴趣可以去看看


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zl18603543572/article/details/122187770
今日推荐