diff --git a/lib/src/pages/home/profile.dart b/lib/src/pages/home/profile.dart index 4433f3d8c0..4d8b58857a 100644 --- a/lib/src/pages/home/profile.dart +++ b/lib/src/pages/home/profile.dart @@ -256,6 +256,83 @@ class _UserProfileState extends ConsumerState { } } + void buildDeleteDialog(BuildContext context) { + showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text( + "This account will be deleted permanently !", + style: GoogleFonts.ubuntu( + textStyle: TextStyle( + color: Color(0xFFDC4654), + fontSize: 15, + fontWeight: FontWeight.bold, + ), + ), + ), + content: Container( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + TextButton( + onPressed: () async { + bool success = await AuthApiClient.delete(); + if (success) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text("Account deleted successfully."), + ), + ); + await forgetUser(); + await Navigator.of(context).pushAndRemoveUntil( + MaterialPageRoute( + builder: (context) => WelcomePage()), + (Route route) => false); + } else { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text("Error deleting this account."), + ), + ); + } + }, + style: ButtonStyle( + alignment: Alignment.center, + backgroundColor: WidgetStateProperty.all(Color(0xFFDC4654)), + ), + child: Text( + "Delete", + style: GoogleFonts.ubuntu( + textStyle: TextStyle( + color: Color.fromRGBO(255, 255, 255, 1), + fontSize: 15, + ), + ), + )), + TextButton( + onPressed: () { + Navigator.pop(context); + }, + style: ButtonStyle( + alignment: Alignment.center, + backgroundColor: WidgetStateProperty.all(Color(0xFF737373)), + ), + child: Text( + AppLocalizations.of(context)!.cancel, + style: GoogleFonts.ubuntu( + textStyle: TextStyle( + color: Color.fromRGBO(255, 255, 255, 1), + fontSize: 15, + ), + ), + )), + ], + ), + ), + ), + ); + } + @override void initState() { getLikedList = (currentUser! == guestUser) @@ -291,6 +368,7 @@ class _UserProfileState extends ConsumerState { @override Widget build(BuildContext context) { final Size size = MediaQuery.of(context).size; + final isDarkMode = Theme.of(context).brightness == Brightness.dark; return Scaffold( extendBodyBehindAppBar: true, appBar: AppBar( @@ -651,6 +729,45 @@ class _UserProfileState extends ConsumerState { } }), ), + Center( + child: TextButton( + onPressed: () async { + if (currentUser == guestUser) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text("Anonymous User !!"))); + } else { + buildDeleteDialog(context); + } + }, + child: Padding( + padding: const EdgeInsets.symmetric( + horizontal: 20, vertical: 5), + child: Text( + "Delete Account", + style: GoogleFonts.ubuntu( + textStyle: TextStyle( + color: Colors.white, + fontSize: 18, + ), + ), + ), + ), + style: ButtonStyle( + shape: + WidgetStateProperty.all( + RoundedRectangleBorder( + borderRadius: BorderRadius.circular(8.0), + ), + ), + backgroundColor: WidgetStateProperty.all( + isDarkMode + ? Color.fromRGBO(126, 33, 58, 1) + : Color(0xFFDC4654), + ), + ), + ), + ), + SizedBox(height: 30), ], ), ) diff --git a/lib/src/util/api/auth_api.dart b/lib/src/util/api/auth_api.dart index 693251663b..832edf6935 100644 --- a/lib/src/util/api/auth_api.dart +++ b/lib/src/util/api/auth_api.dart @@ -165,4 +165,23 @@ class AuthApiClient { } } catch (e) {} } + + // Send a request to delete the current User. + static Future delete() async { + bool isDeleted = false; + print(currentUser!.token); + try { + var response = await http.delete( + Uri.parse(GeneralEndPoints.baseUrl + "auth/" + "delete"), + headers: { + "Authorization": "Token ${currentUser!.token!}", + }, + ); + var json = jsonDecode(response.body); + isDeleted = json['success']; + } catch (e) { + print(e); + } + return isDeleted; + } }