11import 'package:flutter/material.dart' ;
2- import 'package:firebase_auth/firebase_auth.dart' ;
32import 'auth.dart' ;
43
54class LoginPage extends StatefulWidget {
65 LoginPage ({this .auth, this .onSignedIn});
76 final BaseAuth auth;
87 final VoidCallback onSignedIn;
9-
8+
109 @override
1110 State <StatefulWidget > createState () => new _LoginPageState ();
1211}
1312
1413enum FormType {
1514 login,
16- register
15+ register,
1716}
1817
1918class _LoginPageState extends State <LoginPage > {
20-
2119 final formKey = new GlobalKey <FormState >();
2220
2321 String _email;
@@ -35,17 +33,18 @@ class _LoginPageState extends State<LoginPage> {
3533
3634 void validateAndSubmit () async {
3735 if (validateAndSave ()) {
38- try {
36+ try {
3937 if (_formType == FormType .login) {
40- String userId = await widget.auth.signInWithEmailAndPassword (_email, _password);
38+ String userId =
39+ await widget.auth.signInWithEmailAndPassword (_email, _password);
4140 print ('Signed in: $userId ' );
4241 } else {
43- String userId = await widget.auth.createUserWithEmailAndPassword (_email, _password);
42+ String userId = await widget.auth
43+ .createUserWithEmailAndPassword (_email, _password);
4444 print ('Registered user: $userId ' );
4545 }
4646 widget.onSignedIn ();
47- }
48- catch (e) {
47+ } catch (e) {
4948 print ('Error: $e ' );
5049 }
5150 }
@@ -66,63 +65,64 @@ class _LoginPageState extends State<LoginPage> {
6665 }
6766
6867 @override
69- Widget build (BuildContext context) {
70- return new Scaffold (
68+ Widget build (BuildContext context) {
69+ return new Scaffold (
7170 appBar: new AppBar (
7271 title: new Text ('Flutter login demo' ),
7372 ),
7473 body: new Container (
75- padding: EdgeInsets .all (16.0 ),
76- child: new Form (
77- key: formKey,
78- child: new Column (
79- crossAxisAlignment: CrossAxisAlignment .stretch,
80- children: buildInputs () + buildSubmitButtons (),
81- ),
82- )
83- )
84- );
85- }
74+ padding: EdgeInsets .all (16.0 ),
75+ child: new Form (
76+ key: formKey,
77+ child: new Column (
78+ crossAxisAlignment: CrossAxisAlignment .stretch,
79+ children: buildInputs () + buildSubmitButtons (),
80+ ),
81+ )));
82+ }
83+
84+ List <Widget > buildInputs () {
85+ return [
86+ new TextFormField (
87+ decoration: new InputDecoration (labelText: 'Email' ),
88+ validator: (value) => value.isEmpty ? 'Email can\' t be empty' : null ,
89+ onSaved: (value) => _email = value,
90+ ),
91+ new TextFormField (
92+ decoration: new InputDecoration (labelText: 'Password' ),
93+ obscureText: true ,
94+ validator: (value) => value.isEmpty ? 'Password can\' t be empty' : null ,
95+ onSaved: (value) => _password = value,
96+ ),
97+ ];
98+ }
8699
87- List <Widget > buildInputs () {
100+ List <Widget > buildSubmitButtons () {
101+ if (_formType == FormType .login) {
88102 return [
89- new TextFormField (
90- decoration: new InputDecoration (labelText: 'Email' ),
91- validator: (value) => value.isEmpty ? 'Email can\' t be empty' : null ,
92- onSaved: (value) => _email = value,
103+ new RaisedButton (
104+ child: new Text ('Login' , style: new TextStyle (fontSize: 20.0 )),
105+ onPressed: validateAndSubmit,
93106 ),
94- new TextFormField (
95- decoration: new InputDecoration (labelText: 'Password' ),
96- obscureText: true ,
97- validator: (value) => value.isEmpty ? 'Password can\' t be empty' : null ,
98- onSaved: (value) => _password = value,
107+ new FlatButton (
108+ child: new Text ('Create an account' ,
109+ style: new TextStyle (fontSize: 20.0 )),
110+ onPressed: moveToRegister,
111+ ),
112+ ];
113+ } else {
114+ return [
115+ new RaisedButton (
116+ child: new Text ('Create an account' ,
117+ style: new TextStyle (fontSize: 20.0 )),
118+ onPressed: validateAndSubmit,
119+ ),
120+ new FlatButton (
121+ child: new Text ('Have an account? Login' ,
122+ style: new TextStyle (fontSize: 20.0 )),
123+ onPressed: moveToLogin,
99124 ),
100125 ];
101126 }
102-
103- List <Widget > buildSubmitButtons () {
104- if (_formType == FormType .login) {
105- return [
106- new RaisedButton (
107- child: new Text ('Login' , style: new TextStyle (fontSize: 20.0 )),
108- onPressed: validateAndSubmit,
109- ),
110- new FlatButton (
111- child: new Text ('Create an account' , style: new TextStyle (fontSize: 20.0 )),
112- onPressed: moveToRegister,
113- ),
114- ];
115- } else {
116- return [
117- new RaisedButton (
118- child: new Text ('Create an account' , style: new TextStyle (fontSize: 20.0 )),
119- onPressed: validateAndSubmit,
120- ),
121- new FlatButton (
122- child: new Text ('Have an account? Login' , style: new TextStyle (fontSize: 20.0 )),
123- onPressed: moveToLogin,
124- ),
125- ];
126- }
127- }
128- }
127+ }
128+ }
0 commit comments