Advertisement
rachmadi

signin_fascia_flutter

Sep 14th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.76 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:firebase_auth/firebase_auth.dart';
  4. import 'package:flutter/material.dart';
  5.  
  6. import 'home.dart';
  7.  
  8. class SignInPage extends StatefulWidget {
  9.   @override
  10.   _SignInPageState createState() => new _SignInPageState();
  11. }
  12.  
  13. enum FormType { signIn, signUp }
  14. enum AuthStatus { notSignIn, signIn }
  15.  
  16. class _SignInPageState extends State<SignInPage> {
  17.   final formKey = new GlobalKey<FormState>();
  18.   AuthStatus _authStatus = AuthStatus.notSignIn;
  19.  
  20.   initState() {
  21.     super.initState();
  22.     getCurrentUser().then((userId) {
  23.       setState(() {
  24.         _authStatus = userId == null ? AuthStatus.notSignIn : AuthStatus.signIn;
  25.       });
  26.     });
  27.   }
  28.  
  29.   Future<String> getCurrentUser() async {
  30.     try {
  31.       FirebaseUser user = await FirebaseAuth.instance.currentUser();
  32.       return user.uid;
  33.     } catch (e) {
  34.       print('Error: $e');
  35.       return null;
  36.     }
  37.   }
  38.  
  39.   String _email;
  40.   String _password;
  41.   FormType _formType = FormType.signIn;
  42.  
  43.   bool validateAndSave() {
  44.     final form = formKey.currentState;
  45.  
  46.     if (form.validate()) {
  47.       form.save();
  48.       return true;
  49.     } else {
  50.       return false;
  51.     }
  52.   }
  53.  
  54.   void validateAndSubmit() async {
  55.     if (validateAndSave()) {
  56.       try {
  57.         if (_formType == FormType.signIn) {
  58.           FirebaseUser user = await FirebaseAuth.instance
  59.               .signInWithEmailAndPassword(email: _email, password: _password);
  60.           print('User: ${user.uid}');
  61.           formKey.currentState.reset();
  62.           this.setState(() {
  63.             _authStatus = AuthStatus.signIn;
  64.           });
  65.         } else {
  66.           FirebaseUser user = await FirebaseAuth.instance
  67.               .createUserWithEmailAndPassword(
  68.                   email: _email, password: _password);
  69.           print('User: ${user.uid}');
  70.           formKey.currentState.reset();
  71.           this.setState(() {
  72.             _authStatus = AuthStatus.signIn;
  73.           });
  74.         }
  75.       } catch (e) {
  76.         print("Error: $e");
  77.       }
  78.     }
  79.   }
  80.  
  81.   void moveToSignUp() {
  82.     setState(() {
  83.       _formType = FormType.signUp;
  84.     });
  85.   }
  86.  
  87.   void moveToSignIn() {
  88.     setState(() {
  89.       _formType = FormType.signIn;
  90.     });
  91.   }
  92.  
  93.   @override
  94.   Widget build(BuildContext context) {
  95.     switch (_authStatus) {
  96.       case AuthStatus.signIn:
  97.         return new HomePage();
  98.       case AuthStatus.notSignIn:
  99.         return new Scaffold(
  100.           body: new Container(
  101.             height: double.infinity,
  102.             decoration: new BoxDecoration(color: Colors.teal),
  103.             child: new ListView(
  104.               children: <Widget>[
  105.                 new Stack(
  106.                   children: <Widget>[
  107.                     new Container(
  108.                       height: 380.0,
  109.                       margin: const EdgeInsets.only(
  110.                         top: 150.0,
  111.                         left: 20.0,
  112.                         right: 20.0,
  113.                       ),
  114.                       child: new Container(
  115.                         height: 200.0,
  116.                         padding: const EdgeInsets.only(
  117.                           top: 80.0,
  118.                           left: 16.0,
  119.                           right: 16.0,
  120.                           bottom: 20.0,
  121.                         ),
  122.                         decoration: new BoxDecoration(
  123.                           color: Colors.white,
  124.                           borderRadius: new BorderRadius.circular(10.0),
  125.                         ),
  126.                         child: new Form(
  127.                           key: formKey,
  128.                           child: new Column(
  129.                             children: buildInputs() + buildSubmitButtons(),
  130.                           ),
  131.                         ),
  132.                       ),
  133.                     ),
  134.                     new Column(
  135.                       crossAxisAlignment: CrossAxisAlignment.stretch,
  136.                       children: <Widget>[
  137.                         new Container(
  138.                           padding: const EdgeInsets.only(
  139.                             top: 75.0,
  140.                           ),
  141.                           child: new CircleAvatar(
  142.                             child: new Image.asset(
  143.                               "images/fascia_icon.png",
  144.                               width: 150.0,
  145.                               height: 150.0,
  146.                             ),
  147.                             radius: 78.0,
  148.                             backgroundColor: Colors.teal,
  149.                           ),
  150.                         ),
  151.                       ],
  152.                     ),
  153.                   ],
  154.                 ),
  155.               ],
  156.             ),
  157.           ),
  158.         );
  159.     }
  160.     return null;
  161.   }
  162.  
  163.   List<Widget> buildInputs() {
  164.     return [
  165.       new TextFormField(
  166.         decoration: new InputDecoration(
  167.           labelText: 'Email',
  168.         ),
  169.         keyboardType: TextInputType.emailAddress,
  170.         validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  171.         onSaved: (value) => _email = value,
  172.       ),
  173.       new TextFormField(
  174.         decoration: new InputDecoration(
  175.           labelText: 'Password',
  176.         ),
  177.         obscureText: true,
  178.         validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
  179.         onSaved: (value) => _password = value,
  180.       ),
  181.     ];
  182.   }
  183.  
  184.   List<Widget> buildSubmitButtons() {
  185.     if (_formType == FormType.signIn) {
  186.       return [
  187.         new Padding(
  188.           padding: const EdgeInsets.only(
  189.             top: 20.0,
  190.           ),
  191.         ),
  192.         new RaisedButton(
  193.           child: new Text(
  194.             'Sign In',
  195.             style: new TextStyle(
  196.               color: Colors.white,
  197.             ),
  198.           ),
  199.           color: Colors.teal,
  200.           splashColor: Colors.yellowAccent,
  201.           onPressed: validateAndSubmit,
  202.         ),
  203.         new Padding(
  204.           padding: const EdgeInsets.only(
  205.             top: 20.0,
  206.           ),
  207.         ),
  208.         new FlatButton(
  209.           child: new Text('Create new account'),
  210.           onPressed: moveToSignUp,
  211.         )
  212.       ];
  213.     } else {
  214.       return [
  215.         new Padding(
  216.           padding: const EdgeInsets.only(
  217.             top: 20.0,
  218.           ),
  219.         ),
  220.         new RaisedButton(
  221.           child: new Text(
  222.             'Sign Up',
  223.             style: new TextStyle(
  224.               color: Colors.white,
  225.             ),
  226.           ),
  227.           color: Colors.teal,
  228.           splashColor: Colors.yellowAccent,
  229.           onPressed: validateAndSubmit,
  230.         ),
  231.         new Padding(
  232.           padding: const EdgeInsets.only(
  233.             top: 20.0,
  234.           ),
  235.         ),
  236.         new FlatButton(
  237.           child: new Text('Have an account? Sign In'),
  238.           onPressed: moveToSignIn,
  239.         )
  240.       ];
  241.     }
  242.   }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement