注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 Form、FormField、TextFormField是
TextFormField(
onSaved: (value){
print('$value');
},autovalidate: false,validator: (String value){
return value.length>=6?null:'账号最少6个字符';
},)
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,...
)
var _state = _formKey.currentState;
if(_state.validate()){
_state.save();
}
var _account = '';
var _pwd = '';
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(hintText: '输入账号'),onSaved: (value) {
_name = value;
},validator: (String value) {
return value.length >= 6 ? null : '账号最少6个字符';
},),TextFormField(
decoration: InputDecoration(hintText: '输入密码'),obscureText: true,onSaved: (value) {
_pwd = value;
},RaisedButton(
child: Text('登录'),onPressed: () {
var _state = Form.of(context);
if(_state.validate()){
_state.save();
login(_name,_pwd);
}
},)
],)
Form(
key: _formKey,onWillPop: () async {
return await showDialog<bool>(
context: context,builder: (BuildContext context) {
return AlertDialog(
title: Text('提示'),content: Text('确认退出吗?'),actions: <Widget>[
FlatButton(
child: Text('取消'),onPressed: () {
Navigator.of(context).pop(false);
},FlatButton(
child: Text('确认'),onPressed: () {
Navigator.of(context).pop(true);
},],);
});
},...
)