Advertisement
Guest User

Untitled

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