From e8f2a4a6c23228be5853b9a3f539b6c9d8ac1e30 Mon Sep 17 00:00:00 2001 From: Felipe Date: Thu, 26 Oct 2023 10:32:03 -0500 Subject: [PATCH] fix --- lib/src/screens/profile.dart | 146 ++++++++++++++++++++++--------- lib/src/screens/profile_web.dart | 86 +++++++++++++++++- 2 files changed, 192 insertions(+), 40 deletions(-) diff --git a/lib/src/screens/profile.dart b/lib/src/screens/profile.dart index d231aec..a3fb9e5 100644 --- a/lib/src/screens/profile.dart +++ b/lib/src/screens/profile.dart @@ -330,46 +330,30 @@ class _ProfileScreenState extends State { builder: (BuildContext context) { return AlertDialog( title: const Text('Actualizar Email'), - content: Column( - mainAxisSize: MainAxisSize.min, - children: [ - TextFormField( - controller: emailController, - decoration: const InputDecoration( - labelText: 'Nuevo Email', - ), + content: const Text( + '¿Recuerda primero volver a iniciar sesión para confirmar tu identidad .'), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('Cancelar'), + ), + TextButton( + onPressed: () { + String newEmail = emailController.text.trim(); + if (newEmail.isNotEmpty) { + _updateEmail(newEmail); + } + Navigator.of(context).pop(); + }, + child: const Text( + 'Guardar', + style: + TextStyle(color: Colors.red, fontWeight: FontWeight.w600), ), - const SizedBox(height: 16.0), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - ElevatedButton( - onPressed: () { - Navigator.of(context).pop(); - }, - style: ElevatedButton.styleFrom( - backgroundColor: Colors.grey, - ), - child: const Text('Cancelar'), - ), - ElevatedButton( - onPressed: () { - // Llama a una función para actualizar el correo electrónico. - String newEmail = emailController.text.trim(); - if (newEmail.isNotEmpty) { - _updateEmail(newEmail); - } - Navigator.of(context).pop(); - }, - style: ElevatedButton.styleFrom( - backgroundColor: Colors.blue, - ), - child: const Text('Guardar'), - ), - ], - ), - ], - ), + ), + ], ); }, ); @@ -392,6 +376,67 @@ class _ProfileScreenState extends State { } } + Future deleteAccount() async { + try { + final currentUser = _auth.currentUser; + + if (currentUser != null) { + final uid = currentUser.uid; + + await FirebaseFirestore.instance.collection('users').doc(uid).delete(); + + await currentUser.delete(); + + await _auth.signOut(); + + Get.snackbar( + 'Cuenta Eliminada', + 'Tu cuenta ha sido eliminada con éxito.', + snackPosition: SnackPosition.BOTTOM, + ); + } + } catch (e) { + Get.snackbar( + 'Error al Eliminar Cuenta', + 'Hubo un error al eliminar tu cuenta. Por favor, inténtalo de nuevo más tarde.', + snackPosition: SnackPosition.BOTTOM, + ); + } + } + + Future _showDeleteAccountConfirmationDialog( + BuildContext context) async { + return showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Eliminar Cuenta'), + content: const Text( + '¿Estás seguro de que deseas eliminar tu cuenta? Esta acción no se puede deshacer.'), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('Cancelar'), + ), + TextButton( + onPressed: () { + deleteAccount(); + Navigator.of(context).pop(); + }, + child: const Text( + 'Eliminar', + style: + TextStyle(color: Colors.red, fontWeight: FontWeight.w600), + ), + ), + ], + ); + }, + ); + } + Future _showChoiceDialog(BuildContext context) async { return showDialog( context: context, @@ -630,6 +675,29 @@ class _ProfileScreenState extends State { ), ), ), + const SizedBox(height: 15), + ElevatedButton( + onPressed: () { + _showDeleteAccountConfirmationDialog(context); + }, + style: ElevatedButton.styleFrom( + backgroundColor: + Colors.red, // Puedes elegir un color adecuado + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(50), + ), + elevation: 0, + minimumSize: const Size(300, 45), + ), + child: const Text( + 'Eliminar Cuenta', + style: TextStyle( + color: Colors.white, + fontWeight: FontWeight.bold, + fontSize: 17, + ), + ), + ), const SizedBox(height: 40), ElevatedButton( onPressed: () async { diff --git a/lib/src/screens/profile_web.dart b/lib/src/screens/profile_web.dart index 38ae3fb..4ce2d54 100644 --- a/lib/src/screens/profile_web.dart +++ b/lib/src/screens/profile_web.dart @@ -253,6 +253,67 @@ class _ProfileWebScreenState extends State { } } + Future deleteAccount() async { + try { + final currentUser = _auth.currentUser; + + if (currentUser != null) { + final uid = currentUser.uid; + + await FirebaseFirestore.instance.collection('users').doc(uid).delete(); + + await currentUser.delete(); + + await _auth.signOut(); + + Get.snackbar( + 'Cuenta Eliminada', + 'Tu cuenta ha sido eliminada con éxito.', + snackPosition: SnackPosition.BOTTOM, + ); + } + } catch (e) { + Get.snackbar( + 'Error al Eliminar Cuenta', + 'Hubo un error al eliminar tu cuenta. Por favor, inténtalo de nuevo más tarde.', + snackPosition: SnackPosition.BOTTOM, + ); + } + } + + Future _showDeleteAccountConfirmationDialog( + BuildContext context) async { + return showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Eliminar Cuenta'), + content: const Text( + '¿Estás seguro de que deseas eliminar tu cuenta? Esta acción no se puede deshacer.'), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('Cancelar'), + ), + TextButton( + onPressed: () { + deleteAccount(); + Navigator.of(context).pop(); + }, + child: const Text( + 'Eliminar', + style: + TextStyle(color: Colors.red, fontWeight: FontWeight.w600), + ), + ), + ], + ); + }, + ); + } + Future updateEmailAndPassword(String email, String password) async { final User? user = FirebaseAuth.instance.currentUser; if (user != null) { @@ -388,7 +449,7 @@ class _ProfileWebScreenState extends State { body: Center( child: SizedBox( width: 300, - height: 600, + height: 680, child: Card( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), @@ -579,6 +640,29 @@ class _ProfileWebScreenState extends State { ), ), ), + const SizedBox(height: 15), + ElevatedButton( + onPressed: () { + _showDeleteAccountConfirmationDialog(context); + }, + style: ElevatedButton.styleFrom( + backgroundColor: + Colors.red, // Puedes elegir un color adecuado + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(50), + ), + elevation: 0, + minimumSize: const Size(300, 45), + ), + child: const Text( + 'Eliminar Cuenta', + style: TextStyle( + color: Colors.white, + fontWeight: FontWeight.bold, + fontSize: 17, + ), + ), + ), const SizedBox(height: 40), ElevatedButton( onPressed: () async {