flutter 限制输入数字

TextInputType.number只能弹出数字键盘,还是可以输入特殊字符等,需要加上WhitelistingTextInputFormatter.digitsOnly

 TextField(
      inputFormatters: [
        LengthLimitingTextInputFormatter(11),
        WhitelistingTextInputFormatter.digitsOnly
      ],
      keyboardType: TextInputType.number,

WhitelistingTextInputFormatter 源码里写的很清楚

  /// A [WhitelistingTextInputFormatter] that takes in digits `[0-9]` only.
  static final WhitelistingTextInputFormatter digitsOnly
      = WhitelistingTextInputFormatter(RegExp(r'\d+'));

flutter 限制输入数字_第1张图片
flutter 限制输入数字_第2张图片
更多详解:
喜欢可以加Q群号:913934649,点赞,关注

简书: https://www.jianshu.com/u/88db5f15770d

csdn:https://me.csdn.net/beyondforme

掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7

你可能感兴趣的:(flutter)