Commit 713d82dd authored by laishanqi's avatar laishanqi

注册页输入框换成TextFormField

parent ec19dac5
import 'package:flutter/material.dart';
class DoctorHomePage extends StatefulWidget {
@override
_DoctorHomePageState createState() => _DoctorHomePageState();
}
class _DoctorHomePageState extends State<DoctorHomePage>{
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
...@@ -18,12 +18,13 @@ class _RegisterState extends State { ...@@ -18,12 +18,13 @@ class _RegisterState extends State {
final passwordController = TextEditingController(); final passwordController = TextEditingController();
var isShowPassword = false; var isShowPassword = false;
var isShowClean = false; var isShowClean = false;
final GlobalKey<FormState> _formKey = GlobalKey();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
nameController.addListener(() { /*nameController.addListener(() {
setState(() {}); setState(() {});
}); });
phoneController.addListener(() { phoneController.addListener(() {
...@@ -31,7 +32,7 @@ class _RegisterState extends State { ...@@ -31,7 +32,7 @@ class _RegisterState extends State {
}); });
codeController.addListener(() { codeController.addListener(() {
setState(() {}); setState(() {});
}); });*/
passwordController.addListener(() { passwordController.addListener(() {
print("密码 ${passwordController.text}"); print("密码 ${passwordController.text}");
setState(() { setState(() {
...@@ -70,13 +71,20 @@ class _RegisterState extends State { ...@@ -70,13 +71,20 @@ class _RegisterState extends State {
), ),
body: Container( body: Container(
margin: EdgeInsets.fromLTRB(16, 0, 16, 0), margin: EdgeInsets.fromLTRB(16, 0, 16, 0),
child: Form(
key:_formKey,
child: ListView( child: ListView(
children: [ children: [
_getTextField(TextInputType.text,"请输入姓名", nameController, 10, false), _getTextField(
_getTextField(TextInputType.number,"请输入手机号码", phoneController, 11, false), TextInputType.text, "请输入姓名", nameController, 10, false),
_getTextField(
TextInputType.number, "请输入手机号码", phoneController, 11,
false),
Stack( Stack(
children: [ children: [
_getTextField(TextInputType.number,"请输入验证码", codeController, 8, false), _getTextField(
TextInputType.number, "请输入验证码", codeController, 8,
false),
Positioned( Positioned(
child: Text("获取验证码"), child: Text("获取验证码"),
right: 16, right: 16,
...@@ -107,15 +115,17 @@ class _RegisterState extends State { ...@@ -107,15 +115,17 @@ class _RegisterState extends State {
"注册", "注册",
style: TextStyle(color: Colors.white), style: TextStyle(color: Colors.white),
), ),
onPressed: checkIsAllEmpty() ? null : onRegisterClick), onPressed: onRegisterClick),
) )
], ],
)), ))),
); );
} }
onRegisterClick() { onRegisterClick() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("注册"))); if(_formKey.currentState.validate()){
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("请求注册")));
}
} }
/// 校验是否全部为空 /// 校验是否全部为空
...@@ -130,8 +140,8 @@ class _RegisterState extends State { ...@@ -130,8 +140,8 @@ class _RegisterState extends State {
} }
Widget _getPassWordField() { Widget _getPassWordField() {
TextField password = TextFormField password = _getTextField(TextInputType.number, "请输入9-16位密码",
_getTextField(TextInputType.number,"请输入9-16位密码", passwordController, 16, isShowPassword); passwordController, 16, isShowPassword);
return Stack( return Stack(
children: [ children: [
password, password,
...@@ -159,7 +169,8 @@ class _RegisterState extends State { ...@@ -159,7 +169,8 @@ class _RegisterState extends State {
: Icon(Icons.visibility), : Icon(Icons.visibility),
onPressed: () { onPressed: () {
setState(() { setState(() {
isShowPassword = !password.obscureText; isShowPassword = !isShowPassword;
// isShowPassword = !password.obscureText;
}); });
}, },
))), ))),
...@@ -168,25 +179,33 @@ class _RegisterState extends State { ...@@ -168,25 +179,33 @@ class _RegisterState extends State {
} }
/// 输入框 /// 输入框
Widget _getTextField(TextInputType keyboardType,String hintText, TextEditingController controller, Widget _getTextField(TextInputType keyboardType, String hintText,
int maxLength, bool isPassword) { TextEditingController controller, int maxLength, bool isPassword) {
return TextField( return TextFormField(
validator: (value) {
if(value.isEmpty){
return "${hintText}不能为空";
}else{
return null;
}
},
keyboardType: keyboardType, keyboardType: keyboardType,
obscureText: isPassword, obscureText: isPassword,
buildCounter: (context, {currentLength, isFocused, maxLength}) => null, buildCounter: (context, {currentLength, isFocused, maxLength}) => null,
maxLength: maxLength, maxLength: maxLength,
autofocus: false, // 自动获取焦点 autofocus: false,
// 自动获取焦点
decoration: InputDecoration( decoration: InputDecoration(
labelStyle: TextStyle(height: 0.5), labelStyle: TextStyle(height: 0.5),
//装饰 //装饰
alignLabelWithHint: true, alignLabelWithHint: true,
contentPadding: EdgeInsets.only(top: 20, bottom: 10), contentPadding: EdgeInsets.only(top: 20, bottom: 10),
enabledBorder: UnderlineInputBorder( enabledBorder: UnderlineInputBorder(
// 默认输入状态的下划线边框 // 默认状态的下划线边框
borderSide: borderSide:
BorderSide(color: Color(AppColors.dividerColor), width: 0.5)), BorderSide(color: Color(AppColors.dividerColor), width: 0.5)),
focusedBorder: UnderlineInputBorder( focusedBorder: UnderlineInputBorder(
// 获取焦点后 // 输入框获取焦点后
borderSide: borderSide:
BorderSide(color: Color(AppColors.mainColor), width: 0.5)), BorderSide(color: Color(AppColors.mainColor), width: 0.5)),
errorBorder: UnderlineInputBorder( errorBorder: UnderlineInputBorder(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment