Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:flutter/material.dart';
- import 'home.dart';
- class SignInPage extends StatefulWidget {
- @override
- _SignInPageState createState() => new _SignInPageState();
- }
- enum FormType { signIn, signUp }
- enum AuthStatus { notSignIn, signIn }
- class _SignInPageState extends State<SignInPage> {
- final formKey = new GlobalKey<FormState>();
- AuthStatus _authStatus = AuthStatus.notSignIn;
- initState() {
- super.initState();
- getCurrentUser().then((userId) {
- setState(() {
- _authStatus = userId == null ? AuthStatus.notSignIn : AuthStatus.signIn;
- });
- });
- }
- Future<String> getCurrentUser() async {
- try {
- FirebaseUser user = await FirebaseAuth.instance.currentUser();
- return user.uid;
- } catch (e) {
- print('Error: $e');
- return null;
- }
- }
- String _email;
- String _password;
- FormType _formType = FormType.signIn;
- bool validateAndSave() {
- final form = formKey.currentState;
- if (form.validate()) {
- form.save();
- return true;
- } else {
- return false;
- }
- }
- void validateAndSubmit() async {
- if (validateAndSave()) {
- try {
- if (_formType == FormType.signIn) {
- FirebaseUser user = await FirebaseAuth.instance
- .signInWithEmailAndPassword(email: _email, password: _password);
- print('User: ${user.uid}');
- formKey.currentState.reset();
- this.setState(() {
- _authStatus = AuthStatus.signIn;
- });
- } else {
- FirebaseUser user = await FirebaseAuth.instance
- .createUserWithEmailAndPassword(
- email: _email, password: _password);
- print('User: ${user.uid}');
- formKey.currentState.reset();
- this.setState(() {
- _authStatus = AuthStatus.signIn;
- });
- }
- } catch (e) {
- print("Error: $e");
- }
- }
- }
- void moveToSignUp() {
- setState(() {
- _formType = FormType.signUp;
- });
- }
- void moveToSignIn() {
- setState(() {
- _formType = FormType.signIn;
- });
- }
- @override
- Widget build(BuildContext context) {
- switch (_authStatus) {
- case AuthStatus.signIn:
- return new HomePage();
- case AuthStatus.notSignIn:
- return new Scaffold(
- body: new Container(
- height: double.infinity,
- decoration: new BoxDecoration(color: Colors.teal),
- child: new ListView(
- children: <Widget>[
- new Stack(
- children: <Widget>[
- new Container(
- height: 380.0,
- margin: const EdgeInsets.only(
- top: 150.0,
- left: 20.0,
- right: 20.0,
- ),
- child: new Container(
- height: 200.0,
- padding: const EdgeInsets.only(
- top: 80.0,
- left: 16.0,
- right: 16.0,
- bottom: 20.0,
- ),
- decoration: new BoxDecoration(
- color: Colors.white,
- borderRadius: new BorderRadius.circular(10.0),
- ),
- child: new Form(
- key: formKey,
- child: new Column(
- children: buildInputs() + buildSubmitButtons(),
- ),
- ),
- ),
- ),
- new Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: <Widget>[
- new Container(
- padding: const EdgeInsets.only(
- top: 75.0,
- ),
- child: new CircleAvatar(
- child: new Image.asset(
- "images/fascia_icon.png",
- width: 150.0,
- height: 150.0,
- ),
- radius: 78.0,
- backgroundColor: Colors.teal,
- ),
- ),
- ],
- ),
- ],
- ),
- ],
- ),
- ),
- );
- }
- return null;
- }
- List<Widget> buildInputs() {
- return [
- new TextFormField(
- decoration: new InputDecoration(
- labelText: 'Email',
- ),
- keyboardType: TextInputType.emailAddress,
- validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
- onSaved: (value) => _email = value,
- ),
- new TextFormField(
- decoration: new InputDecoration(
- labelText: 'Password',
- ),
- obscureText: true,
- validator: (value) => value.length < 6 ? 'Minimum 6 characters' : null,
- onSaved: (value) => _password = value,
- ),
- ];
- }
- List<Widget> buildSubmitButtons() {
- if (_formType == FormType.signIn) {
- return [
- new Padding(
- padding: const EdgeInsets.only(
- top: 20.0,
- ),
- ),
- new RaisedButton(
- child: new Text(
- 'Sign In',
- style: new TextStyle(
- color: Colors.white,
- ),
- ),
- color: Colors.teal,
- splashColor: Colors.yellowAccent,
- onPressed: validateAndSubmit,
- ),
- new Padding(
- padding: const EdgeInsets.only(
- top: 20.0,
- ),
- ),
- new FlatButton(
- child: new Text('Create new account'),
- onPressed: moveToSignUp,
- )
- ];
- } else {
- return [
- new Padding(
- padding: const EdgeInsets.only(
- top: 20.0,
- ),
- ),
- new RaisedButton(
- child: new Text(
- 'Sign Up',
- style: new TextStyle(
- color: Colors.white,
- ),
- ),
- color: Colors.teal,
- splashColor: Colors.yellowAccent,
- onPressed: validateAndSubmit,
- ),
- new Padding(
- padding: const EdgeInsets.only(
- top: 20.0,
- ),
- ),
- new FlatButton(
- child: new Text('Have an account? Sign In'),
- onPressed: moveToSignIn,
- )
- ];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement