Flutter TextField 边框设置无效/限制输入小数(一位小数点)

 需求:每行添加分隔线,隐藏所有下划线展示

 

实现

TextField(
  style: TextStyle(fontSize: 14),
  enabled: false,
  decoration: InputDecoration(
    border: InputBorder.none,
    suffixIcon: Container(
      child: IconButton(
        padding: EdgeInsets.only(left: 10, top: 8),
        color: Theme.of(context).accentColor,
        icon: Icon(Icons.search),
        iconSize: 25,
        onPressed: () {},
      ),
    ),
  ),
)

增加border: InputBorder.none未生效

增加enabledBorder: InputBorder.none,focusedBorder: InputBorder.none也无效

最后发现是设置了enabled: false 禁用状态

改为disabledBorder: InputBorder.none后成功

TextField(
  style: TextStyle(fontSize: 14),
  enabled: false,
  decoration: InputDecoration(
    disabledBorder: InputBorder.none,
    suffixIcon: Container(
      child: IconButton(
        padding: EdgeInsets.only(left: 10, top: 8),
        color: Theme.of(context).accentColor,
        icon: Icon(Icons.search),
        iconSize: 25,
        onPressed: () {},
      ),
    ),
  ),
)

总结 

 TextField状态为启用时可以使用border设置边框,对应状态边框根据需求设置。但处于禁用状态时只能使用disabledBorder来设置边框样式。

参考链接
Flutter TextField 设置边框无效https://blog.csdn.net/bawomingtian123/article/details/121954521


输入小数

限制输入数字+小数点 且小数点只有一位

inputFormatters: [
    TextInputFormatter.withFunction((oldValue, newValue) => newValue.text.indexOf(".") != newValue.text.lastIndexOf(".") ? oldValue : newValue),
    FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
]

小数点后两位 

keyboardType: TextInputType.number,
inputFormatters: [
    TextInputFormatter.withFunction((oldValue, newValue) => 
        newValue.text.indexOf(".") != newValue.text.lastIndexOf(".") || 
        newValue.text.indexOf(".") > -1 && ((newValue.text.length - 1) - newValue.text.lastIndexOf(".")) > 2 
        ? oldValue : newValue
    ),
    FilteringTextInputFormatter.allow(RegExp("[1-9]{1}[0-9.]*"))
],
改为RegExp(r'[1-9]{1}[0-9.]*') -- 首位不为0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值