Flutter:KeyboardType 属性在 TextFormField 中没有按预期工作,仍然可以粘贴 text.how 来更改输入类型?(Flutter)
Flutter: KeyboardType attribute not working as expected in TextFormField ,still can paste text.how to change input type?(Flutter)
我正在使用 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))),
)
但现在我可以在此处显示的同一字段中粘贴一些文本
那么现在我如何格式化我的 TextFormfield 以限制仅键入数字并限制粘贴文本?
你可以尝试使用WhitelistingTextInputFormatter
喜欢
inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
见官方docs
自 Flutter 版本 1.20.0-1 起,已接受的答案现已弃用。0.pre。现在,使用 inputFormatters: [FilteringTextInputFormatter.digitsOnly],
代替 (see documentation).
此外,如果您想更好地控制使用 RegEx 输入的内容,请使用 FilteringTextInputFormatter.allow(RegExp(r'your regex here'))
。
我正在使用 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))),
)
但现在我可以在此处显示的同一字段中粘贴一些文本
那么现在我如何格式化我的 TextFormfield 以限制仅键入数字并限制粘贴文本?
你可以尝试使用WhitelistingTextInputFormatter
喜欢
inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
见官方docs
自 Flutter 版本 1.20.0-1 起,已接受的答案现已弃用。0.pre。现在,使用 inputFormatters: [FilteringTextInputFormatter.digitsOnly],
代替 (see documentation).
此外,如果您想更好地控制使用 RegEx 输入的内容,请使用 FilteringTextInputFormatter.allow(RegExp(r'your regex here'))
。