flutter TextField is restricted to only allow input of decimal points

Maybe you are confused, but I want to say that while you are confused, keep your heart and live a good day.


core code snippet

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

Complete code snippet used in TextField

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

The idea here is to limit the pop-up type of the mobile phone keyboard to a numeric keyboard, which is a data keyboard with a decimal point, but on Android phones, there will be other special characters, so it is also necessary to use a regular to limit the input in the text box. content

insert image description here

inputFormatters
are used to limit the content of the input and receive a collection of type TextInputFormatter.


complete


I also wrote a few books, if you are interested, you can read them


insert image description here

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/122187770