443 lines
16 KiB
Dart
443 lines
16 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:intl_phone_field/countries.dart';
|
|
import 'package:prosappco/src/authentication/authentication_repository.dart';
|
|
import 'package:prosappco/src/controllers/add_name_email_city.dart';
|
|
import 'package:prosappco/src/screens/welcome.dart';
|
|
|
|
class ProfileScreen extends StatefulWidget {
|
|
ProfileScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ProfileScreen> createState() => _ProfileScreenState();
|
|
}
|
|
|
|
class _ProfileScreenState extends State<ProfileScreen> {
|
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
|
final _formKey = GlobalKey<FormState>();
|
|
final controller = Get.put(NameEmailCityController());
|
|
final _phoneNumberController = TextEditingController();
|
|
final _nameController = TextEditingController();
|
|
final _emailController = TextEditingController();
|
|
late final FirebaseAuth _auth;
|
|
|
|
var _ciudad = '...';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_auth = FirebaseAuth.instance;
|
|
|
|
final currentUser = _auth.currentUser;
|
|
|
|
if (currentUser != null && currentUser.phoneNumber != null) {
|
|
_phoneNumberController.text = currentUser.phoneNumber!;
|
|
}
|
|
|
|
if (currentUser != null && currentUser.displayName != null) {
|
|
_nameController.text = currentUser.displayName!;
|
|
}
|
|
if (currentUser != null && currentUser.email != null) {
|
|
_emailController.text = currentUser.email!;
|
|
}
|
|
|
|
if (_ciudad == '...') {
|
|
AuthenticationRepository.instance
|
|
.getCity(uid.toString())
|
|
.then((String s) => setState(() {
|
|
_ciudad = s;
|
|
}));
|
|
}
|
|
}
|
|
|
|
Future<void> updateInfo() async {
|
|
String newName = _nameController.text.trim();
|
|
String newEmail = _emailController.text.trim();
|
|
|
|
if (newName.isEmpty) {
|
|
// Si el nuevo nombre está vacío, no lo actualizamos y mostramos un mensaje al usuario
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Ingrese un nombre válido')),
|
|
);
|
|
return;
|
|
}
|
|
if (newEmail.isEmpty) {
|
|
// Si el nuevo nombre está vacío, no lo actualizamos y mostramos un mensaje al usuario
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Ingrese un email válido')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await FirebaseAuth.instance.currentUser!.updateDisplayName(newName);
|
|
await FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
'name': newName,
|
|
});
|
|
print('Nombre actualizado correctamente');
|
|
} catch (e) {
|
|
print('Error al actualizar el nombre: $e');
|
|
}
|
|
|
|
try {
|
|
await FirebaseAuth.instance.currentUser!.updateEmail(newEmail);
|
|
await FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
'email': newEmail,
|
|
});
|
|
print('Correo electrónico actualizado correctamente');
|
|
} catch (e) {
|
|
print('Error al actualizar el correo electrónico: $e');
|
|
}
|
|
|
|
AuthCredential credential =
|
|
EmailAuthProvider.credential(email: newEmail, password: '654321');
|
|
|
|
try {
|
|
// Reautentica al usuario con las credenciales proporcionadas
|
|
await FirebaseAuth.instance.currentUser!
|
|
.reauthenticateWithCredential(credential);
|
|
|
|
// Actualiza el correo electrónico del usuario
|
|
await FirebaseAuth.instance.currentUser!.updateEmail(newEmail);
|
|
|
|
// Actualiza los datos del usuario en tu base de datos
|
|
await FirebaseFirestore.instance.collection('users').doc(uid).update({
|
|
'email': newEmail,
|
|
});
|
|
|
|
print('Correo electrónico actualizado correctamente');
|
|
} catch (e) {
|
|
print('Error al actualizar el correo electrónico: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final User? user = FirebaseAuth.instance.currentUser;
|
|
|
|
String city = _ciudad.toString();
|
|
|
|
return SafeArea(
|
|
child: Scaffold(
|
|
resizeToAvoidBottomInset: false,
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.white,
|
|
iconTheme: IconThemeData(
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Perfil',
|
|
style: TextStyle(
|
|
color: Colors.black,
|
|
),
|
|
)),
|
|
drawer: Drawer(
|
|
child: ListView(
|
|
padding: EdgeInsets.zero,
|
|
children: [
|
|
Column(
|
|
children: [
|
|
Column(
|
|
children: [
|
|
Container(
|
|
child: Builder(builder: (context) {
|
|
return ListTile(
|
|
onTap: () {
|
|
Scaffold.of(context).openEndDrawer();
|
|
},
|
|
title: Text('${user?.displayName}',
|
|
style: TextStyle(fontWeight: FontWeight.bold)),
|
|
subtitle: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'${user?.phoneNumber}',
|
|
style: TextStyle(fontSize: 12),
|
|
),
|
|
Text(
|
|
'$city',
|
|
style: TextStyle(fontSize: 12),
|
|
),
|
|
],
|
|
),
|
|
leading: Container(
|
|
width: 60,
|
|
height: 60,
|
|
decoration: BoxDecoration(
|
|
color: Color(0xFF2BA4EC),
|
|
borderRadius: BorderRadius.circular(50),
|
|
),
|
|
child: Icon(
|
|
Icons.person,
|
|
color: Colors.white,
|
|
size: 55,
|
|
),
|
|
),
|
|
trailing: Icon(Icons.keyboard_arrow_right,
|
|
color: Colors.black),
|
|
contentPadding: EdgeInsets.symmetric(
|
|
vertical: 20, horizontal: 16),
|
|
);
|
|
}),
|
|
),
|
|
], // padding: const EdgeInsets.only(top: 20, bottom: 15),
|
|
),
|
|
Divider(height: 0, color: Colors.grey),
|
|
Column(
|
|
children: [
|
|
ListTile(
|
|
onTap: () {},
|
|
leading: Icon(
|
|
Icons.history,
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Mis servicios',
|
|
style: TextStyle(fontSize: 15),
|
|
),
|
|
),
|
|
Builder(builder: (context) {
|
|
return ListTile(
|
|
onTap: () {
|
|
Scaffold.of(context).openEndDrawer();
|
|
},
|
|
leading: Icon(
|
|
Icons.person_outline,
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Mi perfil',
|
|
style: TextStyle(fontSize: 15),
|
|
),
|
|
);
|
|
}),
|
|
ListTile(
|
|
onTap: () {},
|
|
leading: Icon(
|
|
Icons.construction_outlined,
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Configuración',
|
|
style: TextStyle(fontSize: 15),
|
|
),
|
|
),
|
|
ListTile(
|
|
onTap: () {},
|
|
leading: Icon(
|
|
Icons.question_mark_rounded,
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Soporte',
|
|
style: TextStyle(fontSize: 15),
|
|
),
|
|
),
|
|
ListTile(
|
|
onTap: () {},
|
|
leading: Icon(
|
|
Icons.messenger_outline,
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Mensajes',
|
|
style: TextStyle(fontSize: 15),
|
|
),
|
|
),
|
|
ListTile(
|
|
onTap: () {
|
|
Navigator.pushReplacementNamed(context, '/welcome');
|
|
},
|
|
trailing: Icon(
|
|
Icons.keyboard_arrow_right,
|
|
color: Colors.white,
|
|
),
|
|
title: Text(
|
|
'Solicitar servicio',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 17,
|
|
fontWeight: FontWeight.bold),
|
|
),
|
|
tileColor: Color(0xFF2BA4EC),
|
|
contentPadding:
|
|
EdgeInsets.symmetric(vertical: 5, horizontal: 16),
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
'Prosapp',
|
|
style: TextStyle(fontSize: 10, color: Colors.grey[700]),
|
|
),
|
|
Padding(
|
|
padding:
|
|
const EdgeInsets.only(top: 7, left: 3, right: 3),
|
|
child: Text(
|
|
'®',
|
|
style:
|
|
TextStyle(fontSize: 25, color: Colors.grey[700]),
|
|
),
|
|
),
|
|
Text(
|
|
'todos los derechos reservados',
|
|
style: TextStyle(fontSize: 10, color: Colors.grey[700]),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 200),
|
|
child: Row(
|
|
children: [
|
|
SizedBox(width: 10),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
AuthenticationRepository.instance.logout();
|
|
},
|
|
child: Icon(
|
|
Icons.logout_outlined,
|
|
color: Colors.red,
|
|
size: 35,
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
primary: Colors.white,
|
|
shape: CircleBorder(),
|
|
elevation: 3,
|
|
minimumSize: Size(45, 45),
|
|
),
|
|
),
|
|
SizedBox(width: 5),
|
|
ElevatedButton(
|
|
onPressed: () {},
|
|
child: Text(
|
|
'Modo profesional',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 15,
|
|
),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
primary: Color(0xFF2BA4EC),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(50),
|
|
),
|
|
elevation: 0,
|
|
minimumSize: Size(200, 45),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
body: Center(
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
margin: EdgeInsets.symmetric(vertical: 50),
|
|
width: 100,
|
|
height: 100,
|
|
decoration: BoxDecoration(
|
|
color: Color(0xFF2BA4EC),
|
|
borderRadius: BorderRadius.circular(50),
|
|
),
|
|
child: Icon(
|
|
Icons.person,
|
|
color: Colors.white,
|
|
size: 90,
|
|
),
|
|
),
|
|
Container(
|
|
width: 300,
|
|
padding: EdgeInsets.only(top: 0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
children: [
|
|
TextFormField(
|
|
controller: _nameController,
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(Icons.person_outline),
|
|
hintText: 'Nombre'),
|
|
),
|
|
SizedBox(
|
|
height:
|
|
20.0), // Agrega espacio vertical entre los TextFormFields
|
|
|
|
TextFormField(
|
|
controller: _emailController,
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(Icons.email_outlined),
|
|
hintText: 'Email'),
|
|
),
|
|
SizedBox(
|
|
height:
|
|
20.0), // Agrega espacio vertical entre los TextFormFields
|
|
TextFormField(
|
|
readOnly: true,
|
|
onTap: () {
|
|
Navigator.pushReplacementNamed(context, '/city');
|
|
},
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(Icons.near_me),
|
|
suffixIcon: Icon(Icons.arrow_drop_down),
|
|
hintText: city == '' ? 'Ciudad' : '$city',
|
|
),
|
|
),
|
|
|
|
SizedBox(
|
|
height:
|
|
20.0), // Agrega espacio vertical entre los TextFormFields
|
|
TextFormField(
|
|
controller: _phoneNumberController,
|
|
readOnly: true,
|
|
onTap: () {
|
|
Navigator.pushNamed(context, '/newNumber');
|
|
},
|
|
decoration: InputDecoration(
|
|
prefixIcon: Icon(Icons.phone_android),
|
|
suffixIcon: Icon(Icons.edit_outlined),
|
|
hintText: '+57'),
|
|
),
|
|
],
|
|
)),
|
|
),
|
|
Container(
|
|
alignment: Alignment.bottomCenter,
|
|
margin: EdgeInsets.only(top: 170),
|
|
child: ElevatedButton(
|
|
onPressed: () {
|
|
updateInfo();
|
|
},
|
|
child: Text(
|
|
'Guardar',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 17,
|
|
),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Color(0xFF2BA4EC),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(50),
|
|
),
|
|
elevation: 0,
|
|
minimumSize: Size(200, 50),
|
|
),
|
|
)),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|