import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../../services/auth_service.dart'; class ForgotPasswordScreen extends StatefulWidget { const ForgotPasswordScreen({super.key}); @override State createState() => _ForgotPasswordScreenState(); } class _ForgotPasswordScreenState extends State { final _formKey = GlobalKey(); final _emailController = TextEditingController(); bool _isSubmitting = false; bool _emailSent = false; @override void dispose() { _emailController.dispose(); super.dispose(); } // 비밀번호 재설정 이메일 전송 함수 Future _resetPassword() async { if (_formKey.currentState!.validate()) { setState(() { _isSubmitting = true; }); final authService = Provider.of(context, listen: false); final success = await authService.sendPasswordResetEmail( _emailController.text.trim(), ); setState(() { _isSubmitting = false; _emailSent = success; }); if (success && mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('비밀번호 재설정 이메일이 전송되었습니다. 이메일을 확인해주세요.'), backgroundColor: Colors.green, ), ); } else if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('비밀번호 재설정 이메일 전송에 실패했습니다. 이메일 주소를 확인해주세요.'), backgroundColor: Colors.red, ), ); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('비밀번호 찾기'), ), body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: Form( key: _formKey, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Icon( Icons.lock_reset, size: 80, color: Colors.blue, ), const SizedBox(height: 24), const Text( '비밀번호를 잊으셨나요?', textAlign: TextAlign.center, style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 16), const Text( '가입하신 이메일 주소를 입력하시면 비밀번호 재설정 링크를 보내드립니다.', textAlign: TextAlign.center, style: TextStyle( fontSize: 16, color: Colors.grey, ), ), const SizedBox(height: 32), // 이메일 입력 필드 TextFormField( controller: _emailController, keyboardType: TextInputType.emailAddress, decoration: const InputDecoration( labelText: '이메일', prefixIcon: Icon(Icons.email), border: OutlineInputBorder(), ), validator: (value) { if (value == null || value.isEmpty) { return '이메일을 입력해주세요'; } if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(value)) { return '유효한 이메일 주소를 입력해주세요'; } return null; }, ), const SizedBox(height: 24), // 비밀번호 재설정 이메일 전송 버튼 ElevatedButton( onPressed: _isSubmitting ? null : _resetPassword, style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 16), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), child: _isSubmitting ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( strokeWidth: 2, color: Colors.white, ), ) : const Text( '비밀번호 재설정 이메일 전송', style: TextStyle(fontSize: 16), ), ), const SizedBox(height: 16), // 로그인 화면으로 돌아가기 Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text('비밀번호가 기억나셨나요?'), TextButton( onPressed: () { Navigator.pop(context); // 로그인 화면으로 돌아가기 }, child: const Text('로그인'), ), ], ), ], ), ), ), ), ), ); } }