import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../core/auth/auth_notifier.dart'; import '../../core/theme/app_theme.dart'; import '../../core/widgets/loading_button.dart'; class ChangePasswordScreen extends ConsumerStatefulWidget { const ChangePasswordScreen({super.key}); @override ConsumerState createState() => _ChangePasswordScreenState(); } class _ChangePasswordScreenState extends ConsumerState { final _formKey = GlobalKey(); final _currentCtrl = TextEditingController(); final _newCtrl = TextEditingController(); final _confirmCtrl = TextEditingController(); bool _loading = false; bool _obscureCurrent = true; bool _obscureNew = true; @override void dispose() { _currentCtrl.dispose(); _newCtrl.dispose(); _confirmCtrl.dispose(); super.dispose(); } Future _submit() async { if (!_formKey.currentState!.validate()) return; setState(() => _loading = true); try { await ref.read(authProvider.notifier).changePassword(_currentCtrl.text, _newCtrl.text); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Password changed successfully'), backgroundColor: AppColors.primary)); context.pop(); } } catch (e) { if (mounted) ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(e.toString()), backgroundColor: AppColors.danger)); } finally { if (mounted) setState(() => _loading = false); } } @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(title: const Text('Change Password')), body: Form( key: _formKey, child: ListView( padding: const EdgeInsets.all(16), children: [ _label('Current Password'), TextFormField( controller: _currentCtrl, obscureText: _obscureCurrent, decoration: InputDecoration( hintText: 'Enter current password', suffixIcon: IconButton( onPressed: () => setState(() => _obscureCurrent = !_obscureCurrent), icon: Icon(_obscureCurrent ? Icons.visibility_off_outlined : Icons.visibility_outlined, color: AppColors.textSub), ), ), validator: (v) => (v == null || v.isEmpty) ? 'Required' : null, ), const SizedBox(height: 16), _label('New Password'), TextFormField( controller: _newCtrl, obscureText: _obscureNew, decoration: InputDecoration( hintText: 'Minimum 6 characters', suffixIcon: IconButton( onPressed: () => setState(() => _obscureNew = !_obscureNew), icon: Icon(_obscureNew ? Icons.visibility_off_outlined : Icons.visibility_outlined, color: AppColors.textSub), ), ), validator: (v) { if (v == null || v.isEmpty) return 'Required'; if (v.length < 6) return 'Minimum 6 characters'; return null; }, ), const SizedBox(height: 16), _label('Confirm New Password'), TextFormField( controller: _confirmCtrl, obscureText: true, decoration: const InputDecoration(hintText: 'Repeat new password'), validator: (v) { if (v == null || v.isEmpty) return 'Required'; if (v != _newCtrl.text) return 'Passwords do not match'; return null; }, ), const SizedBox(height: 32), LoadingButton(loading: _loading, label: 'Save', onPressed: _submit), ], ), ), ); Widget _label(String text) => Padding( padding: const EdgeInsets.only(bottom: 6), child: Text(text, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: AppColors.text)), ); }