Advertisement
rachmadi

authentication.dart

Mar 13th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.08 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:firebase_auth/firebase_auth.dart';
  3. import 'package:firebase_database/firebase_database.dart';
  4. import 'package:bencana/main.dart';
  5.  
  6. class AuthenticationPage extends StatefulWidget {
  7.   @override
  8.   _AuthenticationPageState createState() => _AuthenticationPageState();
  9. }
  10.  
  11. enum FormType { signIn, signUp }
  12. enum AuthStatus { signIn, notSignIn }
  13.  
  14. final userRef = FirebaseDatabase.instance.reference().child('user');
  15.  
  16. class _AuthenticationPageState extends State<AuthenticationPage> {
  17.   AuthStatus _authStatus = AuthStatus.notSignIn;
  18.  
  19.   final formKey = GlobalKey<FormState>();
  20.   FormType _formType = FormType.signIn;
  21.  
  22.   String _email;
  23.   String _password;
  24.   String _fullName;
  25.  
  26.   bool validateAndSave() {
  27.     final form = formKey.currentState;
  28.     form.save();
  29.     if (form.validate()) {
  30.       print('Form is valid. Email: $_email, Password: $_password');
  31.       return true;
  32.     } else {
  33.       print('Form is invalid. Email: $_email, Password: $_password');
  34.       return false;
  35.     }
  36.   }
  37.  
  38.   void validateAndSubmit() async {
  39.     if (validateAndSave()) {
  40.       try {
  41.         if (_formType == FormType.signIn) {
  42.           formKey.currentState.reset();
  43.           // Buat proses sign in
  44.           FirebaseUser user = await FirebaseAuth.instance
  45.               .signInWithEmailAndPassword(email: _email, password: _password);
  46.           print('user: ${user.uid}');
  47.           this.setState(() {
  48.             _authStatus = AuthStatus.signIn;
  49.           });
  50.         } else {
  51.           formKey.currentState.reset();
  52.           // Buat proses sign up
  53.           FirebaseUser user = await FirebaseAuth.instance
  54.               .createUserWithEmailAndPassword(
  55.                   email: _email, password: _password);
  56.           print('user: ${user.uid}');
  57.           userRef
  58.               .child(user.uid)
  59.               .set({'fullName': _fullName, 'email': _email}).then((_) {
  60.             this.setState(() {
  61.               _authStatus = AuthStatus.signIn;
  62.             });
  63.           });
  64.         }
  65.       } catch (e) {
  66.         print('Error: $e');
  67.       }
  68.     }
  69.   }
  70.  
  71.   void moveToSignIn() {
  72.     setState(() {
  73.       _formType = FormType.signIn;
  74.     });
  75.   }
  76.  
  77.   void moveToSignUp() {
  78.     setState(() {
  79.       _formType = FormType.signUp;
  80.     });
  81.   }
  82.  
  83.   List<Widget> buildInputs() {
  84.     if (_formType == FormType.signIn) {
  85.       return [
  86.         SizedBox(
  87.           height: 80.0,
  88.         ),
  89.         Container(
  90.           width: 150,
  91.           height: 150,
  92.           decoration: BoxDecoration(
  93.             color: Colors.teal[200],
  94.             shape: BoxShape.circle,
  95.             image: DecorationImage(
  96.                 fit: BoxFit.fill,
  97.                 image: AssetImage('images/bencana_logo.png'))
  98.           ),
  99.         ),
  100.  
  101.         SizedBox(
  102.           height: 20.0,
  103.         ),
  104.         TextFormField(
  105.           decoration: InputDecoration(labelText: 'Email'),
  106.           keyboardType: TextInputType.emailAddress,
  107.           validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  108.           onSaved: (value) => _email = value,
  109.         ),
  110.         TextFormField(
  111.           decoration: InputDecoration(
  112.             labelText: 'Password',
  113.           ),
  114.           obscureText: true,
  115.           validator: (value) => value.length < 6 ? 'Minimum 6 character' : null,
  116.           onSaved: (value) => _password = value,
  117.         ),
  118.       ];
  119.     } else {
  120.       return [
  121.         SizedBox(
  122.           height: 80.0,
  123.         ),
  124.         Container(
  125.           child: Image.asset('images/bencana_logo.png'),
  126.           width: 200.0,
  127.           height: 200.0,
  128.           color: Colors.white,
  129.         ),
  130.         SizedBox(
  131.           height: 20.0,
  132.         ),
  133.         TextFormField(
  134.           decoration: InputDecoration(labelText: 'Email'),
  135.           keyboardType: TextInputType.emailAddress,
  136.           validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
  137.           onSaved: (value) => _email = value,
  138.         ),
  139.         TextFormField(
  140.           decoration: InputDecoration(
  141.             labelText: 'Password',
  142.           ),
  143.           obscureText: true,
  144.           validator: (value) => value.length < 6 ? 'Minimum 6 character' : null,
  145.           onSaved: (value) => _password = value,
  146.         ),
  147.         TextFormField(
  148.           decoration: InputDecoration(labelText: 'Full Name'),
  149.           validator: (value) =>
  150.               value.isEmpty ? 'Full name can\'t be empty' : null,
  151.           onSaved: (value) => _fullName = value,
  152.         ),
  153.       ];
  154.     }
  155.   }
  156.  
  157.   List<Widget> buildButtons() {
  158.     if (_formType == FormType.signIn) {
  159.       return [
  160.         SizedBox(
  161.           height: 10,
  162.         ),
  163.         RaisedButton(
  164.           child: Text('Sign In'),
  165.           color: Colors.teal,
  166.           textColor: Colors.white,
  167.           onPressed: validateAndSubmit,
  168.         ),
  169.         FlatButton(
  170.           onPressed: moveToSignUp,
  171.           child: Text('Create a new account'),
  172.           textColor: Colors.teal,
  173.         ),
  174.       ];
  175.     } else {
  176.       return [
  177.         SizedBox(
  178.           height: 10,
  179.         ),
  180.         RaisedButton(
  181.           child: Text('Sign Up'),
  182.           color: Colors.teal,
  183.           textColor: Colors.white,
  184.           onPressed: validateAndSubmit,
  185.         ),
  186.         FlatButton(
  187.           onPressed: moveToSignIn,
  188.           child: Text('Already have account? Sign in!'),
  189.           textColor: Colors.teal,
  190.         ),
  191.       ];
  192.     }
  193.   }
  194.  
  195.   @override
  196.   Widget build(BuildContext context) {
  197.     switch (_authStatus) {
  198.       case AuthStatus.signIn:
  199.         return DashboardScreen();
  200.       case AuthStatus.notSignIn:
  201.         return Scaffold(
  202.           body: Container(
  203.             child: Padding(
  204.               padding: EdgeInsets.all(16.0),
  205.               child: Form(
  206.                 key: formKey,
  207.                 child: ListView(
  208.                   children: <Widget>[
  209.                     Column(
  210.                       children: buildInputs() + buildButtons(),
  211.                     ),
  212.                   ],
  213.                 ),
  214.               ),
  215.             ),
  216.           ),
  217.         );
  218.     }
  219.   }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement