refactor: extract email change modal into a dedicated StatefulWidget to improve state management and lifecycle handling

This commit is contained in:
pradeepkumar
2026-05-06 14:40:37 +05:30
parent ff71a50229
commit 03ab3bbe0e

View File

@@ -431,55 +431,90 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
} }
Future<void> _openChangeEmailDialog() async { Future<void> _openChangeEmailDialog() async {
final newEmailCtl = TextEditingController();
final passwordCtl = TextEditingController();
String? localError;
bool submitting = false;
bool obscurePw = true;
await showModalBottomSheet<void>( await showModalBottomSheet<void>(
context: context, context: context,
isScrollControlled: true, isScrollControlled: true,
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
builder: (sheetCtx) { builder: (sheetCtx) {
return StatefulBuilder( return _ChangeEmailSheet(
builder: (sheetCtx, setSheetState) { onSubmit: (email, password) async {
Future<void> submit() async { final repo = ref.read(profileRepositoryProvider);
final email = newEmailCtl.text.trim(); return repo.requestEmailChange(email, password);
final pw = passwordCtl.text; },
onSuccess: (message) {
if (mounted) _showSnackBar(message);
},
);
},
);
}
}
/// Bottom-sheet widget for changing email. Owns its own controllers so they
/// remain valid throughout the dismiss animation, then disposes cleanly.
class _ChangeEmailSheet extends StatefulWidget {
final Future<String> Function(String email, String password) onSubmit;
final void Function(String message) onSuccess;
const _ChangeEmailSheet({
required this.onSubmit,
required this.onSuccess,
});
@override
State<_ChangeEmailSheet> createState() => _ChangeEmailSheetState();
}
class _ChangeEmailSheetState extends State<_ChangeEmailSheet> {
final _newEmailCtl = TextEditingController();
final _passwordCtl = TextEditingController();
String? _localError;
bool _submitting = false;
bool _obscurePw = true;
@override
void dispose() {
_newEmailCtl.dispose();
_passwordCtl.dispose();
super.dispose();
}
Future<void> _submit() async {
final email = _newEmailCtl.text.trim();
final pw = _passwordCtl.text;
if (email.isEmpty || pw.isEmpty) { if (email.isEmpty || pw.isEmpty) {
setSheetState(() => localError = setState(() => _localError =
'Please enter a new email and your current password'); 'Please enter a new email and your current password');
return; return;
} }
setSheetState(() { setState(() {
submitting = true; _submitting = true;
localError = null; _localError = null;
}); });
try { try {
final repo = ref.read(profileRepositoryProvider); final message = await widget.onSubmit(email, pw);
final message = await repo.requestEmailChange(email, pw); if (!mounted) return;
if (sheetCtx.mounted) Navigator.of(sheetCtx).pop(); Navigator.of(context).pop();
if (mounted) _showSnackBar(message); widget.onSuccess(message);
} catch (e) { } catch (e) {
setSheetState(() { if (!mounted) return;
submitting = false; setState(() {
localError = e.toString().replaceFirst('Exception: ', ''); _submitting = false;
_localError = e.toString().replaceFirst('Exception: ', '');
}); });
} }
} }
// Add bottom inset so the sheet rises above the keyboard. @override
final bottomInset = MediaQuery.of(sheetCtx).viewInsets.bottom; Widget build(BuildContext context) {
final bottomInset = MediaQuery.of(context).viewInsets.bottom;
return Padding( return Padding(
padding: EdgeInsets.only(bottom: bottomInset), padding: EdgeInsets.only(bottom: bottomInset),
child: Container( child: Container(
decoration: const BoxDecoration( decoration: const BoxDecoration(
color: Colors.white, color: Colors.white,
borderRadius: BorderRadius.vertical( borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
top: Radius.circular(24),
),
), ),
padding: const EdgeInsets.fromLTRB(24, 12, 24, 24), padding: const EdgeInsets.fromLTRB(24, 12, 24, 24),
child: SafeArea( child: SafeArea(
@@ -521,51 +556,47 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
), ),
), ),
const SizedBox(height: 20), const SizedBox(height: 20),
_buildSheetLabel('New Email'), _label('New Email'),
const SizedBox(height: 6), const SizedBox(height: 6),
TextField( TextField(
controller: newEmailCtl, controller: _newEmailCtl,
keyboardType: TextInputType.emailAddress, keyboardType: TextInputType.emailAddress,
autocorrect: false, autocorrect: false,
enableSuggestions: false, enableSuggestions: false,
textCapitalization: TextCapitalization.none, textCapitalization: TextCapitalization.none,
autofillHints: const [AutofillHints.email], autofillHints: const [AutofillHints.email],
decoration: _sheetInputDecoration( decoration: _decoration('new@example.com'),
'new@example.com',
),
), ),
const SizedBox(height: 14), const SizedBox(height: 14),
_buildSheetLabel('Current Password'), _label('Current Password'),
const SizedBox(height: 6), const SizedBox(height: 6),
TextField( TextField(
controller: passwordCtl, controller: _passwordCtl,
obscureText: obscurePw, obscureText: _obscurePw,
autocorrect: false, autocorrect: false,
enableSuggestions: false, enableSuggestions: false,
// Empty autofillHints prevents iOS from showing the // Empty autofillHints prevents iOS from showing the
// "Strong Password" suggestion bar, which was adding // "Strong Password" suggestion bar.
// extra bottom inset.
autofillHints: const <String>[], autofillHints: const <String>[],
decoration: _sheetInputDecoration( decoration: _decoration(
'Enter your current password', 'Enter your current password',
suffixIcon: IconButton( suffixIcon: IconButton(
icon: Icon( icon: Icon(
obscurePw _obscurePw
? Icons.visibility_off_outlined ? Icons.visibility_off_outlined
: Icons.visibility_outlined, : Icons.visibility_outlined,
color: const Color(0xFF6B7280), color: const Color(0xFF6B7280),
size: 20, size: 20,
), ),
onPressed: () => setSheetState( onPressed: () =>
() => obscurePw = !obscurePw, setState(() => _obscurePw = !_obscurePw),
), ),
), ),
), ),
), if (_localError != null) ...[
if (localError != null) ...[
const SizedBox(height: 12), const SizedBox(height: 12),
Text( Text(
localError!, _localError!,
style: const TextStyle( style: const TextStyle(
fontFamily: 'SourceSerif4', fontFamily: 'SourceSerif4',
color: Colors.red, color: Colors.red,
@@ -578,16 +609,13 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
children: [ children: [
Expanded( Expanded(
child: OutlinedButton( child: OutlinedButton(
onPressed: submitting onPressed: _submitting
? null ? null
: () => Navigator.of(sheetCtx).pop(), : () => Navigator.of(context).pop(),
style: OutlinedButton.styleFrom( style: OutlinedButton.styleFrom(
side: const BorderSide( side: const BorderSide(color: Color(0xFFE5E7EB)),
color: Color(0xFFE5E7EB), padding:
), const EdgeInsets.symmetric(vertical: 14),
padding: const EdgeInsets.symmetric(
vertical: 14,
),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
), ),
@@ -606,20 +634,19 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
const SizedBox(width: 12), const SizedBox(width: 12),
Expanded( Expanded(
child: ElevatedButton( child: ElevatedButton(
onPressed: submitting ? null : submit, onPressed: _submitting ? null : _submit,
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
backgroundColor: AppColors.accentOrange, backgroundColor: AppColors.accentOrange,
foregroundColor: Colors.white, foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric( padding:
vertical: 14, const EdgeInsets.symmetric(vertical: 14),
),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
), ),
elevation: 0, elevation: 0,
), ),
child: Text( child: Text(
submitting ? 'Sending...' : 'Send Link', _submitting ? 'Sending...' : 'Send Link',
style: const TextStyle( style: const TextStyle(
fontFamily: 'Fractul', fontFamily: 'Fractul',
fontSize: 14, fontSize: 14,
@@ -636,17 +663,9 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
), ),
), ),
); );
},
);
},
);
newEmailCtl.dispose();
passwordCtl.dispose();
} }
Widget _buildSheetLabel(String text) { Widget _label(String text) => Text(
return Text(
text, text,
style: const TextStyle( style: const TextStyle(
fontFamily: 'Fractul', fontFamily: 'Fractul',
@@ -655,9 +674,8 @@ class _ProfileSettingsTabState extends ConsumerState<ProfileSettingsTab> {
color: AppColors.primaryDark, color: AppColors.primaryDark,
), ),
); );
}
InputDecoration _sheetInputDecoration(String hint, {Widget? suffixIcon}) { InputDecoration _decoration(String hint, {Widget? suffixIcon}) {
return InputDecoration( return InputDecoration(
hintText: hint, hintText: hint,
hintStyle: const TextStyle( hintStyle: const TextStyle(