Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 12.82 KB | None | 0 0
  1. import 'package:blogging_app/Firebase/google_signIn.dart';
  2. import 'package:blogging_app/Firebase/main_tgglePage.dart';
  3. import 'package:blogging_app/Other_screens/home_page.dart';
  4. import 'package:firebase_auth/firebase_auth.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:font_awesome_flutter/font_awesome_flutter.dart';
  7.  
  8. class LoginScreen extends StatefulWidget {
  9.   final GoogleSignin googleSignin;
  10.   LoginScreen({
  11.     this.googleSignin,
  12.   });
  13.  
  14.   @override
  15.   _LoginScreenState createState() => _LoginScreenState();
  16. }
  17.  
  18. class _LoginScreenState extends State<LoginScreen> {
  19.   String errorText = "";
  20.   final scaffoldKey = GlobalKey<ScaffoldState>();
  21.   FirebaseUser userF;
  22.   String state;
  23.   ToggleStatus toggleStatus = ToggleStatus.unknown;
  24.   @override
  25.   void initState() {
  26.     widget.googleSignin.currentUser().then((user) {
  27.       setState(() {
  28.         userF = user;
  29.         user?.uid != null
  30.             ? toggleStatus = ToggleStatus.signedIn
  31.             : toggleStatus = ToggleStatus.signedOut;
  32.         user?.photoUrl != null ? state = 'google' : state = 'firebase';
  33.       });
  34.     });
  35.  
  36.     super.initState();
  37.   }
  38.  
  39.   @override
  40.   Widget build(BuildContext context) {
  41.     return toggleStatus == ToggleStatus.signedIn
  42.         ? HomePage(
  43.             state: state,
  44.             user: userF,
  45.             googleSignin: widget.googleSignin,
  46.           )
  47.         : Scaffold(
  48.             backgroundColor: Colors.white,
  49.             key: scaffoldKey,
  50.             body: Hero(
  51.                 tag: 'nepal',
  52.                 child: Container(
  53.                     decoration: BoxDecoration(
  54.                       image: DecorationImage(
  55.                           image: AssetImage(
  56.                             "assets/images/bgLight.png",
  57.                           ),
  58.                           fit: BoxFit.contain),
  59.                     ),
  60.                     child: ListView(
  61.                       physics: BouncingScrollPhysics(),
  62.                       reverse: true,
  63.                       children: <Widget>[
  64.                         Padding(
  65.                           padding: const EdgeInsets.only(bottom: 60.0),
  66.                           child: LoginPage(
  67.                             scaffoldKey: scaffoldKey,
  68.                             googleSignin: widget.googleSignin,
  69.                             userF: userF,
  70.                           ),
  71.                         ),
  72.                       ],
  73.                     ))));
  74.   }
  75. }
  76.  
  77. class LoginPage extends StatefulWidget {
  78.   final GlobalKey scaffoldKey;
  79.   final GoogleSignin googleSignin;
  80.   final FirebaseUser userF;
  81.  
  82.   LoginPage({
  83.     this.scaffoldKey,
  84.     this.googleSignin,
  85.     this.userF,
  86.   });
  87.  
  88.   @override
  89.   _LoginPageState createState() => _LoginPageState();
  90. }
  91.  
  92. class _LoginPageState extends State<LoginPage> {
  93.   String errorText = "";
  94.   final TextEditingController _pwController = TextEditingController();
  95.   String _email = "";
  96.   String _password = "";
  97.  
  98.   final _formKey = GlobalKey<FormState>();
  99.  
  100.   @override
  101.   Widget build(BuildContext context) {
  102.     return Column(children: <Widget>[
  103.       Row(
  104.         mainAxisAlignment: MainAxisAlignment.center,
  105.         children: <Widget>[
  106.           _raisedButton("  SignIn    ", Icons.lock_open, () {
  107.             _showForm(context, 0);
  108.           }),
  109.           SizedBox(
  110.             width: 30.0,
  111.           ),
  112.           _raisedButton(" SignUp    ", Icons.note_add, () {
  113.             _showForm(context, 1);
  114.           }),
  115.         ],
  116.       ),
  117.       SizedBox(height: 30.0),
  118.       _outlineButton(
  119.           "   Continue with Google",
  120.           Icon(
  121.             FontAwesomeIcons.google,
  122.             color: Colors.red,
  123.           ), () async {
  124.         try {
  125.           await widget.googleSignin.signIn().then((user) {
  126.             Navigator.pushReplacement(
  127.                 context,
  128.                 MaterialPageRoute(
  129.                     builder: (context) => HomePage(
  130.                           googleSignin: widget.googleSignin,
  131.                           state: 'google',
  132.                           user: user,
  133.                         )));
  134.           });
  135.         } catch (e) {
  136.           print(e.toString());
  137.         }
  138.       })
  139.     ]);
  140.   }
  141.  
  142.   Widget _raisedButton(String label, IconData icons, ontap()) {
  143.     return SizedBox(
  144.       height: 50.0,
  145.       child: RaisedButton.icon(
  146.         icon: Icon(
  147.           icons,
  148.           color: Colors.white,
  149.         ),
  150.         onPressed: () {
  151.           ontap();
  152.         },
  153.         label: Text(
  154.           label,
  155.         ),
  156.         shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
  157.       ),
  158.     );
  159.   }
  160.  
  161.   Widget _outlineButton(String label, Icon icons, ontap()) {
  162.     return SizedBox(
  163.       height: 55.0,
  164.       child: OutlineButton.icon(
  165.         borderSide: BorderSide(color: Colors.red, width: 2.0),
  166.         highlightedBorderColor: Colors.red,
  167.         disabledBorderColor: Colors.green,
  168.         padding: EdgeInsets.symmetric(horizontal: 25.0),
  169.         icon: icons,
  170.         onPressed: () {
  171.           ontap();
  172.         },
  173.         label: Text(label, style: TextStyle(color: Colors.black)),
  174.         shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
  175.       ),
  176.     );
  177.   }
  178.  
  179.   Future _showForm(BuildContext context, int number) {
  180.     return showDialog(
  181.         context: context,
  182.         builder: (context) {
  183.           return Container(
  184.             child: Dialog(
  185.               child: Container(
  186.                 padding: EdgeInsets.symmetric(vertical: 10.0),
  187.                 decoration: BoxDecoration(
  188.                   borderRadius: BorderRadius.circular(300.0),
  189.                 ),
  190.                 child: Form(
  191.                   key: _formKey,
  192.                   child: ListView(
  193.                     shrinkWrap: true,
  194.                     physics: BouncingScrollPhysics(),
  195.                     padding:
  196.                         EdgeInsets.symmetric(vertical: 30.0, horizontal: 8.0),
  197.                     children: <Widget>[
  198.                       TextFormField(
  199.                           validator: (label) => label.contains("@gmail.com")
  200.                               ? null
  201.                               : "Enter valid email",
  202.                           onSaved: (label) => _email = label,
  203.                           decoration: InputDecoration(
  204.                             hintText: "Email",
  205.                             icon: Icon(
  206.                               Icons.email,
  207.                               color: Colors.grey,
  208.                             ),
  209.                             focusedBorder: UnderlineInputBorder(
  210.                                 borderSide: BorderSide(color: Colors.black)),
  211.                             border: UnderlineInputBorder(),
  212.                             enabledBorder: UnderlineInputBorder(
  213.                               borderSide: BorderSide(color: Colors.grey),
  214.                             ),
  215.                           )),
  216.                       SizedBox(
  217.                         height: 10.0,
  218.                       ),
  219.                       TextFormField(
  220.                           validator: (label) => label.length >= 6
  221.                               ? null
  222.                               : "Try using longer password",
  223.                           onSaved: (password) => _password = password,
  224.                           controller: number == 1 ? _pwController : null,
  225.                           obscureText: true,
  226.                           decoration: InputDecoration(
  227.                             hintText: "Password",
  228.                             icon: Icon(
  229.                               Icons.lock_outline,
  230.                               color: Colors.grey,
  231.                             ),
  232.                             focusedBorder: UnderlineInputBorder(
  233.                                 borderSide: BorderSide(color: Colors.black)),
  234.                             border: UnderlineInputBorder(),
  235.                             enabledBorder: UnderlineInputBorder(
  236.                               borderSide: BorderSide(color: Colors.grey),
  237.                             ),
  238.                           )),
  239.                       number == 1
  240.                           ? TextFormField(
  241.                               obscureText: true,
  242.                               validator: (confirm) =>
  243.                                   confirm == _pwController.text
  244.                                       ? null
  245.                                       : "Password doesnt match",
  246.                               decoration: InputDecoration(
  247.                                 hintText: "Confirm Password",
  248.                                 icon: Icon(
  249.                                   Icons.lock,
  250.                                   color: Colors.grey,
  251.                                 ),
  252.                                 focusedBorder: UnderlineInputBorder(
  253.                                     borderSide:
  254.                                         BorderSide(color: Colors.black)),
  255.                                 border: UnderlineInputBorder(),
  256.                                 enabledBorder: UnderlineInputBorder(
  257.                                   borderSide: BorderSide(color: Colors.grey),
  258.                                 ),
  259.                               ))
  260.                           : Container(),
  261.                       SizedBox(
  262.                         height: 10.0,
  263.                       ),
  264.                       Padding(
  265.                         padding: const EdgeInsets.symmetric(horizontal: 25.0),
  266.                         child: SizedBox(
  267.                           height: 55.0,
  268.                           child: RaisedButton(
  269.                               color: Colors.blue.shade900,
  270.                               shape: RoundedRectangleBorder(
  271.                                   borderRadius: BorderRadius.circular(25.0)),
  272.                               child: Text(
  273.                                   number == 0 ? "LogIn" : "Create account",
  274.                                   style: TextStyle(color: Colors.white)),
  275.                               onPressed: () => _firebaseLogin(0)),
  276.                         ),
  277.                       ),
  278.                     ],
  279.                   ),
  280.                 ),
  281.               ),
  282.             ),
  283.           );
  284.         });
  285.   }
  286.  
  287.   Future _showAlert(String title, String email, int color) {
  288.     return showDialog(
  289.       context: context,
  290.       child: AlertDialog(
  291.         title:
  292.             Center(child: Text(title, style: TextStyle(color: Colors.black))),
  293.         content: Text(email,
  294.             style: TextStyle(
  295.                 color: color == 1 ? Colors.black : Colors.red,
  296.                 fontWeight: FontWeight.bold)),
  297.         actions: <Widget>[
  298.           FlatButton(
  299.             child: Text("OK"),
  300.             onPressed: () {
  301.               Navigator.pop(context);
  302.             },
  303.           )
  304.         ],
  305.       ),
  306.     );
  307.   }
  308.  
  309.   Future _firebaseLogin(int number) async {
  310.     if (_formKey.currentState.validate()) {
  311.       _formKey.currentState.save();
  312.  
  313.       if (number == 0) {
  314.         try {
  315.           await widget.googleSignin
  316.               .firebaseSignIn(_email, _password)
  317.               .then((user) {
  318.             Navigator.pushReplacement(
  319.                 context,
  320.                 MaterialPageRoute(
  321.                     builder: (context) => HomePage(
  322.                           googleSignin: widget.googleSignin,
  323.                           state: 'firebase',
  324.                           user: user,
  325.                         )));
  326.           });
  327.  
  328.           print("LoggedIn: $_email and $_password");
  329.         } catch (e) {
  330.           print(e.toString());
  331.           if (e.toString().contains(
  332.               'The password is invalid or the user does not have a password.')) {
  333.             setState(() {
  334.               errorText = "Incorrect Password";
  335.             });
  336.           }
  337.           if (e.toString().contains(
  338.               'There is no user record corresponding to this identifier. The user may have been deleted.')) {
  339.             setState(() {
  340.               errorText = "No user found corresponding to $_email";
  341.             });
  342.           }
  343.           _showAlert("Error", errorText, 0);
  344.         }
  345.       } else {
  346.         try {
  347.           await widget.googleSignin.firebaseRegister(_email, _password);
  348.           Navigator.pop(context);
  349.           _showAlert(
  350.               "Created user successfully. Now you can login with your account",
  351.               _email,
  352.               1);
  353.  
  354.           print("Registered: $_email and $_password");
  355.         } catch (e) {
  356.           if (e.toString().contains(
  357.               'The email address is already in use by another account.')) {
  358.             setState(() {
  359.               errorText =
  360.                   "Email already in use by another user. Try using another email";
  361.             });
  362.           }
  363.           _showAlert("Error", errorText, 0);
  364.         }
  365.       }
  366.  
  367.       _formKey.currentState.reset();
  368.     } else {
  369.       print("Error");
  370.     }
  371.   }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement