I'm just starting to get the hang of Flutter, but I'm having trouble figuring out how to set the enabled state of a button.
From the docs, it says to set onPressed
to null to disable a button, and give it a value to enable it. This is fine if the button continues to be in the same state for the lifecycle.
I get the impression I need to create a custom Stateful widget that will allow me to update the button's enabled state (or onPressed callback) somehow.
So my question is how would I do that? This seems like a pretty straightforward requirement, but I can't find anything in the docs on how to do it.
Thanks.
You can also use the AbsorbPointer, and you can use it in the following way:
AbsorbPointer(
absorbing: true, // by default is true
child: RaisedButton(
onPressed: (){
print('pending to implement onPressed function');
},
child: Text("Button Click!!!"),
),
),
If you want to know more about this widget, you can check the following link Flutter Docs
For disabling any Button in flutter such as FlatButton
, RaisedButton
, MaterialButton
, IconButton
etc all you need to do is to set the onPressed
and onLongPress
properties to null. Below is some simple examples for some of the buttons:
FlatButton (Enabled)
FlatButton(
onPressed: (){},
onLongPress: null, // Set one as NOT null is enough to enable the button
textColor: Colors.black,
disabledColor: Colors.orange,
disabledTextColor: Colors.white,
child: Text('Flat Button'),
),
FlatButton (Disabled)
FlatButton(
onPressed: null,
onLongPress: null,
textColor: Colors.black,
disabledColor: Colors.orange,
disabledTextColor: Colors.white,
child: Text('Flat Button'),
),
RaisedButton (Enabled)
RaisedButton(
onPressed: (){},
onLongPress: null, // Set one as NOT null is enough to enable the button
// For when the button is enabled
color: Colors.lightBlueAccent,
textColor: Colors.black,
splashColor: Colors.blue,
elevation: 8.0,
// For when the button is disabled
disabledTextColor: Colors.white,
disabledColor: Colors.orange,
disabledElevation: 0.0,
child: Text('Raised Button'),
),
RaisedButton (Disabled)
RaisedButton(
onPressed: null,
onLongPress: null,
// For when the button is enabled
color: Colors.lightBlueAccent,
textColor: Colors.black,
splashColor: Colors.blue,
elevation: 8.0,
// For when the button is disabled
disabledTextColor: Colors.white,
disabledColor: Colors.orange,
disabledElevation: 0.0,
child: Text('Raised Button'),
),
IconButton (Enabled)
IconButton(
onPressed: () {},
icon: Icon(Icons.card_giftcard_rounded),
color: Colors.lightBlueAccent,
disabledColor: Colors.orange,
),
IconButton (Disabled)
IconButton(
onPressed: null,
icon: Icon(Icons.card_giftcard_rounded),
color: Colors.lightBlueAccent,
disabledColor: Colors.orange,
),
Note: Some of buttons such as IconButton
have only the onPressed
property.
You can set also blank condition, in place of set null
var isDisable=true;
RaisedButton(
padding: const EdgeInsets.all(20),
textColor: Colors.white,
color: Colors.green,
onPressed: isDisable
? () => (){} : myClickingData(),
child: Text('Button'),
)
Enable and Disable functionality is same for most of the widgets.
Ex, button , switch, checkbox etc.
Just set the onPressed
property as shown below
onPressed : null
returns Disabled widget
onPressed : (){}
or onPressed : _functionName
returns Enabled widget
The simple answer is onPressed : null
gives a disabled button.
According to the docs:
"If the onPressed callback is null, then the button will be disabled and by default will resemble a flat button in the disabledColor."
https://docs.flutter.io/flutter/material/RaisedButton-class.html
So, you might do something like this:
RaisedButton(
onPressed: calculateWhetherDisabledReturnsBool() ? null : () => whatToDoOnPressed,
child: Text('Button text')
);
Setting
onPressed: null // disables click
and
onPressed: () => yourFunction() // enables click
For a specific and limited number of widgets, wrapping them in a widget IgnorePointer does exactly this: when its ignoring
property is set to true, the sub-widget (actually, the entire subtree) is not clickable.
IgnorePointer(
ignoring: true, // or false
child: RaisedButton(
onPressed: _logInWithFacebook,
child: Text("Facebook sign-in"),
),
),
Otherwise, if you intend to disable an entire subtree, look into AbsorbPointer().
This is the easiest way in my opinion:
RaisedButton(
child: Text("PRESS BUTTON"),
onPressed: booleanCondition
? () => myTapCallback()
: null
)
I like to use flutter_mobx for this and work on the state.
Next I use an observer:
Container(child: Observer(builder: (_) {
var method;
if (!controller.isDisabledButton) method = controller.methodController;
return RaiseButton(child: Text('Test') onPressed: method);
}));
On the Controller:
@observable
bool isDisabledButton = true;
Then inside the control you can manipulate this variable as you want.
Refs.: Flutter mobx
Source: Stackoverflow.com