Flutter:KeyboardType属性无法在TextFormField中正常工作,仍然可以粘贴文本。如何更改输入类型?(Flutter)

问题描述 投票:0回答:1

我正在使用TextFormField来获取数字作为输入,因此我使用了此代码段

keyboardType: TextInputType.number,

这是我的TextFormField窗口小部件代码

         TextFormField(
                  keyboardType: TextInputType.number,
                  style: textstyle,
                  controller: principal,
                  validator: (String value) {
                    if (value.isEmpty) {
                      return "Please enter principal amount e.g 3245";
                    }
                  },
                  decoration: InputDecoration(
                      labelText: "Principal",
                      labelStyle: textstyle,
                      hintText: "Rupees",
                      border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(5.0))),
                )

但是现在我可以在此处显示的相同字段中粘贴一些文本enter image description here

所以现在我该如何格式化TextFormfield以限制仅键入数字和限制粘贴文本?

android flutter textfield flutter-layout
1个回答
1
投票

您可以尝试使用WhitelistingTextInputFormatter喜欢

inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],

请参见官方docs

© www.soinside.com 2019 - 2024. All rights reserved.