经研究,当 onPressed 为 null 时,Flutter 的 Button 会自动禁用。但是,由于我需要的测试功能,我不得不放置一个箭头函数 () => ,这似乎不会触发 onPressed 实际上为 null,而是将 null 作为值返回。因此,当 textField 为空时,当前按钮什么都不做(null)。如果 textField 为空,我的目标是完全禁用它(灰色)。
onPressed: () => (_textController.text.isNotEmpty) ? _addNewPair() : null,
showDialog(
context: this.context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Add a custom word'),
content: _renderForm(),
actions: <Widget>[
FlatButton(
child: Text('ADD'),
onPressed: () => (_textController.text.isNotEmpty) ? _addNewPair() : null,
),
],
);
}
最佳答案
先设置条件,如果文本为空,您的按钮将被禁用。
onPressed: (_textController.text.isNotEmpty) ? () => _addNewPair() : null,
关于dart - 如何正确启用/禁用 Flutter 的 Button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53716659/