我有一个文本字段,它在onChange中将一个值变量更新为双精度格式...我希望TextField只显示更新后的value变量。我不希望文本字段显示任何字符串值或不是双精度格式的值,因为按下了错误的键盘键。即使数字键盘启用了小数,我们也可以多次按十进制键,我不想在TextField中显示。
TextFormField(
decoration: InputDecoration(labelText: 'Value'),
onChanged: (s) {
if (double.tryParse(s) != null)
setState(() {
value = double.parse(s);
});
},
)如何在TextField中仅显示已解析的值?
发布于 2020-04-15 03:23:40
@user8773560是正确的,但是十进制数的RegExp是错误的,不起作用,所以正确的答案是
inputFormatters: [
WhitelistingTextInputFormatter(RegExp(r'(^\d*\.?\d*)'))
]在我的另一个问题Regex for double numbers in dart中也回答了
发布于 2020-12-07 19:22:48
请将以下属性添加到TextFormField小部件
TextFormField(
keyboardType:TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}'))
],
);发布于 2020-04-15 00:33:39
您好,您不能为此使用onChange,因为它只调用一次TextField已更改。
你应该试试这个。
TextFormField(
inputFormatters: <TextInputFormatter>[
LengthLimitingTextInputFormatter(12), //max length of 12 characters
WhitelistingTextInputFormatter.digitsOnly,//Only numbers
BlacklistingTextInputFormatter.singleLineFormatter,//No line break
WhitelistingTextInputFormatter(RegExp("[0-9]+.[0-9]")) //only double values
],
)发布更多here或Official doc
https://stackoverflow.com/questions/61212332
复制相似问题