Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.91 KB | None | 0 0
  1.  
  2. class LoginPage extends StatefulWidget {
  3.   LoginPage({this.auth});
  4.   final BaseAuth auth;
  5.  
  6.   @override
  7.   State<StatefulWidget> createState() => _LoginPageState();
  8. }
  9.  
  10. enum FormType {
  11.   login,
  12.   register
  13. }
  14.  
  15. class _LoginPageState extends State<LoginPage> {
  16.  
  17.   final formKey = GlobalKey<FormState>();
  18.  
  19.   String _email;
  20.   String _password;
  21.   FormType _formType = FormType.login;
  22.  
  23.   bool validateAndSave() {
  24.     final form = formKey.currentState;
  25.     if (form.validate()) {
  26.       form.save();
  27.       return true;
  28.     }
  29.     return false;
  30.   }
  31.  
  32. void validateAndSubmit() async {
  33.     if (validateAndSave()) {
  34.         try {
  35.       if (_formType == FormType.login){
  36.       String userId = await widget.auth.signInWithEmailAndPassword(_email, _password);
  37.           print('Signed in: $userId ');
  38.       } else{
  39.         String userId = await widget.auth.createUserWithEmailAndPassword(_email , _password);
  40.         print('Registered user: $userId');
  41.       }
  42.         }
  43.         catch (e) {
  44.           print('Error: $e');
  45.         }
  46.     }
  47. }
  48.  
  49. void moveToRegister() {
  50.  // formKey.currentState.reset();
  51.   setState(() {
  52.     _formType = FormType.register;
  53. });
  54. }
  55. void moveToLogin() {
  56.   //formKey.currentState.reset();
  57.     setState(() {
  58.     _formType = FormType.login;
  59. });
  60. }
  61.  
  62.   @override
  63.   Widget build(BuildContext context) {
  64.     return Scaffold(
  65.       appBar: AppBar(
  66.         title: Text('Magic Minion'),
  67.       ),
  68.       body: Container(
  69.         padding: EdgeInsets.all(16.0),
  70.         child: Form(
  71.           child: Column(
  72.             crossAxisAlignment: CrossAxisAlignment.stretch,
  73.             children: buildiInputs() + buildSubmitButtons(),
  74.           ),
  75.         ),
  76.       ),
  77.     );
  78.   }
  79.  
  80.   List<Widget> buildiInputs(){
  81.   return[
  82.     TextFormField(
  83.       decoration: InputDecoration(labelText: 'Email'),
  84.       validator: (value) =>
  85.       value.isEmpty
  86.           ? 'Email can\t be empty'
  87.           : null,
  88.       onSaved: (value) => _email = value,
  89.     ),
  90.     TextFormField(
  91.       decoration: InputDecoration(labelText: 'Password'),
  92.       obscureText: true,
  93.       validator: (value) =>
  94.       value.isEmpty
  95.           ? 'Email can\t be empty'
  96.           : null,
  97.       onSaved: (value) => _password = value,
  98.     ),
  99.   ];
  100.   }
  101.   List<Widget> buildSubmitButtons(){
  102.     if (_formType == FormType.login) {
  103.     return[
  104.       Divider(),
  105.       RaisedButton(
  106.         child: Text('Login', style: TextStyle(fontSize: 20.0),),
  107.         onPressed: validateAndSubmit,
  108.       ),
  109.       FlatButton(
  110.         child: Text('Create an account', style: TextStyle(fontSize: 20.0)),
  111.         onPressed: moveToRegister,
  112.       )
  113.     ];
  114.     } else {return[
  115.       RaisedButton(
  116.         child: Text('Create an account', style: TextStyle(fontSize: 20.0),),
  117.         onPressed: validateAndSubmit,
  118.       ),
  119.       FlatButton(
  120.         child: Text('Have an account? Login', style: TextStyle(fontSize: 20.0)),
  121.         onPressed: moveToLogin,
  122.       )
  123.     ];
  124.  
  125.     }
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement