Advertisement
Guest User

Untitled

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