Upon research, Flutter's Button is disabled automatically when the onPressed is null. However due to my necessary testing function I am forced to place an arrow function () => , which doesn't seem to trigger the onPressed as actually null, but returning null as value. Therefore currently the button just does nothing (null) when textField is empty. I am aiming to disable it fully (grayed out) if the textField is empty.

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:()=&gt;(_textController.text.isNotEmpty)?_addNewPair():空,
        ),
      ],
    );
  }

推荐答案

将条件放在第一位,如果文本为空,您的按钮将被禁用.

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

Dart相关问答推荐

dart 日期时差(分钟)

Dart:判断泛型函数的类型 T

为什么使用三元条件运算符会产生return_of_invalid_type错误?

Flutter 是否提供即时应用程序?

Flutter:将照片或图像显示为模态弹出窗口

如何使用Flutter dio 下载文件?

如何防止键盘在Flutter中按提交键时失效?

为什么使用Dart作为前端开发人员?

如何在命令行上创建和运行Dart元素

设置Dart中非常量的默认值

无法使用 Flutter 调用 Localhost,将随机端口分配给 HTTP GET 调用

如何仅从 DateTime.now() 中提取时间;

对该常量表达式的求值会抛出一个表达式

基于镜子的反射和传统反射有什么区别?

将函数/方法分配给 Dart 中的变量

Dart:你如何让 Future 等待 Stream?

Dart如何将逗号添加到字符串编号

在 Dart 中,bool是否有parse,就像int一样?

Angular.js 和 Angular.dart 的区别?

在 Dart 中验证邮箱地址?