import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../../providers/auth_provider.dart'; import '../../../core/constants/route_constants.dart'; import '../../widgets/common/app_button.dart'; import '../../widgets/common/app_text_field.dart'; import '../../widgets/common/loading_indicator.dart'; class RegisterScreen extends StatefulWidget { const RegisterScreen({super.key}); @override State createState() => _RegisterScreenState(); } class _RegisterScreenState extends State { final _formKey = GlobalKey(); final _nameController = TextEditingController(); final _emailController = TextEditingController(); final _phoneController = TextEditingController(); final _passwordController = TextEditingController(); final _confirmPasswordController = TextEditingController(); @override void dispose() { _nameController.dispose(); _emailController.dispose(); _phoneController.dispose(); _passwordController.dispose(); _confirmPasswordController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final authProvider = Provider.of(context); return Scaffold( appBar: AppBar( title: const Text('注册'), ), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(24.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const SizedBox(height: 20), const Text( '创建账号', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 8), const Text( '请填写以下信息完成注册', style: TextStyle( fontSize: 14, color: Colors.grey, ), ), const SizedBox(height: 30), AppTextField( controller: _nameController, labelText: '姓名', hintText: '请输入您的姓名', prefixIcon: const Icon(Icons.person_outline), validator: (value) { if (value == null || value.isEmpty) { return '请输入姓名'; } return null; }, ), const SizedBox(height: 20), AppTextField( controller: _emailController, labelText: '邮箱地址', hintText: '请输入邮箱', keyboardType: TextInputType.emailAddress, prefixIcon: const Icon(Icons.email_outlined), validator: (value) { if (value == null || value.isEmpty) { return '请输入邮箱地址'; } if (!value.contains('@')) { return '请输入有效的邮箱地址'; } return null; }, ), const SizedBox(height: 20), AppTextField( controller: _phoneController, labelText: '手机号码(可选)', hintText: '请输入手机号码', keyboardType: TextInputType.phone, prefixIcon: const Icon(Icons.phone_outlined), ), const SizedBox(height: 20), AppTextField( controller: _passwordController, labelText: '密码', hintText: '请输入密码', obscureText: true, prefixIcon: const Icon(Icons.lock_outline), validator: (value) { if (value == null || value.isEmpty) { return '请输入密码'; } if (value.length < 6) { return '密码至少需要6位字符'; } return null; }, ), const SizedBox(height: 20), AppTextField( controller: _confirmPasswordController, labelText: '确认密码', hintText: '请再次输入密码', obscureText: true, prefixIcon: const Icon(Icons.lock_outline), validator: (value) { if (value == null || value.isEmpty) { return '请确认密码'; } if (value != _passwordController.text) { return '两次输入的密码不一致'; } return null; }, ), const SizedBox(height: 30), if (authProvider.error != null) Padding( padding: const EdgeInsets.only(bottom: 16), child: Text( authProvider.error!, style: const TextStyle( color: Colors.red, fontSize: 14, ), ), ), if (authProvider.isLoading) const LoadingIndicator() else AppButton( text: '注册', onPressed: () async { if (_formKey.currentState!.validate()) { await authProvider.register( _nameController.text, _emailController.text.trim(), _passwordController.text, _passwordController.text, _phoneController.text.isNotEmpty ? _phoneController.text : null, ); if (authProvider.isAuthenticated) { Navigator.of(context).pushReplacementNamed( RouteConstants.home, ); } } }, ), const SizedBox(height: 20), Center( child: TextButton( onPressed: () { Navigator.of(context).pop(); }, child: const Text( '已有账号?立即登录', style: TextStyle( fontSize: 14, color: Colors.blue, ), ), ), ), const SizedBox(height: 40), Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: RichText( textAlign: TextAlign.center, text: const TextSpan( style: TextStyle( fontSize: 12, color: Colors.grey, ), children: [ TextSpan(text: '注册即表示您同意我们的'), TextSpan( text: '服务条款', style: TextStyle(color: Colors.blue), ), TextSpan(text: '和'), TextSpan( text: '隐私政策', style: TextStyle(color: Colors.blue), ), ], ), ), ), ], ), ), ), ), ); } }