Flutter实战一Flutter聊天应用(十四)
优化输入体验在进行下一步之前,我们先优化一下注册的体验:
首先我们把 class SignUpState extends State<SignUp> {
bool _correctPhone = true;
bool _correctUsername = true;
bool _correctPassword = true;
//...
void _checkInput() {
if (_phoneController.text.isNotEmpty &&
(_phoneController.text.trim().length < 7 ||
_phoneController.text.trim().length > 12)) {
_correctPhone = false;
} else {
_correctPhone = true;
}
if (_usernameController.text.isNotEmpty &&
_usernameController.text.trim().length < 2) {
_correctUsername = false;
} else {
_correctUsername = true;
}
if (_passwordController.text.isNotEmpty &&
_passwordController.text.trim().length < 6) {
_correctPassword = false;
} else {
_correctPassword = true;
}
setState(() {});
}
//...
}
因为我们将使用手机号作为用户的唯一ID存储在数据库中,所以在上面的代码中,我们增加了一个_correctPhone变量与判断手机号长度的逻辑。 在 class SignUpState extends State<SignUp> {
//...
new TextField(
controller: _phoneController,keyboardType: TextInputType.phone,decoration: new InputDecoration(
helperText: 'Your unique ID.',hintText: 'Phone',errorText: _correctPhone
? null
: 'phone length is 7 to 12 bits.',icon: new Icon(
Icons.phone,),onSubmitted: (value) {
_checkInput();
},new TextField(
controller: _usernameController,decoration: new InputDecoration(
helperText: "What's your name?",hintText: 'Username',errorText: _correctUsername
? null
: 'Username length is less than 2 bits.',icon: new Icon(
Icons.account_circle,new TextField(
controller: _passwordController,obscureText: true,keyboardType: TextInputType.number,decoration: new InputDecoration(
helperText: 'Your landing password.',hintText: 'Password',errorText: _correctPassword
? null
: 'Password length is less than 6 bits.',icon: new Icon(
Icons.lock_outline,//...
}
然后我们需要在把有背景图片的 class SignUpState extends State<SignUp> {
//...
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Stack(children: <Widget>[
new Opacity(
opacity: 0.3,child: new GestureDetector(
onTap: () {
FocusScope.of(context).requestFocus(new FocusNode());
_checkInput();
},child: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
image: new ExactAssetImage('images/sign_up_background.jpg'),fit: BoxFit.cover,))),//...
}
//...
}
最后我们在用户点击加入按钮提交注册信息之前,先清除文本输入框的焦点并判断输入内容。 class SignUpState extends State<SignUp> {
//...
Future _handleSubmitted() async {
FocusScope.of(context).requestFocus(new FocusNode());
_checkInput();
//...
}
//...
}
添加等待动画用户在提交注册请求后,需要等待服务端返回请求结果,这一等待时间会根据当前网络的因素或长或短。所以我们现在要增加一个等待动画,使用 class ShowAwait extends StatefulWidget {
ShowAwait(this.requestCallback);
final Future<int> requestCallback;
@override
_ShowAwaitState createState() => new _ShowAwaitState();
}
class _ShowAwaitState extends State<ShowAwait> {
@override
initState() {
super.initState();
new Timer(new Duration(seconds: 2),() {
widget.requestCallback.then((int onValue) {
Navigator.of(context).pop(onValue);
});
});
}
@override
Widget build(BuildContext context) {
return new Center(
child: new CircularProgressIndicator(),);
}
}
上面代码中,在 提交注册信息然后回到 class SignUpState extends State<SignUp> {
//...
final reference = FirebaseDatabase.instance.reference().child('users');
//...
}
现在我们修改一下 class SignUpState extends State<SignUp> {
//...
Future<int> _userLogUp(String username,String password,{String email,String phone}) async {
return await reference
.child(_phoneController.text)
.once()
.then((DataSnapshot onValue) {
if (onValue.value == null) {
reference.child(phone).set({
'name': username,'password': password,'email': email,'phone': phone,});
return 1;
} else {
return 0;
}
});
}
//...
}
再修改一下 class SignUpState extends State<SignUp> {
//...
void _handleSubmitted() {
FocusScope.of(context).requestFocus(new FocusNode());
_checkInput();
if (_usernameController.text == '' || _passwordController.text == '') {
showMessage(context,"The registration information is incomplete!");
return;
} else if (!_correctUsername || !_correctPassword || !_correctPhone) {
showMessage(context,"The registration information is incomplete!");
return;
}
showDialog<int>(
context: context,barrierDismissible: false,child: new ShowAwait(_userLogUp(
_usernameController.text,_passwordController.text,email: _emailController.text,phone: _phoneController.text))).then((int onValue) {
if (onValue == 0) {
showMessage(context,"This number has been registered!");
} else if (onValue == 1) {
Navigator
.of(context)
.pop([_phoneController.text,_passwordController.text]);
}
});
}
//...
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |