mrblab

delete_user.dart

Apr 12th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.49 KB | None | 0 0
  1. import 'package:easy_localization/easy_localization.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:fluttertoast/fluttertoast.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:rounded_loading_button/rounded_loading_button.dart';
  6. import 'package:wordpress_app/blocs/user_bloc.dart';
  7. import 'package:wordpress_app/models/user.dart';
  8. import 'package:wordpress_app/pages/welcome.dart';
  9. import 'package:wordpress_app/services/auth_service.dart';
  10. import 'package:wordpress_app/utils/next_screen.dart';
  11.  
  12. class DeleteUser extends StatefulWidget {
  13.   const DeleteUser({Key? key}) : super(key: key);
  14.  
  15.   @override
  16.   State<DeleteUser> createState() => _DeleteUserState();
  17. }
  18.  
  19. class _DeleteUserState extends State<DeleteUser> {
  20.   final formkey = GlobalKey<FormState>();
  21.   final _btnCtlr = RoundedLoadingButtonController();
  22.   final passwordCtlr = TextEditingController();
  23.  
  24.   _handleDeleteAccount() async {
  25.     final UserBloc ub = context.read<UserBloc>();
  26.     if (formkey.currentState!.validate()) {
  27.       formkey.currentState!.save();
  28.       FocusScope.of(context).requestFocus(new FocusNode());
  29.       _btnCtlr.start();
  30.       await AuthService.loginWithEmail(ub.name!, passwordCtlr.text).then((UserModel? userModel) async {
  31.         if (userModel != null && userModel.token != null) {
  32.           await AuthService.deleteUserAccount(userModel.token!).then((bool value) async {
  33.             if (value) {
  34.               await ub.userSignout();
  35.               _btnCtlr.success();
  36.               Fluttertoast.showToast(msg: 'Account deleted successfully!');
  37.               Future.delayed(Duration(seconds: 1)).then((value) {
  38.                 Navigator.pop(context);
  39.                 nextScreenCloseOthers(context, WelcomePage());
  40.               });
  41.             } else {
  42.               _btnCtlr.reset();
  43.             }
  44.           });
  45.         } else {
  46.           Fluttertoast.showToast(msg: 'Password is incorrect. Please try again');
  47.           debugPrint('Problem while login');
  48.           _btnCtlr.reset();
  49.         }
  50.       });
  51.     }
  52.   }
  53.  
  54.   @override
  55.   Widget build(BuildContext context) {
  56.     return Padding(
  57.       padding:
  58.           EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  59.       child: Container(
  60.         height: MediaQuery.of(context).size.height * 0.50,
  61.         padding: EdgeInsets.fromLTRB(30, 30, 30, 20),
  62.         child: Column(
  63.           children: [
  64.             Form(
  65.               key: formkey,
  66.               child: TextFormField(
  67.                 keyboardType: TextInputType.visiblePassword,
  68.                 decoration: InputDecoration(hintText: 'Enter your password'),
  69.                 controller: passwordCtlr,
  70.                 validator: (value) {
  71.                   if (value!.isEmpty) return "Password shouldn't be empty";
  72.                   return null;
  73.                 },
  74.               ),
  75.             ),
  76.             SizedBox(
  77.               height: 50,
  78.             ),
  79.             RoundedLoadingButton(
  80.               animateOnTap: false,
  81.               controller: _btnCtlr,
  82.               child: Text(
  83.                 'account-delete-confirm',
  84.                 style: Theme.of(context)
  85.                     .textTheme
  86.                     .titleMedium
  87.                     ?.copyWith(color: Colors.white, fontWeight: FontWeight.w600),
  88.               ).tr(),
  89.               elevation: 0,
  90.               color: Theme.of(context).primaryColor,
  91.               onPressed: () => _handleDeleteAccount(),
  92.             )
  93.           ],
  94.         ),
  95.       ),
  96.     );
  97.   }
  98. }
Add Comment
Please, Sign In to add comment