Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EmailFieldValidator {
- static String validate(String value) {
- return value.isEmpty ? 'Email can\'t be empty' : null;
- }
- }
- class PasswordFieldValidator {
- static String validate(String value) {
- return value.isEmpty ? 'Password can\'t be empty' : null;
- }
- }
- class LoginPage extends StatefulWidget {
- LoginPage({this.onSignedIn});
- final VoidCallback onSignedIn;
- @override
- State<StatefulWidget> createState() => _LoginPageState();
- }
- enum FormType {
- login,
- register
- }
- class _LoginPageState extends State<LoginPage> {
- final formKey = GlobalKey<FormState>();
- String _email;
- String _password;
- FormType _formType = FormType.login;
- bool validateAndSave() {
- final form = formKey.currentState;
- if (form.validate()) {
- form.save();
- return true;
- }
- return false;
- }
- void validateAndSubmit() async {
- if (validateAndSave()) {
- try {
- var auth = AuthProvider.of(context).auth;
- if (_formType == FormType.login) {
- String userId =
- await auth.signInWithEmailAndPassword(_email, _password);
- print('Signed in: $userId');
- } else {
- String userId = await auth
- .createUserWithEmailAndPassword(_email, _password);
- print('Registered user: $userId');
- }
- widget.onSignedIn();
- } catch (e) {
- print('Error: $e');
- }
- }
- }
- void moveToRegister() {
- formKey.currentState.reset();
- setState(() {
- _formType = FormType.register;
- });
- }
- void moveToLogin() {
- formKey.currentState.reset();
- setState(() {
- _formType = FormType.login;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Magic Minion'),
- ),
- body: Container(
- padding: EdgeInsets.all(16.0),
- child: Form(
- key: formKey,
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: buildInputs() + buildSubmitButtons()
- )
- )
- ),
- );
- }
- List<Widget> buildInputs() {
- if (_formType == FormType.register) {
- return[
- TextFormField(
- key: Key('email'),
- decoration: InputDecoration(labelText: 'Email'),
- validator: EmailFieldValidator.validate,
- onSaved: (value) => _email = value,
- ),
- TextFormField(
- key: Key('password'),
- decoration: InputDecoration(labelText: 'Password'),
- obscureText: true,
- validator: PasswordFieldValidator.validate,
- onSaved: (value) => _password = value,
- ),
- TextFormField(
- key: Key('name'),
- decoration: InputDecoration(labelText: 'Name'),
- ),
- TextFormField(
- key: Key('dci'),
- decoration: InputDecoration(labelText: 'Wizard/s DCI#(optional)'),
- ),
- ];
- } else {
- return[
- TextFormField(
- key: Key('email'),
- decoration: InputDecoration(labelText: 'Email'),
- validator: EmailFieldValidator.validate,
- onSaved: (value) => _email = value,
- ),
- TextFormField(
- key: Key('password'),
- decoration: InputDecoration(labelText: 'Password'),
- obscureText: true,
- validator: PasswordFieldValidator.validate,
- onSaved: (value) => _password = value,
- )
- ];
- }
- }
- List<Widget> buildSubmitButtons() {
- if (_formType == FormType.login) {
- return [
- RaisedButton(
- key: Key('signIn'),
- color: Theme.of(context).accentColor,
- splashColor: Colors.deepOrange,
- child: Text('Login', style: TextStyle(fontSize: 20.0)),
- onPressed: validateAndSubmit,
- ),
- FlatButton(
- child: Text('Create an account',
- style: TextStyle(fontSize: 20.0)),
- onPressed: moveToRegister,
- ),
- ];
- } else {
- return [
- RaisedButton(
- color: Theme.of(context).accentColor,
- splashColor: Colors.deepOrange,
- child: Text('Register',
- style: TextStyle(fontSize: 20.0)),
- onPressed: validateAndSubmit,
- ),
- FlatButton(
- child: Text('Have an account? Login',
- style: TextStyle(fontSize: 20.0)),
- onPressed: moveToLogin,
- ),
- ];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement