How to properly enable/disable Flutter's Button

◇◆丶佛笑我妖孽 提交于 2020-12-05 04:59:09

问题


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

回答1:


Put the condition first, if text is empty your button will be disabled.

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


来源:https://stackoverflow.com/questions/53716659/how-to-properly-enable-disable-flutters-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!