在我的第一个Flutter 项目中,我试图从Alert Dialog中的TextFormField获取价值。我正在尝试验证输入并基于该验证我想启用/禁用AlertDialog按钮。
我为显示对话框所做的工作:
_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() 方法代码:
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!"),
)
],
),
),
);
}
不幸的是,它正在脚手架的主体中工作:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_container(),
RaisedButton(
onPressed: (){_displayDialog(context);},
child: Text("Show Alert"),
)
],
),
);
}
谁能帮我解决这个问题。TIA