Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 13.77 KB | None | 0 0
  1. // import 'package:firebase_auth/firebase_auth.dart';
  2. // import 'dart:io';
  3.  
  4. // import 'package:firebase_auth/firebase_auth.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:fluttertoast/fluttertoast.dart';
  7. import 'package:passwordsaver/services/auth.dart';
  8. import '../navigation_bloc/navigation_bloc.dart';
  9.  
  10. class Setting extends StatefulWidget with NavigationStates {
  11.   @override
  12.   _SettingState createState() => _SettingState();
  13. }
  14.  
  15. class _SettingState extends State<Setting> {
  16.   final AuthService _auth = AuthService();
  17.   //initialisation
  18.   String _name = "";
  19.   String _email = "";
  20.   String _pass = "";
  21.   String _passv = "";
  22.   bool editname = true;
  23.   bool editemail = true;
  24.   bool editpass = true;
  25.   bool show = false;
  26.  
  27. // user defined function
  28.   void _showDialog() {
  29.     // flutter defined function
  30.     showDialog(
  31.       context: context,
  32.       builder: (BuildContext context) {
  33.         // return object of type Dialog
  34.         return AlertDialog(
  35.           title: new Text("Are You Sure?"),
  36.           content: new Text(
  37.               "Deleting this account will result in completely removing it from the system and you won't be able retrieve the saved data."),
  38.           actions: <Widget>[
  39.             // usually buttons at the bottom of the dialog
  40.             FlatButton(
  41.               child: new Text("DISMISS"),
  42.               onPressed: () {
  43.                 Navigator.of(context).pop();
  44.               },
  45.             ),
  46.             FlatButton(
  47.               child: new Text("DELETE"),
  48.               onPressed: () async {
  49.                 var user = await _auth.getCurrentuser();
  50.                 if (user != null) {
  51.                   user.delete().then((e) {
  52.                     Fluttertoast.showToast(
  53.                       msg: "Delete with Success",
  54.                       toastLength: Toast.LENGTH_SHORT,
  55.                       gravity: ToastGravity.BOTTOM,
  56.                       timeInSecForIos: 1,
  57.                     );
  58.                   }).catchError((error) {
  59.                     Fluttertoast.showToast(
  60.                       msg: "Error while delete this account",
  61.                       toastLength: Toast.LENGTH_SHORT,
  62.                       gravity: ToastGravity.BOTTOM,
  63.                       timeInSecForIos: 1,
  64.                     );
  65.                   });
  66.                 }
  67.                 Navigator.of(context).pop();
  68.               },
  69.             ),
  70.           ],
  71.         );
  72.       },
  73.     );
  74.   }
  75.  
  76.   Widget deletebutton() {
  77.     return InkWell(
  78.       onTap: () {
  79.         _showDialog();
  80.       },
  81.       child: Container(
  82.         margin: EdgeInsets.only(
  83.             left: (MediaQuery.of(context).size.width * 10) / 100,
  84.             right: (MediaQuery.of(context).size.width * 10) / 100),
  85.         height: 56.0,
  86.         decoration: BoxDecoration(
  87.           borderRadius: BorderRadius.circular(5.0),
  88.           color: Color(0xFFF52525),
  89.         ),
  90.         child: Center(
  91.           child: Text(
  92.             "Delete Account",
  93.             style: TextStyle(
  94.               color: Colors.white,
  95.               fontSize: 18.0,
  96.             ),
  97.           ),
  98.         ),
  99.       ),
  100.     );
  101.   }
  102.  
  103.   void getuser() async {
  104.     var user = await _auth.getCurrentuser();
  105.  
  106.     if (user != null) {
  107.       setState(() {
  108.         _name = user.displayName;
  109.         _email = user.email;
  110.       });
  111.     }
  112.   }
  113.  
  114.   void verifem() async {
  115.     print("hello");
  116.     var user = await _auth.getCurrentuser();
  117.     if (user.isEmailVerified) {
  118.       setState(() {
  119.         show = false;
  120.       });
  121.     } else if (user.isEmailVerified == false) {
  122.       setState(() {
  123.         show = true;
  124.       });
  125.     }
  126.   }
  127.  
  128.   @override
  129.   void initState() {
  130.     getuser();
  131.     verifem();
  132.     super.initState();
  133.   }
  134.  
  135.   void reload() async {
  136.     var user = await _auth.getCurrentuser();
  137.     await user.reload();
  138.   }
  139.  
  140.   @override
  141.   Widget build(BuildContext context) {
  142.     reload();
  143.     return Scaffold(
  144.       appBar: AppBar(
  145.         //  backgroundColor: Colors.blue,
  146.         leading: SizedBox(
  147.           width: 10,
  148.         ),
  149.         title: Text(
  150.           'Settings',
  151.           style: TextStyle(
  152.             color: Colors.white,
  153.             fontWeight: FontWeight.w900,
  154.           ),
  155.         ),
  156.       ),
  157.       body: SingleChildScrollView(
  158.         child: Container(
  159.           margin:
  160.               EdgeInsets.all((MediaQuery.of(context).size.width * 15) / 100),
  161.           child: Column(
  162.             mainAxisAlignment: MainAxisAlignment.center,
  163.             children: <Widget>[
  164.               TextFormField(
  165.                 onChanged: (val) {
  166.                   setState(() {
  167.                     _name = val;
  168.                   });
  169.                 },
  170.                 scrollPadding: EdgeInsets.all(50),
  171.                 readOnly: editname,
  172.                 initialValue: _name,
  173.                 // controller:TextEditingController(text:_name),
  174.                 decoration: InputDecoration(
  175.                   hintText: _name,
  176.                   border: OutlineInputBorder(
  177.                     borderRadius: BorderRadius.all(Radius.circular(5.0)),
  178.                   ),
  179.                   prefixIcon: Icon(Icons.person),
  180.                   suffixIcon: IconButton(
  181.                     icon: Icon(Icons.edit),
  182.                     onPressed: () async {
  183.                       verifem();
  184.                       if (show == false) {
  185.                         setState(() {
  186.                           editname = false;
  187.                         });
  188.                       } else {
  189.                         Fluttertoast.showToast(
  190.                           msg: "Verify Your Email",
  191.                           toastLength: Toast.LENGTH_SHORT,
  192.                           gravity: ToastGravity.BOTTOM,
  193.                           timeInSecForIos: 1,
  194.                         );
  195.                       }
  196.                     },
  197.                   ),
  198.                 ),
  199.               ),
  200.               SizedBox(height: 10),
  201.               TextFormField(
  202.                 onChanged: (val) {
  203.                   setState(() {
  204.                     _email = val;
  205.                   });
  206.                 },
  207.                 scrollPadding: EdgeInsets.all(50),
  208.                 readOnly: editemail,
  209.                 initialValue: _email,
  210.                 // controller:TextEditingController(text:_name),
  211.                 decoration: InputDecoration(
  212.                   hintText: _email,
  213.                   border: OutlineInputBorder(
  214.                     borderRadius: BorderRadius.all(Radius.circular(5.0)),
  215.                   ),
  216.                   prefixIcon: Icon(Icons.email),
  217.                   suffixIcon: IconButton(
  218.                     icon: Icon(Icons.edit),
  219.                     onPressed: () async {
  220.                       var v = await _auth.isEmailVerified();
  221.  
  222.                       if (v) {
  223.                         setState(() {
  224.                           editemail = false;
  225.                         });
  226.                       } else {
  227.                         Fluttertoast.showToast(
  228.                           msg: "Verif Your Email",
  229.                           toastLength: Toast.LENGTH_SHORT,
  230.                           gravity: ToastGravity.BOTTOM,
  231.                           timeInSecForIos: 1,
  232.                         );
  233.                       }
  234.                     },
  235.                   ),
  236.                 ),
  237.               ),
  238.               SizedBox(height: 10),
  239.               TextFormField(
  240.                 onChanged: (val) {
  241.                   setState(() {
  242.                     _passv = val;
  243.                   });
  244.                 },
  245.                 scrollPadding: EdgeInsets.all(50),
  246.                 readOnly: editpass,
  247.                 initialValue: _pass,
  248.                 // controller:TextEditingController(text:_name),
  249.                 decoration: InputDecoration(
  250.                   hintText: "Old Password",
  251.                   border: OutlineInputBorder(
  252.                     borderRadius: BorderRadius.all(Radius.circular(5.0)),
  253.                   ),
  254.                   prefixIcon: Icon(Icons.security),
  255.                 ),
  256.               ),
  257.               SizedBox(height: 10),
  258.               TextFormField(
  259.                 onChanged: (val) {
  260.                   setState(() {
  261.                     _pass = val;
  262.                   });
  263.                 },
  264.                 scrollPadding: EdgeInsets.all(50),
  265.                 readOnly: editpass,
  266.                 initialValue: _pass,
  267.                 // controller:TextEditingController(text:_name),
  268.                 decoration: InputDecoration(
  269.                   hintText: "New Password",
  270.                   border: OutlineInputBorder(
  271.                     borderRadius: BorderRadius.all(Radius.circular(5.0)),
  272.                   ),
  273.                   prefixIcon: Icon(Icons.security),
  274.                   suffixIcon: IconButton(
  275.                     icon: Icon(Icons.edit),
  276.                     onPressed: () async {
  277.                       var v = await _auth.isEmailVerified();
  278.  
  279.                       if (v) {
  280.                         setState(() {
  281.                           editpass = false;
  282.                         });
  283.                       } else {
  284.                         Fluttertoast.showToast(
  285.                           msg: "Verif Your Email",
  286.                           toastLength: Toast.LENGTH_SHORT,
  287.                           gravity: ToastGravity.BOTTOM,
  288.                           timeInSecForIos: 1,
  289.                         );
  290.                       }
  291.                     },
  292.                   ),
  293.                 ),
  294.               ),
  295.               SizedBox(height: 10),
  296.               InkWell(
  297.                 onTap: () async {
  298.                   verifem();
  299.                   Fluttertoast.showToast(
  300.                     msg: "Reloaded",
  301.                     toastLength: Toast.LENGTH_SHORT,
  302.                     gravity: ToastGravity.BOTTOM,
  303.                     timeInSecForIos: 1,
  304.                   );
  305.                   //  var v=await _auth.isEmailVerified();
  306.                   //  if(v){
  307.                   //  dynamic result=await _auth.signInWithEmailAndPassword(_email, _passv);
  308.                   //  if(result==null){
  309.                   //    Fluttertoast.showToast(msg: "Wrong Password",toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIos: 1, );
  310.                   //  }else{
  311.                   //    await _auth.update(_name,_email,_pass);
  312.                   //    Fluttertoast.showToast(msg: "Success",toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIos: 1, );
  313.                   //  }
  314.  
  315.                   //  }
  316.                   //  else
  317.                   //  {
  318.                   //    Fluttertoast.showToast(msg: "You must verify your email first.",toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIos: 1, );
  319.                   //  }
  320.                 },
  321.                 child: Container(
  322.                   margin: EdgeInsets.only(
  323.                       left: (MediaQuery.of(context).size.width * 10) / 100,
  324.                       right: (MediaQuery.of(context).size.width * 10) / 100),
  325.                   height: 56.0,
  326.                   decoration: BoxDecoration(
  327.                     borderRadius: BorderRadius.circular(5.0),
  328.                     color: Theme.of(context).primaryColor,
  329.                   ),
  330.                   child: Center(
  331.                     child: Text(
  332.                       "Reload",
  333.                       style: TextStyle(
  334.                         color: Colors.white,
  335.                         fontSize: 18.0,
  336.                       ),
  337.                     ),
  338.                   ),
  339.                 ),
  340.               ),
  341.               Visibility(
  342.                 child: Column(
  343.                   children: <Widget>[
  344.                     SizedBox(height: 10),
  345.                     show
  346.                         ? InkWell(
  347.                             onTap: () async {
  348.                               var v = await _auth.isEmailVerified();
  349.                               if (!v) {
  350.                                 String e = await _auth.sendEmailVerification();
  351.                                 Fluttertoast.showToast(
  352.                                   msg: e,
  353.                                   toastLength: Toast.LENGTH_SHORT,
  354.                                   gravity: ToastGravity.BOTTOM,
  355.                                   timeInSecForIos: 1,
  356.                                 );
  357.                               }
  358.                             },
  359.                             child: Container(
  360.                               margin: EdgeInsets.only(
  361.                                   left:
  362.                                       (MediaQuery.of(context).size.width * 10) /
  363.                                           100,
  364.                                   right:
  365.                                       (MediaQuery.of(context).size.width * 10) /
  366.                                           100),
  367.                               height: 56.0,
  368.                               decoration: BoxDecoration(
  369.                                 borderRadius: BorderRadius.circular(5.0),
  370.                                 color: Theme.of(context).primaryColor,
  371.                               ),
  372.                               child: Center(
  373.                                 child: Text(
  374.                                   "Send Email Verification",
  375.                                   style: TextStyle(
  376.                                     color: Colors.white,
  377.                                     fontSize: 18.0,
  378.                                   ),
  379.                                 ),
  380.                               ),
  381.                             ),
  382.                           )
  383.                         : SizedBox.shrink(),
  384.                   ],
  385.                 ),
  386.                 visible: show,
  387.               ),
  388.               SizedBox(height: 10),
  389.               deletebutton(),
  390.             ],
  391.           ),
  392.         ),
  393.       ),
  394.     );
  395.   }
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement