Advertisement
maugost

signup

Jun 5th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 19.27 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:firebase_auth/firebase_auth.dart';
  4. import 'package:firebase_database/firebase_database.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:girlies/app/data.dart';
  9. import 'package:girlies/app/progress.dart';
  10. import 'package:girlies/main.dart';
  11.  
  12. class GirliesSignUp extends StatefulWidget {
  13.   @override
  14.   _GirliesSignUpState createState() => new _GirliesSignUpState();
  15. }
  16.  
  17. class _GirliesSignUpState extends State<GirliesSignUp> {
  18.   BuildContext context;
  19.   String fullName;
  20.   String email;
  21.   String phone;
  22.   String userid;
  23.   String profileImgUrl;
  24.   bool isLoggedIn;
  25.   String _btnText;
  26.   FirebaseUser user;
  27.   FirebaseAuth _auth;
  28.   bool _isSignedIn;
  29.   final scaffoldKey = new GlobalKey<ScaffoldState>();
  30.  
  31.   final TextEditingController _nameController = new TextEditingController();
  32.   final TextEditingController _mobileController = new TextEditingController();
  33.   final TextEditingController _emailController = new TextEditingController();
  34.   final TextEditingController _passController = new TextEditingController();
  35.   final userRef = FirebaseDatabase.instance.reference().child(AppData.userDB);
  36.  
  37.   @override
  38.   void initState() {
  39.     // TODO: implement initState
  40.     super.initState();
  41.     _auth = FirebaseAuth.instance;
  42.     _getCurrentUser();
  43.   }
  44.  
  45.   _getCurrentUser() async {
  46.     if (user != null) {
  47.       setState(() {
  48.         _isSignedIn = true;
  49.         AppData.currentUserID = user.uid;
  50.       });
  51.     }
  52.  
  53.     _auth.onAuthStateChanged.listen((user) {
  54.       if (user == null) {
  55.         setState(() {
  56.           _isSignedIn = false;
  57.           AppData.currentUserID = null;
  58.         });
  59.       } else {
  60.         _isSignedIn = true;
  61.         AppData.currentUserID = user.uid;
  62.         UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
  63.         userUpdateInfo.photoUrl = "";
  64.         userUpdateInfo.displayName = _nameController.text;
  65.         _auth.updateProfile(userUpdateInfo);
  66.         Navigator.of(context).pop();
  67.         Navigator.of(context).pop();
  68.       }
  69.     });
  70.   }
  71.  
  72.   @override
  73.   Widget build(BuildContext context) {
  74.     this.context = context;
  75.  
  76.     final Size screenSize = MediaQuery.of(context).size;
  77.     return new Scaffold(
  78.         key: scaffoldKey,
  79.         backgroundColor: MyApp.appColors,
  80.         appBar: new AppBar(
  81.           backgroundColor: MyApp.appColors,
  82.           title: new GestureDetector(
  83.             onLongPress: () {},
  84.             child: new Text(
  85.               "SignUp",
  86.               style: new TextStyle(color: Colors.white),
  87.             ),
  88.           ),
  89.           centerTitle: true,
  90.         ),
  91.         resizeToAvoidBottomPadding: true,
  92.         body: new SingleChildScrollView(
  93.           child: new Column(
  94.             children: <Widget>[
  95.               new Container(
  96.                 height: 120.0,
  97.                 child: new Center(
  98.                   child: new Column(
  99.                     mainAxisAlignment: MainAxisAlignment.center,
  100.                     children: <Widget>[
  101.                       new Container(
  102.                           margin: new EdgeInsets.only(top: 20.0, bottom: 0.0),
  103.                           height: 40.0,
  104.                           width: 40.0,
  105.                           child: new Image.asset(
  106.                               'assets/images/girlies_logo.png')),
  107.                       new SizedBox(
  108.                         height: 20.0,
  109.                         child: new Image.asset(
  110.                           "assets/images/girlies_text.png",
  111.                           height: 20.0,
  112.                         ),
  113.                       ),
  114.                     ],
  115.                   ),
  116.                 ),
  117.                 decoration: new BoxDecoration(
  118.                     color: MyApp.appColors,
  119.                     borderRadius: new BorderRadius.only(
  120.                         bottomLeft: new Radius.circular(20.0),
  121.                         bottomRight: new Radius.circular(20.0))),
  122.               ),
  123.               new Container(
  124.                 margin: new EdgeInsets.only(left: 5.0, right: 5.0),
  125.                 decoration: new BoxDecoration(
  126.                     color: Colors.white,
  127.                     borderRadius: new BorderRadius.only(
  128.                         topLeft: new Radius.circular(20.0),
  129.                         topRight: new Radius.circular(20.0))),
  130.                 constraints: const BoxConstraints(maxHeight: 350.0),
  131.                 child: new Column(
  132.                   mainAxisAlignment: MainAxisAlignment.center,
  133.                   children: <Widget>[
  134.                     new Container(
  135.                       height: 60.0,
  136.                       margin: new EdgeInsets.only(top: 5.0),
  137.                       child: new Padding(
  138.                         padding: const EdgeInsets.all(5.0),
  139.                         child: new Container(
  140.                           width: screenSize.width,
  141.                           margin: new EdgeInsets.only(
  142.                               left: 10.0, right: 10.0, bottom: 2.0),
  143.                           height: 60.0,
  144.                           decoration: new BoxDecoration(
  145.                               borderRadius: new BorderRadius.all(
  146.                                   new Radius.circular(20.0))),
  147.                           child: new TextFormField(
  148.                             controller: _nameController,
  149.                             style: new TextStyle(
  150.                                 color: MyApp.appColors[500], fontSize: 18.0),
  151.                             decoration: new InputDecoration(
  152.                               prefixIcon: new Padding(
  153.                                 padding: const EdgeInsets.all(5.0),
  154.                                 child: new Icon(
  155.                                   Icons.person,
  156.                                   size: 20.0,
  157.                                 ),
  158.                               ),
  159.                               contentPadding: EdgeInsets.all(12.0),
  160.                               labelText: "Full Name",
  161.                               labelStyle: new TextStyle(
  162.                                   fontSize: 20.0, color: Colors.black54),
  163.                               border: OutlineInputBorder(
  164.                                   borderRadius: BorderRadius.circular(20.0),
  165.                                   borderSide:
  166.                                       new BorderSide(color: Colors.black54)),
  167.                             ),
  168.                           ),
  169.                         ),
  170.                       ),
  171.                     ),
  172.                     new Container(
  173.                       height: 60.0,
  174.                       margin: new EdgeInsets.only(top: 5.0),
  175.                       child: new Padding(
  176.                         padding: const EdgeInsets.all(5.0),
  177.                         child: new Container(
  178.                           width: screenSize.width,
  179.                           margin: new EdgeInsets.only(
  180.                               left: 10.0, right: 10.0, bottom: 2.0),
  181.                           height: 60.0,
  182.                           decoration: new BoxDecoration(
  183.                               borderRadius: new BorderRadius.all(
  184.                                   new Radius.circular(20.0))),
  185.                           child: new TextFormField(
  186.                             controller: _mobileController,
  187.                             keyboardType: TextInputType.phone,
  188.                             style: new TextStyle(
  189.                                 color: MyApp.appColors[500], fontSize: 18.0),
  190.                             decoration: new InputDecoration(
  191.                               prefixIcon: new Padding(
  192.                                 padding: const EdgeInsets.all(5.0),
  193.                                 child: new Icon(
  194.                                   Icons.phone,
  195.                                   size: 20.0,
  196.                                 ),
  197.                               ),
  198.                               contentPadding: EdgeInsets.all(12.0),
  199.                               labelText: "Mobile Number",
  200.                               labelStyle: new TextStyle(
  201.                                   fontSize: 20.0, color: Colors.black54),
  202.                               border: OutlineInputBorder(
  203.                                   borderRadius: BorderRadius.circular(20.0),
  204.                                   borderSide:
  205.                                       new BorderSide(color: Colors.black54)),
  206.                             ),
  207.                           ),
  208.                         ),
  209.                       ),
  210.                     ),
  211.                     new Container(
  212.                       height: 60.0,
  213.                       margin: new EdgeInsets.only(top: 5.0),
  214.                       child: new Padding(
  215.                         padding: const EdgeInsets.all(5.0),
  216.                         child: new Container(
  217.                           width: screenSize.width,
  218.                           margin: new EdgeInsets.only(
  219.                               left: 10.0, right: 10.0, bottom: 2.0),
  220.                           height: 60.0,
  221.                           decoration: new BoxDecoration(
  222.                               borderRadius: new BorderRadius.all(
  223.                                   new Radius.circular(20.0))),
  224.                           child: new TextFormField(
  225.                             controller: _emailController,
  226.                             style: new TextStyle(
  227.                                 color: MyApp.appColors[500], fontSize: 18.0),
  228.                             decoration: new InputDecoration(
  229.                               prefixIcon: new Padding(
  230.                                 padding: const EdgeInsets.all(5.0),
  231.                                 child: new Icon(
  232.                                   Icons.email,
  233.                                   size: 20.0,
  234.                                 ),
  235.                               ),
  236.                               contentPadding: EdgeInsets.all(12.0),
  237.                               labelText: "Email",
  238.                               labelStyle: new TextStyle(
  239.                                   fontSize: 20.0, color: Colors.black54),
  240.                               border: OutlineInputBorder(
  241.                                   borderRadius: BorderRadius.circular(20.0),
  242.                                   borderSide:
  243.                                       new BorderSide(color: Colors.black54)),
  244.                             ),
  245.                           ),
  246.                         ),
  247.                       ),
  248.                     ),
  249.                     new Container(
  250.                       height: 60.0,
  251.                       margin: new EdgeInsets.only(top: 5.0),
  252.                       child: new Padding(
  253.                         padding: const EdgeInsets.all(5.0),
  254.                         child: new Container(
  255.                           width: screenSize.width,
  256.                           margin: new EdgeInsets.only(
  257.                               left: 10.0, right: 10.0, bottom: 2.0),
  258.                           height: 60.0,
  259.                           decoration: new BoxDecoration(
  260.                               borderRadius: new BorderRadius.all(
  261.                                   new Radius.circular(20.0))),
  262.                           child: new TextFormField(
  263.                             controller: _passController,
  264.                             obscureText: true,
  265.                             style: new TextStyle(
  266.                                 color: MyApp.appColors[500], fontSize: 18.0),
  267.                             decoration: new InputDecoration(
  268.                               prefixIcon: new Padding(
  269.                                 padding: const EdgeInsets.all(5.0),
  270.                                 child: new Icon(
  271.                                   Icons.lock,
  272.                                   size: 20.0,
  273.                                 ),
  274.                               ),
  275.                               contentPadding: EdgeInsets.all(12.0),
  276.                               labelText: "Password",
  277.                               labelStyle: new TextStyle(
  278.                                   fontSize: 20.0, color: Colors.black54),
  279.                               border: OutlineInputBorder(
  280.                                   borderRadius: BorderRadius.circular(20.0),
  281.                                   borderSide:
  282.                                       new BorderSide(color: Colors.black54)),
  283.                             ),
  284.                           ),
  285.                         ),
  286.                       ),
  287.                     ),
  288.                     new InkWell(
  289.                       onTap: () {
  290.                         createUserAccount();
  291.                       },
  292.                       child: new Container(
  293.                         height: 60.0,
  294.                         margin: new EdgeInsets.only(top: 5.0),
  295.                         child: new Padding(
  296.                           padding: const EdgeInsets.all(5.0),
  297.                           child: new Container(
  298.                             width: screenSize.width,
  299.                             margin: new EdgeInsets.only(
  300.                                 left: 10.0, right: 10.0, bottom: 2.0),
  301.                             height: 60.0,
  302.                             decoration: new BoxDecoration(
  303.                                 color: MyApp.appColors[400],
  304.                                 borderRadius: new BorderRadius.all(
  305.                                     new Radius.circular(20.0))),
  306.                             child: new Center(
  307.                                 child: new Text(
  308.                               "Create Account",
  309.                               style: new TextStyle(
  310.                                   color: Colors.white, fontSize: 20.0),
  311.                             )),
  312.                           ),
  313.                         ),
  314.                       ),
  315.                     ),
  316.                   ],
  317.                 ),
  318.               ),
  319.             ],
  320.           ),
  321.         ));
  322.   }
  323.  
  324.   Widget _buildListItem(String title, IconData iconData, VoidCallback action) {
  325.     final textStyle =
  326.         new TextStyle(fontSize: 15.0, fontWeight: FontWeight.w500);
  327.  
  328.     return new InkWell(
  329.       onTap: action,
  330.       child: new Padding(
  331.         padding: const EdgeInsets.only(
  332.             left: 10.0, right: 10.0, bottom: 5.0, top: 5.0),
  333.         child: new Row(
  334.           mainAxisAlignment: MainAxisAlignment.start,
  335.           crossAxisAlignment: CrossAxisAlignment.center,
  336.           children: <Widget>[
  337.             new Container(
  338.               width: 35.0,
  339.               height: 35.0,
  340.               margin: const EdgeInsets.only(right: 10.0),
  341.               decoration: new BoxDecoration(
  342.                 color: MyApp.appColors[600],
  343.                 borderRadius: new BorderRadius.circular(5.0),
  344.               ),
  345.               alignment: Alignment.center,
  346.               child: new Icon(iconData, color: Colors.white, size: 20.0),
  347.             ),
  348.             new Text(title, style: textStyle),
  349.             new Expanded(child: new Container()),
  350.             new IconButton(
  351.                 icon: new Icon(Icons.chevron_right, color: Colors.black26),
  352.                 onPressed: null)
  353.           ],
  354.         ),
  355.       ),
  356.     );
  357.   }
  358.  
  359.   Widget _buildAvatar() {
  360.     final mainTextStyle = new TextStyle(
  361.         fontFamily: 'Timeburner',
  362.         color: Colors.white,
  363.         fontWeight: FontWeight.w500,
  364.         fontSize: 20.0);
  365.     final subTextStyle = new TextStyle(
  366.         fontFamily: 'Timeburner',
  367.         fontSize: 16.0,
  368.         color: Colors.white70,
  369.         fontWeight: FontWeight.w500);
  370.  
  371.     return new Container(
  372.       margin: new EdgeInsets.only(top: 10.0, bottom: 10.0),
  373.       child: new Row(
  374.         children: <Widget>[
  375.           new GestureDetector(
  376.             //onTap: isLoggedIn == true ? setProfilePicture : null,
  377.             child: new Container(
  378.               width: 70.0,
  379.               height: 70.0,
  380.               margin: new EdgeInsets.only(left: 10.0),
  381.               decoration: new BoxDecoration(
  382.                 image: new DecorationImage(
  383.                     image: profileImgUrl != null
  384.                         ? new NetworkImage(profileImgUrl)
  385.                         : new NetworkImage("http://i.imgur.com/QSev0hg.jpg"),
  386.                     fit: BoxFit.cover),
  387.                 borderRadius: new BorderRadius.all(new Radius.circular(50.0)),
  388.                 boxShadow: <BoxShadow>[
  389.                   new BoxShadow(
  390.                       color: Colors.black26,
  391.                       blurRadius: 5.0,
  392.                       spreadRadius: 1.0),
  393.                 ],
  394.               ),
  395.             ),
  396.           ),
  397.           new Padding(padding: const EdgeInsets.only(right: 20.0)),
  398.           new Column(
  399.             crossAxisAlignment: CrossAxisAlignment.start,
  400.             children: <Widget>[
  401.               new Text(fullName != null ? fullName : fullName = "Your Name",
  402.                   style: mainTextStyle),
  403.               new Text(email != null ? email : email = "Your Email",
  404.                   style: subTextStyle),
  405.               new Text(phone != null ? phone : phone = "Your Number",
  406.                   style: subTextStyle),
  407.             ],
  408.           ),
  409.         ],
  410.       ),
  411.     );
  412.   }
  413.  
  414.   var progress = new ProgressBar(
  415.     backgroundColor: Colors.black12,
  416.     color: Colors.white,
  417.     containerColor: MyApp.appColors,
  418.     borderRadius: 5.0,
  419.     text: 'Creating Account....',
  420.   );
  421.  
  422.   void showInSnackBar(String value) {
  423.     scaffoldKey.currentState.showSnackBar(new SnackBar(
  424.       content: new Text(value),
  425.     ));
  426.   }
  427.  
  428.   Future createUserAccount() async {
  429.     if (_nameController.text == "") {
  430.       showInSnackBar("Name cannot be empty");
  431.       return;
  432.     }
  433.  
  434.     if (_mobileController.text == "") {
  435.       showInSnackBar("Mobile cannot be empty");
  436.       return;
  437.     }
  438.  
  439.     if (_emailController.text == "") {
  440.       showInSnackBar("Email cannot be empty");
  441.       return;
  442.     }
  443.  
  444.     if (_passController.text == "") {
  445.       showInSnackBar("Password cannot be empty");
  446.       return;
  447.     }
  448.  
  449.     showDialog(
  450.       context: context,
  451.       child: progress,
  452.     );
  453.  
  454.     try {
  455.       await _auth
  456.           .createUserWithEmailAndPassword(
  457.               email: _emailController.text, password: _passController.text)
  458.           .then((loggedUser) {
  459.         Map userMap = new Map();
  460.         userMap[AppData.fullName] = _nameController.text;
  461.         userMap[AppData.phoneNumber] = _mobileController.text;
  462.         userMap[AppData.email] = _emailController.text;
  463.         userMap[AppData.password] = _passController.text;
  464.         userMap[AppData.profileImgURL] = loggedUser.photoUrl;
  465.         userMap[AppData.deliveryAddress] = "";
  466.         userMap[AppData.userID] = loggedUser.uid;
  467.         userRef.child(loggedUser.uid).set(userMap);
  468.         user = loggedUser;
  469.  
  470.         _auth.signInWithEmailAndPassword(
  471.             email: _emailController.text, password: _passController.text);
  472. //        Navigator.of(context).pop();
  473. //        Navigator.of(context).pop();
  474.       });
  475.     } on PlatformException catch (e) {
  476.       Navigator.of(context).pop();
  477.       showInSnackBar(e.message);
  478.     }
  479.  
  480. //    setState(() {});
  481. //
  482. //    if (user != null) {
  483. //      UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
  484. //      userUpdateInfo.photoUrl = "";
  485. //      userUpdateInfo.displayName = _nameController.text;
  486. //      _auth.updateProfile(userUpdateInfo);
  487. //    }
  488.   }
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement