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

247 lines
6.7 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:cherry_toast/cherry_toast.dart';
import '../../../components/common_widgets.dart';
import 'package:together_mobile/common/constants.dart';
import 'package:together_mobile/request/signup_signin.dart' as signup_request;
class SignupBody extends StatefulWidget {
const SignupBody({super.key});
@override
State<SignupBody> createState() => _SignupBodyState();
}
class _SignupBodyState extends State<SignupBody> {
bool _isProgressShow = false;
bool _isHttpSend = false;
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController emailController = TextEditingController();
final TextEditingController codeController = TextEditingController();
final Map<String, bool> _isError = {
'username': false,
'password': false,
'email': false,
'code': false,
};
Timer? _timer;
int _count = 60;
bool _isGetCode = false;
void _isInputError(String type, bool error) {
setState(() {
_isError[type] = error;
});
}
@override
void dispose() {
usernameController.dispose();
passwordController.dispose();
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
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: '密码',
type: 'password',
controller: passwordController,
isError: _isInputError,
),
),
SizedBox(
width: 250,
child: SignTextField(
controller: emailController,
color: kSecondaryColor,
type: 'email',
labelText: '邮箱',
isError: _isInputError,
),
),
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(
onPressed: () async {
await _getCode(context);
},
style: TextButton.styleFrom(
foregroundColor:
_isGetCode ? kUnActivatedColor : kSecondaryColor,
),
child: Text(
_isGetCode ? '已发送($_count)' : '获取验证码',
style: TextStyle(
color: _isGetCode ? kUnActivatedColor : kSecondaryColor,
fontSize: 15.0,
),
),
),
],
),
),
CommonElevatedButton(
onPressed: () {
_confirm(context);
},
text: '确定',
color: kSecondaryColor,
),
],
);
}
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, '邮箱不能为空');
return;
}
await signup_request.askCode(emailController.text);
setState(() {
_isGetCode = true;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
setState(() {
_count--;
});
if (_count == 0) {
timer.cancel();
setState(() {
_isGetCode = false;
_count = 60;
});
}
});
});
}
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;
setState(() {
if (!_isProgressShow) _isProgressShow = true;
});
if (_isProgressShow && !_isHttpSend) {
_isHttpSend = true;
res = await signup_request.signup(
usernameController.text,
passwordController.text,
emailController.text,
codeController.text,
);
}
setState(() {
_isHttpSend = false;
_isProgressShow = false;
});
if (res['code'] == 10100) {
// 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) {
// ignore: use_build_context_synchronously
_showWarningToast(context, '验证码错误或已过期');
}
}
void _showWarningToast(BuildContext context, String text) {
CherryToast.warning(
title: Text(text),
toastDuration: const Duration(seconds: 2),
animationDuration: const Duration(seconds: 1),
).show(context);
}
}