In My First Flutter Project, I am trying to get value from TextFormField inside a Alert Dialog. I am trying to validate inputs and based on that validation I want to enable/disable button of AlertDialog.
What I have done to display dialog:
_displayDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('TextField AlertDemo'),
content: _container(),
actions: <Widget>[
RaisedButton(
onPressed: isValid
? () {
print("ISVALID:");
}
: null,
child: Text("Click Me"),
)
],
);
});
}
_container() method code:
Widget _container() {
return Container(
margin: EdgeInsets.all(25),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
onChanged: (text) {
setState(() {
if (text.length > 5) {
isValid = true;
} else {
isValid = false;
}
});
},
decoration: InputDecoration(labelText: 'Enter Text'),
),
SizedBox(height: 15),
RaisedButton(
onPressed: isValid
? () {
print("ISVALID:");
}
: null,
child: Text("Done!"),
)
],
),
),
);
}
Unfortunately It is working in body of Scaffold:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_container(),
RaisedButton(
onPressed: (){_displayDialog(context);},
child: Text("Show Alert"),
)
],
),
);
}
Can anyone please help me out in this problem. TIA