You are on page 1of 5

import 'package:bikers_go_couriers/authentication/car_info_screen.

dart';
import 'package:bikers_go_couriers/authentication/login_screen.dart';
import 'package:bikers_go_couriers/global/global.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 SignUpScreen extends StatefulWidget


{
@override
_SignUpScreenState createState() => _SignUpScreenState();
}

class _SignUpScreenState extends State<SignUpScreen>


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

validateForm()
{
if(nameTextEditingController.text.length < 3)
{
Fluttertoast.showToast(msg: "O nome precisa ter pelo menos 3 caracteres.");
}
else if(!emailTextEditingController.text.contains("@"))
{
Fluttertoast.showToast(msg: "O endereço de email não é válido.");
}
else if(phoneTextEditingController.text.isEmpty)
{
Fluttertoast.showToast(msg: "O número de telefone é obrigatório.");
}
else if(passwordTextEditingController.text.length < 6)
{
Fluttertoast.showToast(msg: "A senha precisa ter pelo menos 6 caracteres.");
}
else
{
saveDriverInfoNow();
}
}

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

final User? firebaseUser = (


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

if(firebaseUser != null)
{
Map driverMap =
{
"id": firebaseUser.uid,
"name": nameTextEditingController.text.trim(),
"email": emailTextEditingController.text.trim(),
"phone": phoneTextEditingController.text.trim(),
};

DatabaseReference driversRef =
FirebaseDatabase.instance.ref().child("drivers");
driversRef.child(firebaseUser.uid).set(driverMap);

currentFirebaseUser = firebaseUser;
Fluttertoast.showToast(msg: "Conta criada com sucesso.");
Navigator.push(context, MaterialPageRoute(builder: (c)=> CarInfoScreen()));
}
else
{
Navigator.pop(context);
Fluttertoast.showToast(msg: "A conta não pode ser criada.");
}
}

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

const SizedBox(height: 10,),

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

const SizedBox(height: 10,),

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

TextField(
controller: nameTextEditingController,
style: const TextStyle(
color: Colors.grey
),
decoration: const InputDecoration(
labelText: "Nome",
hintText: "Nome",
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: 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: phoneTextEditingController,
keyboardType: TextInputType.phone,
style: const TextStyle(
color: Colors.grey
),
decoration: const InputDecoration(
labelText: "Telefone",
hintText: "Telefone",
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(
"Criar Conta",
style: TextStyle(
color: Colors.black54,
fontSize: 18,
),
),
),

TextButton(
child: const Text(
"Já possui uma conta? Entre aqui",
style: TextStyle(color: Colors.grey),
),
onPressed: ()
{
Navigator.push(context, MaterialPageRoute(builder: (c)=>
LoginScreen()));
},
),

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

You might also like