如何正确启用/禁用Flutter的Button

2024-03-16

经研究,当 onPressed 为 null 时,Flutter 的 Button 会自动禁用。然而,由于我必要的测试功能,我被迫放置一个箭头函数 () => ,它似乎不会触发 onPressed 实际上为 null,而是返回 null 作为值。因此,当前当 textField 为空时,按钮不执行任何操作(null)。如果文本字段为空,我的目标是完全禁用它(灰显)。

onPressed: () => (_textController.text.isNotEmpty) ? _addNewPair() :空,

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,
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何正确启用/禁用Flutter的Button 的相关文章