Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly enable/disable Flutter's Button

Tags:

flutter

dart

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,
        ),
      ],
    );
  }
like image 398
Jesse Avatar asked Dec 11 '18 02:12

Jesse


1 Answers

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

onPressed: (_textController.text.isNotEmpty) ? () =>  _addNewPair() : null,
like image 191
diegoveloper Avatar answered Oct 14 '22 01:10

diegoveloper