together_mobile/lib/screens/signin_signup/components/signup.dart

247 lines
6.7 KiB
Dart
Raw Normal View History

2023-07-04 11:51:11 +08:00
import 'dart:async';
2023-06-21 17:44:28 +08:00
import 'package:flutter/material.dart';
import 'package:cherry_toast/cherry_toast.dart';
import '../../../components/common_widgets.dart';
2023-06-21 17:44:28 +08:00
import 'package:together_mobile/common/constants.dart';
2023-07-04 11:51:11 +08:00
import 'package:together_mobile/request/signup_signin.dart' as signup_request;
2023-06-21 17:44:28 +08:00
2023-06-29 17:02:09 +08:00
class SignupBody extends StatefulWidget {
const SignupBody({super.key});
2023-06-21 17:44:28 +08:00
@override
2023-06-29 17:02:09 +08:00
State<SignupBody> createState() => _SignupBodyState();
2023-06-21 17:44:28 +08:00
}
2023-06-29 17:02:09 +08:00
class _SignupBodyState extends State<SignupBody> {
2023-06-21 17:44:28 +08:00
bool _isProgressShow = false;
bool _isHttpSend = false;
final TextEditingController usernameController = TextEditingController();
2023-06-29 17:02:09 +08:00
final TextEditingController passwordController = TextEditingController();
2023-07-04 11:51:11 +08:00
final TextEditingController emailController = TextEditingController();
2023-06-29 17:02:09 +08:00
final TextEditingController codeController = TextEditingController();
2023-06-21 17:44:28 +08:00
final Map<String, bool> _isError = {
'username': false,
2023-07-04 11:51:11 +08:00
'password': false,
'email': false,
'code': false,
2023-06-21 17:44:28 +08:00
};
2023-09-10 11:30:20 +08:00
Timer? _timer;
2023-07-04 11:51:11 +08:00
int _count = 60;
bool _isGetCode = false;
2023-06-21 17:44:28 +08:00
void _isInputError(String type, bool error) {
setState(() {
_isError[type] = error;
});
}
2023-07-04 11:51:11 +08:00
@override
void dispose() {
usernameController.dispose();
passwordController.dispose();
2023-09-10 11:30:20 +08:00
_timer?.cancel();
2023-07-04 11:51:11 +08:00
super.dispose();
}
2023-06-21 17:44:28 +08:00
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
2023-06-29 17:02:09 +08:00
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
2023-06-21 17:44:28 +08:00
children: [
Stack(
alignment: Alignment.center,
children: [
const SizedBox(
width: double.infinity,
),
if (_isProgressShow)
const CircularProgressIndicator(
color: kSecondaryColor,
strokeWidth: 6.0,
),
],
),
SizedBox(
width: 250,
child: SignTextField(
color: kSecondaryColor,
labelText: '用户名',
type: 'username',
controller: usernameController,
isError: _isInputError,
),
),
SizedBox(
width: 250,
child: SignTextField(
color: kSecondaryColor,
obscureText: true,
labelText: '密码',
2023-07-04 11:51:11 +08:00
type: 'password',
2023-06-29 17:02:09 +08:00
controller: passwordController,
2023-06-21 17:44:28 +08:00
isError: _isInputError,
),
),
SizedBox(
width: 250,
child: SignTextField(
2023-07-04 11:51:11 +08:00
controller: emailController,
2023-06-21 17:44:28 +08:00
color: kSecondaryColor,
2023-06-29 17:02:09 +08:00
type: 'email',
labelText: '邮箱',
2023-06-21 17:44:28 +08:00
isError: _isInputError,
),
),
2023-06-29 17:02:09 +08:00
SizedBox(
width: 250,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: 120,
child: SignTextField(
controller: codeController,
color: kSecondaryColor,
type: 'code',
labelText: '验证码',
isError: _isInputError,
),
),
TextButton(
2023-07-04 11:51:11 +08:00
onPressed: () async {
await _getCode(context);
},
2023-06-29 17:02:09 +08:00
style: TextButton.styleFrom(
2023-07-04 11:51:11 +08:00
foregroundColor:
_isGetCode ? kUnActivatedColor : kSecondaryColor,
2023-06-29 17:02:09 +08:00
),
2023-07-04 11:51:11 +08:00
child: Text(
_isGetCode ? '已发送($_count)' : '获取验证码',
style: TextStyle(
color: _isGetCode ? kUnActivatedColor : kSecondaryColor,
fontSize: 15.0,
),
2023-06-29 17:02:09 +08:00
),
),
],
),
2023-06-21 17:44:28 +08:00
),
CommonElevatedButton(
2023-06-21 17:44:28 +08:00
onPressed: () {
2023-07-04 11:51:11 +08:00
_confirm(context);
2023-06-21 17:44:28 +08:00
},
text: '确定',
color: kSecondaryColor,
),
],
);
}
2023-07-04 11:51:11 +08:00
Future<void> _getCode(BuildContext context) async {
if (_isGetCode) {
_showWarningToast(context, '验证码还在有效期内');
return;
} else if (_isError['email']!) {
_showWarningToast(context, '邮箱格式不正确');
return;
} else if (emailController.text.isEmpty) {
_showWarningToast(context, '邮箱不能为空');
2023-06-21 17:44:28 +08:00
return;
}
2023-07-04 11:51:11 +08:00
await signup_request.askCode(emailController.text);
2023-06-21 17:44:28 +08:00
setState(() {
2023-07-04 11:51:11 +08:00
_isGetCode = true;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
2023-07-04 11:51:11 +08:00
setState(() {
_count--;
2023-07-04 11:51:11 +08:00
});
if (_count == 0) {
timer.cancel();
setState(() {
_isGetCode = false;
_count = 60;
});
}
});
2023-07-04 11:51:11 +08:00
});
}
void _confirm(BuildContext context) async {
if (_isError['username']! || _isError['password']!) {
_showWarningToast(context, '账号或密码不符合规范');
return;
} else if (_isError['email']!) {
_showWarningToast(context, '邮箱格式不正确');
return;
} else if (_isError['code']!) {
_showWarningToast(context, '验证码格式不正确');
return;
} else if (emailController.text.isEmpty) {
_showWarningToast(context, '邮箱不能为空');
return;
} else if (codeController.text.isEmpty) {
_showWarningToast(context, '验证码不能为空');
return;
} else if (usernameController.text.isEmpty) {
_showWarningToast(context, '用户名不能为空');
return;
} else if (passwordController.text.isEmpty) {
_showWarningToast(context, '密码不能为空');
return;
}
late Map<String, dynamic> res;
2023-07-04 11:51:11 +08:00
setState(() {
if (!_isProgressShow) _isProgressShow = true;
});
2023-06-21 17:44:28 +08:00
if (_isProgressShow && !_isHttpSend) {
_isHttpSend = true;
2023-07-04 11:51:11 +08:00
res = await signup_request.signup(
usernameController.text,
passwordController.text,
emailController.text,
codeController.text,
);
2023-06-21 17:44:28 +08:00
}
2023-07-04 11:51:11 +08:00
2023-06-21 17:44:28 +08:00
setState(() {
2023-07-04 11:51:11 +08:00
_isHttpSend = false;
_isProgressShow = false;
2023-06-21 17:44:28 +08:00
});
2023-07-04 11:51:11 +08:00
if (res['code'] == 10100) {
2023-07-04 11:51:11 +08:00
// ignore: use_build_context_synchronously
CherryToast.success(
title: const Text('注册成功!'),
toastDuration: const Duration(seconds: 2),
animationDuration: const Duration(seconds: 1),
).show(context);
} else if (res['code'] == 10102) {
2023-07-04 11:51:11 +08:00
// ignore: use_build_context_synchronously
_showWarningToast(context, '验证码错误或已过期');
}
2023-06-21 17:44:28 +08:00
}
2023-07-04 11:51:11 +08:00
void _showWarningToast(BuildContext context, String text) {
CherryToast.warning(
title: Text(text),
toastDuration: const Duration(seconds: 2),
animationDuration: const Duration(seconds: 1),
).show(context);
2023-06-21 17:44:28 +08:00
}
}