Flutter disable and enable button programmatically

You can make button disable by setting onPressed property null. If you set onPressed to null Flutter will added disabled related characteristic to button automatically.

I have created a switch with a button and when you turn on switch the value of onPressed set to null.

    

    Column(
          children: [
                        Switch(
                            value: isDisabled,
                            onChanged: (check) {
                              setState(() {
                                isDisabled = check;
                              });
                            }),
                        RaisedButton(
                          onPressed: isDisabled
                              ? null
                              : () {
                                  print("Clicked");
                                },
                          child: Text("Click Me"),
                        ),
                 ],
        )
    
flutter disable button

Change the disabled button colour

disabledColor property allow to set a default disable color of the button.
    

    Column(
          children: [
                        Switch(
                            value: isDisabled,
                            onChanged: (check) {
                              setState(() {
                                isDisabled = check;
                              });
                            }),
                        RaisedButton(
                          disabledColor: Colors.black,
                          onPressed: isDisabled
                              ? null
                              : () {
                                  print("Clicked");
                                },
                          child: Text("Click Me"),
                        ),
                 ],
        )
    

Change the disabled text colour

disabledTextColor property allow to set a default disable colour of the button.
    

    Column(
          children: [
                        Switch(
                            value: isDisabled,
                            onChanged: (check) {
                              setState(() {
                                isDisabled = check;
                              });
                            }),
                        RaisedButton(
                          disabledColor: Colors.black,
                          disabledTextColor: Colors.white,
                          onPressed: isDisabled
                              ? null
                              : () {
                                  print("Clicked");
                                },
                          child: Text("Click Me"),
                        ),
                 ],
        )
    
flutter disable button color
You Will Like Also