【发布时间】:2020-06-23 20:34:29
【问题描述】:
我有一个类似这样的验证表单:
class Example extends StatelessWidget{
Widget build(BuildContext context){
... widgets ...
child: Form(
key: _formKey, // type: GlobalKey<FormState>()
... more widgets ...
child: TextFormField(
_helperText = "Some text",
validator: (String text){
return "Error text!";
}
decoration: InputDecoration(
...
_helperText: helperText // I mutate the helperText somewhere else and rebuild the
... // widget tree if something notifies it to do so ...
)
...
MaterialButton(
onPressed: _formKey.currentState.validate(),
)
...
在验证之前 helperText 的文本是“一些文本”,但在我验证它之后,它会得到“错误文本!”并变成红色。到目前为止,一切都很好。我操作 helperText 属性时对 bloc 模式没有任何问题(因此在这种特殊情况下,我使用 StatelessWidget 而不是 StatefulWidget 和 setState() - 但我认为这不会解决问题),但在验证之后(如果这是一个错误),更改 helperText 对 TextFormField 小部件的 helperText 属性没有视觉效果。
注意:如果我使用return null; 而不是return "Error text!";,helperText 将不会处于“错误”状态,因此在这种情况下改变 helperText 属性将正常工作。当返回 null 以外的值时会出现此问题。
那么我可以做些什么来验证表单,即使在那之后 - 尽管这是一个错误 - 我仍然能够编辑 helperText 以及 TextFormField 装饰颜色和 helperText 颜色等?
对不起,这个极简主义的例子,但也许你有一个想法,或者你遇到了类似的问题。
非常感谢。
【问题讨论】:
标签: forms validation flutter