【发布时间】:2018-12-26 20:21:33
【问题描述】:
我目前在 SimpleDialog 中嵌入了一个 TextField,用于输入信息。输入的信息需要在使用前进行验证。如果此验证失败,我想使用 TextField 的 ErrorText 字段显示错误消息。当用户按下“保存”按钮时会发生此验证。
失败时,我将文本的值从 null 更新为“Error!”。我知道这是正确完成的,因为如果我退出 SimpleDialog 然后回到对话框,文本就会更新。显然这是状态问题。
这是保存按钮的 OnPressed 方法的代码:
onPressed: () {
if (!checkDevEUICorrectness()) {
setState(() {
devEUIErrorText = "Error!";
});
}
else {
setState((){
devEUIErrorText = null;
});
}
},
这里是 TextField 的简短代码:
new TextField(
controller: devEUIController,
decoration: new InputDecoration(
errorText: devEUIErrorText,
// This is the save button whose code is above
icon: new IconButton(...implementation...),
),
),
我应该如何正确更新文本的状态?与我在网上查看的许多指南相比,我看不出我正在做的事情有什么不同。
【问题讨论】: