Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:firebase_database/firebase_database.dart';
- import 'package:bencana/main.dart';
- class AuthenticationPage extends StatefulWidget {
- @override
- _AuthenticationPageState createState() => _AuthenticationPageState();
- }
- enum FormType { signIn, signUp }
- enum AuthStatus { signIn, notSignIn }
- final userRef = FirebaseDatabase.instance.reference().child('user');
- class _AuthenticationPageState extends State<AuthenticationPage> {
- AuthStatus _authStatus = AuthStatus.notSignIn;
- final formKey = GlobalKey<FormState>();
- FormType _formType = FormType.signIn;
- String _email;
- String _password;
- String _fullName;
- bool validateAndSave() {
- final form = formKey.currentState;
- form.save();
- if (form.validate()) {
- print('Form is valid. Email: $_email, Password: $_password');
- return true;
- } else {
- print('Form is invalid. Email: $_email, Password: $_password');
- return false;
- }
- }
- void validateAndSubmit() async {
- if (validateAndSave()) {
- try {
- if (_formType == FormType.signIn) {
- formKey.currentState.reset();
- // Buat proses sign in
- FirebaseUser user = await FirebaseAuth.instance
- .signInWithEmailAndPassword(email: _email, password: _password);
- print('user: ${user.uid}');
- this.setState(() {
- _authStatus = AuthStatus.signIn;
- });
- } else {
- formKey.currentState.reset();
- // Buat proses sign up
- FirebaseUser user = await FirebaseAuth.instance
- .createUserWithEmailAndPassword(
- email: _email, password: _password);
- print('user: ${user.uid}');
- userRef
- .child(user.uid)
- .set({'fullName': _fullName, 'email': _email}).then((_) {
- this.setState(() {
- _authStatus = AuthStatus.signIn;
- });
- });
- }
- } catch (e) {
- print('Error: $e');
- }
- }
- }
- void moveToSignIn() {
- setState(() {
- _formType = FormType.signIn;
- });
- }
- void moveToSignUp() {
- setState(() {
- _formType = FormType.signUp;
- });
- }
- List<Widget> buildInputs() {
- if (_formType == FormType.signIn) {
- return [
- SizedBox(
- height: 80.0,
- ),
- Container(
- width: 150,
- height: 150,
- decoration: BoxDecoration(
- color: Colors.teal[200],
- shape: BoxShape.circle,
- image: DecorationImage(
- fit: BoxFit.fill,
- image: AssetImage('images/bencana_logo.png'))
- ),
- ),
- SizedBox(
- height: 20.0,
- ),
- TextFormField(
- decoration: InputDecoration(labelText: 'Email'),
- keyboardType: TextInputType.emailAddress,
- validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
- onSaved: (value) => _email = value,
- ),
- TextFormField(
- decoration: InputDecoration(
- labelText: 'Password',
- ),
- obscureText: true,
- validator: (value) => value.length < 6 ? 'Minimum 6 character' : null,
- onSaved: (value) => _password = value,
- ),
- ];
- } else {
- return [
- SizedBox(
- height: 80.0,
- ),
- Container(
- child: Image.asset('images/bencana_logo.png'),
- width: 200.0,
- height: 200.0,
- color: Colors.white,
- ),
- SizedBox(
- height: 20.0,
- ),
- TextFormField(
- decoration: InputDecoration(labelText: 'Email'),
- keyboardType: TextInputType.emailAddress,
- validator: (value) => value.isEmpty ? 'Email can\'t be empty' : null,
- onSaved: (value) => _email = value,
- ),
- TextFormField(
- decoration: InputDecoration(
- labelText: 'Password',
- ),
- obscureText: true,
- validator: (value) => value.length < 6 ? 'Minimum 6 character' : null,
- onSaved: (value) => _password = value,
- ),
- TextFormField(
- decoration: InputDecoration(labelText: 'Full Name'),
- validator: (value) =>
- value.isEmpty ? 'Full name can\'t be empty' : null,
- onSaved: (value) => _fullName = value,
- ),
- ];
- }
- }
- List<Widget> buildButtons() {
- if (_formType == FormType.signIn) {
- return [
- SizedBox(
- height: 10,
- ),
- RaisedButton(
- child: Text('Sign In'),
- color: Colors.teal,
- textColor: Colors.white,
- onPressed: validateAndSubmit,
- ),
- FlatButton(
- onPressed: moveToSignUp,
- child: Text('Create a new account'),
- textColor: Colors.teal,
- ),
- ];
- } else {
- return [
- SizedBox(
- height: 10,
- ),
- RaisedButton(
- child: Text('Sign Up'),
- color: Colors.teal,
- textColor: Colors.white,
- onPressed: validateAndSubmit,
- ),
- FlatButton(
- onPressed: moveToSignIn,
- child: Text('Already have account? Sign in!'),
- textColor: Colors.teal,
- ),
- ];
- }
- }
- @override
- Widget build(BuildContext context) {
- switch (_authStatus) {
- case AuthStatus.signIn:
- return DashboardScreen();
- case AuthStatus.notSignIn:
- return Scaffold(
- body: Container(
- child: Padding(
- padding: EdgeInsets.all(16.0),
- child: Form(
- key: formKey,
- child: ListView(
- children: <Widget>[
- Column(
- children: buildInputs() + buildButtons(),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement