How do I disable a Button in Flutter?

How do I disable a Button in Flutter?



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.


onPressed



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.





Can you clarify what you mean by "This is fine if the button continues to be in the same state for the lifecycle." ?
– Seth Ladd
Mar 18 at 21:21




3 Answers
3



I think you may want to introduce some helper functions to build your button as well as a Stateful widget along with some property to key off of.


build


isButtonDisabled


onPressed


null


onPressed: ()


isButtonDisabled


null


setState(() => isButtonDisabled = true)


build()


null



Here's is some more context using the Flutter counter project.


class MyHomePage extends StatefulWidget
@override
_MyHomePageState createState() => new _MyHomePageState();


class _MyHomePageState extends State<MyHomePage>
int _counter = 0;
bool _isButtonDisabled;

@override
void initState()
_isButtonDisabled = false;


void _incrementCounter()
setState(()
_isButtonDisabled = true;
_counter++;
);


@override
Widget build(BuildContext context)
return new Scaffold(
appBar: new AppBar(
title: new Text("The App"),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
_buildCounterButton(),
],
),
),
);


Widget _buildCounterButton()
return new RaisedButton(
child: new Text(
_isButtonDisabled ? "Hold on..." : "Increment"
),
onPressed: _isButtonDisabled ? null : _incrementCounter,
);




In this example I am using an inline ternary to conditionally set the Text and onPressed, but it may be more appropriate for you to extract this into a function (you can use this same method to change the text of the button as well):


Text


onPressed


Widget _buildCounterButton()
return new RaisedButton(
child: new Text(
_isButtonDisabled ? "Hold on..." : "Increment"
),
onPressed: _counterButtonPress(),
);


Function _counterButtonPress()
if (_isButtonDisabled)
return null;
else
return ()
// do anything else you may want to here
_incrementCounter();
;






This is perfect. I thought about the operator on the onPressed event, but wasn't sure if that was valid. The full sample really helps.
– chris84948
Mar 19 at 21:04





@chris84948 would you like to 'accept' the answer as is or were there other notes we needed to add?
– Ashton Thomas
Mar 21 at 14:22





I think it's good. I accepted the answer a while back.
– chris84948
Mar 22 at 19:03





You need to add fat arrow function as an argument, otherwise the _incrementCounter() function will be called right away when button becomes enabled. This way it will actually wait until the button is clicked: The onPressed should look like this: onPressed: _isButtonDisabled ? null : () => _incrementCounter
– Vit Veres
Apr 12 at 5:21


onPressed: _isButtonDisabled ? null : () => _incrementCounter





@vitVeres that is usually true but the _counterButtonPress() is returning a function return () so this is intentional. I don't want to use the fat arrow here as I want the function to execute and return null and disable the button.
– Ashton Thomas
Apr 14 at 14:34


return ()


null



The simple answer is onPressed : null gives a disabled button.


onPressed : null



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')
);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ