Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.58 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:magic_minion_2/auth_provider.dart';
  3.  
  4.  
  5. class EmailFieldValidator {
  6.   static String validate(String value) {
  7.     return value.isEmpty ? 'Email can\'t be empty' : null;
  8.   }
  9. }
  10.  
  11. class PasswordFieldValidator {
  12.   static String validate(String value) {
  13.     return value.isEmpty ? 'Password can\'t be empty' : null;
  14.   }
  15. }
  16.  
  17. class LoginPage extends StatefulWidget {
  18.   LoginPage({this.onSignedIn});
  19.   final VoidCallback onSignedIn;
  20.  
  21.   @override
  22.   State<StatefulWidget> createState() => _LoginPageState();
  23. }
  24.  
  25. enum FormType {
  26.   login,
  27.   register
  28. }
  29.  
  30. class _LoginPageState extends State<LoginPage> {
  31.   final formKey = GlobalKey<FormState>();
  32.  
  33.   String _email;
  34.   String _password;
  35.   FormType _formType = FormType.login;
  36.  
  37.   bool validateAndSave() {
  38.     final form = formKey.currentState;
  39.     if (form.validate()) {
  40.       form.save();
  41.       return true;
  42.     }
  43.     return false;
  44.   }
  45.  
  46.   void validateAndSubmit() async {
  47.     if (validateAndSave()) {
  48.       try {
  49.         var auth = AuthProvider.of(context).auth;
  50.         if (_formType == FormType.login) {
  51.           String userId =
  52.               await auth.signInWithEmailAndPassword(_email, _password);
  53.           print('Signed in: $userId');
  54.         } else {
  55.           String userId = await auth
  56.               .createUserWithEmailAndPassword(_email, _password);
  57.           print('Registered user: $userId');
  58.         }
  59.         widget.onSignedIn();
  60.       } catch (e) {
  61.         print('Error: $e');
  62.       }
  63.     }
  64.   }
  65.  
  66.   void moveToRegister() {
  67.     formKey.currentState.reset();
  68.     setState(() {
  69.       _formType = FormType.register;
  70.     });
  71.   }
  72.  
  73.   void moveToLogin() {
  74.     formKey.currentState.reset();
  75.     setState(() {
  76.       _formType = FormType.login;
  77.     });
  78.   }
  79.  
  80.   @override
  81.   Widget build(BuildContext context) {
  82.     return Scaffold(
  83.         appBar: AppBar(
  84.           title: Text('Magic Minion'),
  85.         ),
  86.         body: Container(
  87.             padding: EdgeInsets.all(16.0),
  88.             child: Form(
  89.               key: formKey,
  90.               child: Column(
  91.                 crossAxisAlignment: CrossAxisAlignment.stretch,
  92.                 children:
  93.                 if (_formType == FormType.register) {
  94.                   <Widget>[
  95.                   buildInputs() +profileInfo()+ buildSubmitButtons()
  96.                   ]
  97.                 } else {
  98.                    <Widget>[
  99.                   buildInputs() + buildSubmitButtons()
  100.                    ]
  101.                 }
  102.                 ],
  103.             )
  104.           )
  105.         ),
  106.     );
  107.   }
  108.  
  109.   List<Widget> buildInputs() {
  110.     return [
  111.       TextFormField(
  112.         key: Key('email'),
  113.         decoration: InputDecoration(labelText: 'Email'),
  114.         validator: EmailFieldValidator.validate,
  115.         onSaved: (value) => _email = value,
  116.       ),
  117.       TextFormField(
  118.         key: Key('password'),
  119.         decoration: InputDecoration(labelText: 'Password'),
  120.         obscureText: true,
  121.         validator: PasswordFieldValidator.validate,
  122.         onSaved: (value) => _password = value,
  123.       ),  
  124. //add a if statement based on if form type is set to register then add Name and DCI textformfields.
  125.     ];
  126.   }
  127.  
  128.  List<Widget> profileInfo() {
  129.     return [
  130.       TextFormField(
  131.         key: Key('name'),
  132.         decoration: InputDecoration(labelText: 'Name'),
  133.       ),
  134.       TextFormField(
  135.         key: Key('dci'),
  136.         decoration: InputDecoration(labelText: 'Wizard/s DCI#'),
  137.       ),  
  138. //add a if statement based on if form type is set to register then add Name and DCI textformfields.
  139.     ];
  140.   }
  141.  
  142.   List<Widget> buildSubmitButtons() {
  143.     if (_formType == FormType.login) {
  144.       return [
  145.         RaisedButton(
  146.           key: Key('signIn'),
  147.            color: Theme.of(context).accentColor,
  148.           splashColor: Colors.deepOrange,        
  149.           child: Text('Login', style: TextStyle(fontSize: 20.0)),
  150.           onPressed: validateAndSubmit,
  151.         ),
  152.         FlatButton(
  153.           child: Text('Create an account',
  154.               style: TextStyle(fontSize: 20.0)),
  155.           onPressed: moveToRegister,
  156.         ),
  157.       ];
  158.     } else {
  159.       return [
  160.         RaisedButton(
  161.           color: Theme.of(context).accentColor,
  162.           splashColor: Colors.deepOrange,
  163.           child: Text('Register',
  164.               style: TextStyle(fontSize: 20.0)),
  165.           onPressed: validateAndSubmit,
  166.         ),
  167.         FlatButton(
  168.           child: Text('Have an account? Login',
  169.               style: TextStyle(fontSize: 20.0)),
  170.           onPressed: moveToLogin,
  171.         ),
  172.       ];
  173.     }
  174.   }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement