如何限制文本字段中的值颤音中的值?
我想将用户输入的值限制为flutter文本字段中的预期值。
例子, 如果来自API的值是10,并且想要限制输入不超过10,如果客户端类型11或10以上的内容,则希望显示警报或使用户不输入。 如何控制这个?
TextFormField(
style: TextStyle(fontSize: 20),
onChanged: (value) {
if (value != "") {
int _checkValue = int.parse(value);
if (_checkValue >
Provider.of<SaleProvider>(context, listen: false)
.remainNewQuantity(
this.currentProductItemSelected.id)) {
return 'error';
} else {
setState(() {
this.qty = int.parse(value);
updateByQty();
});
}
} else {
setState(() {
});
}
},
),
这是我的尝试,但不能做我想要的。
I want to limit the value that user input not to over a expected value in flutter text field.
Example,
If there is a value come from API is 10 and want to limit the input not over 10, if client type 11 or something over 10, want to show alert or make user not to type.
How to control this?
TextFormField(
style: TextStyle(fontSize: 20),
onChanged: (value) {
if (value != "") {
int _checkValue = int.parse(value);
if (_checkValue >
Provider.of<SaleProvider>(context, listen: false)
.remainNewQuantity(
this.currentProductItemSelected.id)) {
return 'error';
} else {
setState(() {
this.qty = int.parse(value);
updateByQty();
});
}
} else {
setState(() {
});
}
},
),
This is my trying, but can't do that I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请检查下面的方法。我认为这将解决您的问题。如果仍然不起作用,请告诉我
Please check below method. I think this will resolve your issue. If still not work, please let me know
作为我自己学习的一种练习,我给了它,并提出了以下方法。创建一个定制的小部件,它看起来像是如此简单的东西,看起来像是很多代码……但是它似乎按照预期的方式工作,并且可以以各种方式修改,扩展和集成与其他元素。
用法:
const Maxintfield(最大:100),
实现:
As an exercise for my own learning I gave it a go and came up with the following approach; creating a bespoke widget which admittedly looks like a lot of code for something so simple... but it appears to work as expected I think and one could modify, expand and integrate it with other elements in various ways.
Usage:
const MaxIntField(max: 100),
Implementation: