This commit is contained in:
lz_db
2026-10-17 09:41:11 +08:00
parent c519bdae9f
commit 28b11b4365
7 changed files with 126 additions and 28 deletions
+101 -19
View File
@@ -10,7 +10,7 @@ import '../../core/theme/app_theme.dart';
import '../../providers/auth_provider.dart';
import '../../services/auth_service.dart';
/// 注册页:手机号 + 验证码 + 密码;邀请码选填(口令/模糊归因命中时自动预填)
/// 注册页:手机号/邮箱 + 验证码 + 密码;邀请码选填(口令/模糊归因命中时自动预填)
class RegisterScreen extends ConsumerStatefulWidget {
const RegisterScreen({super.key});
@@ -19,11 +19,16 @@ class RegisterScreen extends ConsumerStatefulWidget {
}
class _RegisterScreenState extends ConsumerState<RegisterScreen> {
final _phone = TextEditingController();
final _account = TextEditingController();
final _code = TextEditingController();
final _password = TextEditingController();
final _nickname = TextEditingController();
late final TextEditingController _inviteCode;
/// 注册方式:phone / email
String _mode = 'phone';
bool get _isEmail => _mode == 'email';
bool _loading = false;
String? _error;
@@ -31,6 +36,9 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
int _countdown = 0;
Timer? _timer;
/// 邮箱格式校验
static final _emailReg = RegExp(r'^[\w.+-]+@[\w-]+\.[\w.-]+$');
@override
void initState() {
super.initState();
@@ -41,7 +49,7 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
@override
void dispose() {
_timer?.cancel();
_phone.dispose();
_account.dispose();
_code.dispose();
_password.dispose();
_nickname.dispose();
@@ -49,20 +57,41 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
super.dispose();
}
void _switchMode(String mode) {
if (mode == _mode) return;
setState(() {
_mode = mode;
_error = null;
// 切换方式后账号和验证码需要重新填写
_account.clear();
_code.clear();
});
}
Future<void> _sendCode() async {
final phone = _phone.text.trim();
if (!RegExp(r'^1\d{10}$').hasMatch(phone)) {
setState(() => _error = '请输入正确的手机号');
return;
final account = _account.text.trim();
if (_isEmail) {
if (!_emailReg.hasMatch(account)) {
setState(() => _error = '请输入正确的邮箱地址');
return;
}
} else {
if (!RegExp(r'^1\d{10}$').hasMatch(account)) {
setState(() => _error = '请输入正确的手机号');
return;
}
}
try {
final devCode = await AuthService.sendSmsCode(phone, 'register');
// 未配置短信/邮件通道时服务端走开发模式,直接返回验证码
final devCode = _isEmail
? await AuthService.sendEmailCode(account, 'register')
: await AuthService.sendSmsCode(account, 'register');
setState(() {
_error = null;
_countdown = 60;
});
if (devCode != null && mounted) {
// 短信通道未配置时的开发模式:直接展示验证码(上线配置短信后服务端不再返回)
// 通道未配置时的开发模式:直接展示验证码(上线配置后服务端不再返回)
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('开发模式验证码:$devCode(5 分钟内有效)'), duration: const Duration(seconds: 6)),
);
@@ -84,11 +113,11 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
}
Future<void> _submit() async {
final phone = _phone.text.trim();
final account = _account.text.trim();
final code = _code.text.trim();
final password = _password.text;
if (phone.isEmpty || code.isEmpty || password.isEmpty) {
setState(() => _error = '请填写手机号、验证码和密码');
if (account.isEmpty || code.isEmpty || password.isEmpty) {
setState(() => _error = _isEmail ? '请填写邮箱、验证码和密码' : '请填写手机号、验证码和密码');
return;
}
setState(() {
@@ -97,8 +126,8 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
});
try {
final result = await AuthService.register(
type: 'phone',
account: phone,
type: _mode,
account: account,
code: code,
password: password,
nickname: _nickname.text.trim(),
@@ -132,11 +161,14 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
padding: const EdgeInsets.all(24),
children: [
const Text('创建账号', style: TextStyle(fontSize: 26, fontWeight: FontWeight.w700)),
const SizedBox(height: 32),
const SizedBox(height: 24),
// 手机号 / 邮箱 切换
_ModeSwitch(current: _mode, onChanged: _switchMode),
const SizedBox(height: 24),
TextField(
controller: _phone,
keyboardType: TextInputType.phone,
decoration: const InputDecoration(hintText: '手机号'),
controller: _account,
keyboardType: _isEmail ? TextInputType.emailAddress : TextInputType.phone,
decoration: InputDecoration(hintText: _isEmail ? '邮箱' : '手机号'),
),
const SizedBox(height: 16),
Row(
@@ -145,7 +177,7 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
child: TextField(
controller: _code,
keyboardType: TextInputType.number,
decoration: const InputDecoration(hintText: '短信验证码'),
decoration: InputDecoration(hintText: _isEmail ? '邮箱验证码' : '短信验证码'),
),
),
const SizedBox(width: 12),
@@ -201,3 +233,53 @@ class _RegisterScreenState extends ConsumerState<RegisterScreen> {
);
}
}
/// 注册方式切换条(手机号 / 邮箱)
class _ModeSwitch extends StatelessWidget {
const _ModeSwitch({required this.current, required this.onChanged});
final String current;
final ValueChanged<String> onChanged;
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: AppColors.surface,
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.all(3),
child: Row(
children: [
_tab(context, '手机号', 'phone'),
_tab(context, '邮箱', 'email'),
],
),
);
}
Widget _tab(BuildContext context, String label, String value) {
final selected = current == value;
return Expanded(
child: GestureDetector(
onTap: () => onChanged(value),
child: Container(
padding: const EdgeInsets.symmetric(vertical: 9),
alignment: Alignment.center,
decoration: BoxDecoration(
color: selected ? Theme.of(context).scaffoldBackgroundColor : Colors.transparent,
borderRadius: BorderRadius.circular(8),
),
child: Text(
label,
style: TextStyle(
fontSize: 14,
fontWeight: selected ? FontWeight.w600 : FontWeight.w400,
color: selected ? AppColors.textPrimary : AppColors.textSecondary,
),
),
),
),
);
}
}
@@ -18,9 +18,9 @@ class SettingsScreen extends ConsumerWidget {
body: ListView(
children: [
// 版本信息
const ListTile(
leading: Icon(Icons.info_outline, color: AppColors.textSecondary),
title: Text('当前版本'),
ListTile(
leading: const Icon(Icons.info_outline, color: AppColors.textSecondary),
title: const Text('当前版本'),
trailing: Text(
'v${AppConfig.appVersion} (${AppConfig.appVersionCode})',
style: TextStyle(color: AppColors.textWeak, fontSize: 13),