I want to enable a button when the textformfield is not empty and disable a button when the textformfield is empty.
bool isEnabled = false;
TextEditingController _textEditingController = TextEditingController();
RaisedButton(
// disabledColor: Colors.grey,
child: Text("Click"),
onPressed: () {
setState(() {
if (_textEditingController.text.isNotEmpty) {
isEnabled = true;
} else
isEnabled = false;
});
})
Right now, what you are doing is wrong, because you can not enable a disabled button using onPressed
.
It should be done like,
Inside TextFormField
, set value of isEnabled
to true
is input text length is greater than zero, false
otherwise.
TextFormField(
onChanged: (text) {
setState(() {
if(text.length>0)
isEnabled=ture;
else
isEnabled=false;
});
}
and then disable/enable button like this,
onPressed: isEnabled?
(){
//do something
}
: null;// or pass blank (){}