11import 'package:flutter/material.dart' ;
2+ import 'package:login_demo/auth.dart' ;
23import 'package:login_demo/auth_provider.dart' ;
34
4-
55class EmailFieldValidator {
66 static String validate (String value) {
77 return value.isEmpty ? 'Email can\' t be empty' : null ;
@@ -15,7 +15,7 @@ class PasswordFieldValidator {
1515}
1616
1717class LoginPage extends StatefulWidget {
18- LoginPage ({this .onSignedIn});
18+ const LoginPage ({this .onSignedIn});
1919 final VoidCallback onSignedIn;
2020
2121 @override
@@ -28,32 +28,30 @@ enum FormType {
2828}
2929
3030class _LoginPageState extends State <LoginPage > {
31- final formKey = GlobalKey <FormState >();
31+ final GlobalKey < FormState > formKey = GlobalKey <FormState >();
3232
3333 String _email;
3434 String _password;
3535 FormType _formType = FormType .login;
3636
3737 bool validateAndSave () {
38- final form = formKey.currentState;
38+ final FormState form = formKey.currentState;
3939 if (form.validate ()) {
4040 form.save ();
4141 return true ;
4242 }
4343 return false ;
4444 }
4545
46- void validateAndSubmit () async {
46+ Future < void > validateAndSubmit () async {
4747 if (validateAndSave ()) {
4848 try {
49- var auth = AuthProvider .of (context).auth;
49+ final BaseAuth auth = AuthProvider .of (context).auth;
5050 if (_formType == FormType .login) {
51- String userId =
52- await auth.signInWithEmailAndPassword (_email, _password);
51+ final String userId = await auth.signInWithEmailAndPassword (_email, _password);
5352 print ('Signed in: $userId ' );
5453 } else {
55- String userId = await auth
56- .createUserWithEmailAndPassword (_email, _password);
54+ final String userId = await auth.createUserWithEmailAndPassword (_email, _password);
5755 print ('Registered user: $userId ' );
5856 }
5957 widget.onSignedIn ();
@@ -80,62 +78,61 @@ class _LoginPageState extends State<LoginPage> {
8078 @override
8179 Widget build (BuildContext context) {
8280 return Scaffold (
83- appBar: AppBar (
84- title: Text ('Flutter login demo' ),
81+ appBar: AppBar (
82+ title: Text ('Flutter login demo' ),
83+ ),
84+ body: Container (
85+ padding: EdgeInsets .all (16.0 ),
86+ child: Form (
87+ key: formKey,
88+ child: Column (
89+ crossAxisAlignment: CrossAxisAlignment .stretch,
90+ children: buildInputs () + buildSubmitButtons (),
91+ ),
8592 ),
86- body: Container (
87- padding: EdgeInsets .all (16.0 ),
88- child: Form (
89- key: formKey,
90- child: Column (
91- crossAxisAlignment: CrossAxisAlignment .stretch,
92- children: buildInputs () + buildSubmitButtons (),
93- ),
94- )));
93+ ),
94+ );
9595 }
9696
9797 List <Widget > buildInputs () {
98- return [
98+ return < Widget > [
9999 TextFormField (
100100 key: Key ('email' ),
101101 decoration: InputDecoration (labelText: 'Email' ),
102102 validator: EmailFieldValidator .validate,
103- onSaved: (value) => _email = value,
103+ onSaved: (String value) => _email = value,
104104 ),
105105 TextFormField (
106106 key: Key ('password' ),
107107 decoration: InputDecoration (labelText: 'Password' ),
108108 obscureText: true ,
109109 validator: PasswordFieldValidator .validate,
110- onSaved: (value) => _password = value,
110+ onSaved: (String value) => _password = value,
111111 ),
112112 ];
113113 }
114114
115115 List <Widget > buildSubmitButtons () {
116116 if (_formType == FormType .login) {
117- return [
117+ return < Widget > [
118118 RaisedButton (
119119 key: Key ('signIn' ),
120120 child: Text ('Login' , style: TextStyle (fontSize: 20.0 )),
121121 onPressed: validateAndSubmit,
122122 ),
123123 FlatButton (
124- child: Text ('Create an account' ,
125- style: TextStyle (fontSize: 20.0 )),
124+ child: Text ('Create an account' , style: TextStyle (fontSize: 20.0 )),
126125 onPressed: moveToRegister,
127126 ),
128127 ];
129128 } else {
130- return [
129+ return < Widget > [
131130 RaisedButton (
132- child: Text ('Create an account' ,
133- style: TextStyle (fontSize: 20.0 )),
131+ child: Text ('Create an account' , style: TextStyle (fontSize: 20.0 )),
134132 onPressed: validateAndSubmit,
135133 ),
136134 FlatButton (
137- child: Text ('Have an account? Login' ,
138- style: TextStyle (fontSize: 20.0 )),
135+ child: Text ('Have an account? Login' , style: TextStyle (fontSize: 20.0 )),
139136 onPressed: moveToLogin,
140137 ),
141138 ];
0 commit comments