204 lines
6.6 KiB
Dart
204 lines
6.6 KiB
Dart
import 'dart:ui';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../providers/auth_provider.dart';
|
|
import '../../theme/colors.dart';
|
|
|
|
class BindEmailScreen extends ConsumerStatefulWidget {
|
|
const BindEmailScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<BindEmailScreen> createState() => _BindEmailScreenState();
|
|
}
|
|
|
|
class _BindEmailScreenState extends ConsumerState<BindEmailScreen> {
|
|
final _emailController = TextEditingController();
|
|
final _formKey = GlobalKey<FormState>();
|
|
bool _isLoading = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String? _validateEmail(String? value) {
|
|
if (value == null || value.isEmpty) {
|
|
return '请输入邮箱';
|
|
}
|
|
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
|
if (!emailRegex.hasMatch(value)) {
|
|
return '请输入有效的邮箱地址';
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> _handleBind() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
|
|
setState(() => _isLoading = true);
|
|
|
|
try {
|
|
final authApi = ref.read(authApiProvider);
|
|
final result = await authApi.bindEmail(_emailController.text);
|
|
|
|
if (result['success'] == true) {
|
|
if (mounted) {
|
|
Navigator.pop(context);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('邮箱绑定成功'),
|
|
backgroundColor: AppColors.income,
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(result['error'] ?? '绑定失败'),
|
|
backgroundColor: AppColors.expense,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('网络错误: $e'),
|
|
backgroundColor: AppColors.expense,
|
|
),
|
|
);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() => _isLoading = false);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Container(
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
colors: [
|
|
Color(0xFFFF6B35),
|
|
Color(0xFFFF9F5B),
|
|
],
|
|
),
|
|
),
|
|
child: SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 32),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Text(
|
|
'绑定邮箱',
|
|
style: TextStyle(
|
|
fontSize: 32,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text(
|
|
'请输入您的邮箱地址',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: Colors.white70,
|
|
),
|
|
),
|
|
const SizedBox(height: 48),
|
|
_buildGlassInput(
|
|
child: TextFormField(
|
|
controller: _emailController,
|
|
keyboardType: TextInputType.emailAddress,
|
|
style: const TextStyle(color: AppColors.textPrimary),
|
|
decoration: const InputDecoration(
|
|
hintText: '请输入邮箱',
|
|
hintStyle: TextStyle(color: AppColors.textSecondary),
|
|
border: InputBorder.none,
|
|
prefixIcon: Icon(Icons.email_outlined, color: AppColors.brand),
|
|
),
|
|
validator: _validateEmail,
|
|
),
|
|
),
|
|
const SizedBox(height: 32),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
height: 56,
|
|
child: ElevatedButton(
|
|
onPressed: _isLoading ? null : _handleBind,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.white,
|
|
foregroundColor: AppColors.brand,
|
|
disabledBackgroundColor: Colors.white.withOpacity(0.6),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
elevation: 0,
|
|
),
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
width: 24,
|
|
height: 24,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
valueColor: AlwaysStoppedAnimation<Color>(AppColors.brand),
|
|
),
|
|
)
|
|
: const Text(
|
|
'绑定',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildGlassInput({required Widget child}) {
|
|
return Container(
|
|
decoration: BoxDecoration(
|
|
color: Colors.white.withOpacity(0.8),
|
|
borderRadius: BorderRadius.circular(14),
|
|
border: Border.all(color: Colors.white.withOpacity(0.8)),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: AppColors.brand.withOpacity(0.06),
|
|
blurRadius: 32,
|
|
offset: const Offset(0, 8),
|
|
),
|
|
],
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(14),
|
|
child: BackdropFilter(
|
|
filter: ImageFilter.blur(sigmaX: 30, sigmaY: 30),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
|
child: child,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |