You are on page 1of 4

import 'package:bikers_go_couriers/authentication/signup_screen.

dart';
import 'package:bikers_go_couriers/global/global.dart';
import 'package:bikers_go_couriers/splashScreen/splash_screen.dart';
import 'package:bikers_go_couriers/widgets/progress_dialog.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class LoginScreen extends StatefulWidget


{

@override
_LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen>


{
TextEditingController emailTextEditingController = TextEditingController();
TextEditingController passwordTextEditingController = TextEditingController();

validateForm()
{
if(!emailTextEditingController.text.contains("@"))
{
Fluttertoast.showToast(msg: "O endereço de email não é válido.");
}
else if(passwordTextEditingController.text.isEmpty)
{
Fluttertoast.showToast(msg: "A senha é obrigatória.");
}
else
{
loginDriverNow();
}
}

loginDriverNow() async
{
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext c)
{
return ProgressDialog(message: "Processando, aguarde...",);
}
);

final User? firebaseUser = (


await fAuth.signInWithEmailAndPassword(
email: emailTextEditingController.text.trim(),
password: passwordTextEditingController.text.trim(),
).catchError((msg){
Navigator.pop(context);
Fluttertoast.showToast(msg: "Error: " + msg.toString());
})
).user;

if(firebaseUser != null)
{
DatabaseReference driversRef =
FirebaseDatabase.instance.ref().child("drivers");
driversRef.child(firebaseUser.uid).once().then((driverKey)
{
final snap = driverKey.snapshot;
if(snap.value != null)
{
currentFirebaseUser = firebaseUser;
Fluttertoast.showToast(msg: "Acesso realizado com sucesso.");
Navigator.push(context, MaterialPageRoute(builder: (c)=> const
MySplashScreen()));
}
else
{
Fluttertoast.showToast(msg: "Email informado não encontrado.");
fAuth.signOut();
Navigator.push(context, MaterialPageRoute(builder: (c)=> const
MySplashScreen()));
}
});
}
else
{
Navigator.pop(context);
Fluttertoast.showToast(msg: "Ocorreu um erro ao se conectar.");
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [

const SizedBox(height: 30,),

Padding(
padding: const EdgeInsets.all(20.0),
child: Image.asset("images/logo.png"),
),

const SizedBox(height: 10,),

const Text(
"Entrar como Entregador",
style: TextStyle(
fontSize: 26,
color: Colors.grey,
fontWeight: FontWeight.bold,
),
),

TextField(
controller: emailTextEditingController,
keyboardType: TextInputType.emailAddress,
style: const TextStyle(
color: Colors.grey
),
decoration: const InputDecoration(
labelText: "Email",
hintText: "Email",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10,
),
labelStyle: TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
),

TextField(
controller: passwordTextEditingController,
keyboardType: TextInputType.text,
obscureText: true,
style: const TextStyle(
color: Colors.grey
),
decoration: const InputDecoration(
labelText: "Senha",
hintText: "Senha",
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 10,
),
labelStyle: TextStyle(
color: Colors.grey,
fontSize: 14,
),
),
),

const SizedBox(height: 20,),

ElevatedButton(
onPressed: ()
{
validateForm();
},
style: ElevatedButton.styleFrom(
primary: Colors.lightGreenAccent,
),
child: const Text(
"Login",
style: TextStyle(
color: Colors.black54,
fontSize: 18,
),
),
),

TextButton(
child: const Text(
"Ainda não possui uma conta? Cadastre-se aqui.",
style: TextStyle(color: Colors.grey),
),
onPressed: ()
{
Navigator.push(context, MaterialPageRoute(builder: (c)=>
SignUpScreen()));
},
),

],
),
),
),
);
}
}

You might also like