1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| //变量定义 var userNameController = new TextEditingController(); var passWordController = new TextEditingController(); var _userNameFieldValue = ''; var _passWordFieldValue = ''; var _name = ''; var _pwd = ''; final _formKey = GlobalKey<FormState>(); //body部分 body: 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); }, ), ], ); }); }, //autovalidateMode:AutovalidateMode.disabled , child: Column( children: [ Text( "用户名", style: TextStyle( color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold), ), TextFormField( controller: userNameController, decoration: InputDecoration( icon: Icon(Icons.person), suffixIcon: Icon(Icons.delete), labelText: "请输入用户名", counterText: "${_userNameFieldValue.length}/32"), autovalidateMode: AutovalidateMode.disabled, validator: (userName) { if (userName.length < 3) { return "用户名过短"; } return null; }, onChanged: (value) { setState(() { _userNameFieldValue = value; }); }, onSaved: (value) { _name = value; }, keyboardType: TextInputType.number, textCapitalization: TextCapitalization.words, textInputAction: TextInputAction.search, ), Text("密码", style: TextStyle( color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold)), TextFormField( controller: passWordController, decoration: InputDecoration( icon: Icon(Icons.lock), suffixIcon: Icon(Icons.delete), labelText: "请输入密码", counterText: "${_passWordFieldValue.length}/32", ), autovalidateMode: AutovalidateMode.disabled, obscureText: true, validator: (passWord) { if (passWord.length < 3) { return "密码过短"; } return null; }, onChanged: (value) { setState(() { _passWordFieldValue = value; }); }, onSaved: (value) { _pwd = value; }, ), RaisedButton( onPressed: () { var _state = _formKey.currentState; if (_state.validate()) { _state.save(); login(_name, _pwd); } //login(_userNameFieldValue, _passWordFieldValue); }, child: Text("登录"), ) ], ), onChanged: () { //print("用户名:${userNameController.text}"); //print("密码:${passWordController.text}"); }, )); //login方法 void login(String name, String pwd) { print("用户名:$name,密码:$_pwd"); userNameController.clear(); passWordController.clear(); }
|