Search code examples
dartflutterflutter-navigation

how to disable a button after click first time


how to disable a button after click first time?

enter image description here


Solution

  • To disable a button, you have to set its onPressed method to null. You can use a ternary operator for this. So what I'm doing here is basically, giving a function to onPressed method conditionally. If _isButtonDisabled is false, onPressed method gets null which makes it disabled, otherwise it gets the function.

    onPressed: _isButtonDisabled ? null : _functionThatDoesSomething,
    

    You can put your _isButtonDisabled variable to your state, and set it to false in your _functionThatDoesSomething body. Probably at the end of the function.