ciudad
This commit is contained in:
+100
-26
@@ -1,3 +1,5 @@
|
||||
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';
|
||||
@@ -6,7 +8,7 @@ import 'package:prosappco/src/controllers/add_name_email_city.dart';
|
||||
import 'package:prosappco/src/screens/welcome.dart';
|
||||
|
||||
class ProfileScreen extends StatefulWidget {
|
||||
const ProfileScreen({super.key});
|
||||
ProfileScreen({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<ProfileScreen> createState() => _ProfileScreenState();
|
||||
@@ -16,20 +18,31 @@ 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 _nombre = '...';
|
||||
var _ciudad = '...';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (_nombre == '...') {
|
||||
AuthenticationRepository.instance
|
||||
.getName(uid.toString())
|
||||
.then((String s) => setState(() {
|
||||
_nombre = s;
|
||||
}));
|
||||
_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())
|
||||
@@ -39,9 +52,73 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
String name = _nombre.toString();
|
||||
final User? user = FirebaseAuth.instance.currentUser;
|
||||
|
||||
String city = _ciudad.toString();
|
||||
|
||||
return SafeArea(
|
||||
child: Scaffold(
|
||||
resizeToAvoidBottomInset: false,
|
||||
@@ -70,15 +147,13 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
onTap: () {
|
||||
Scaffold.of(context).openEndDrawer();
|
||||
},
|
||||
title: Text('$name',
|
||||
title: Text('${user?.displayName}',
|
||||
style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
AuthenticationRepository.instance
|
||||
.getCurrentUserPhone()
|
||||
.toString(),
|
||||
'${user?.phoneNumber}',
|
||||
style: TextStyle(fontSize: 12),
|
||||
),
|
||||
Text(
|
||||
@@ -287,7 +362,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: controller.name,
|
||||
controller: _nameController,
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: Icon(Icons.person_outline),
|
||||
hintText: 'Nombre'),
|
||||
@@ -297,7 +372,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
20.0), // Agrega espacio vertical entre los TextFormFields
|
||||
|
||||
TextFormField(
|
||||
controller: controller.email,
|
||||
controller: _emailController,
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: Icon(Icons.email_outlined),
|
||||
hintText: 'Email'),
|
||||
@@ -306,24 +381,26 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
height:
|
||||
20.0), // Agrega espacio vertical entre los TextFormFields
|
||||
TextFormField(
|
||||
controller: controller.city,
|
||||
readOnly: true,
|
||||
onTap: () {
|
||||
Navigator.pushNamed(context, '/city');
|
||||
Navigator.pushReplacementNamed(context, '/city');
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: Icon(Icons.near_me),
|
||||
suffixIcon: Icon(Icons.arrow_drop_down),
|
||||
hintText: 'Ciudad',
|
||||
hintText: city == '' ? 'Ciudad' : '$city',
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(
|
||||
height:
|
||||
20.0), // Agrega espacio vertical entre los TextFormFields
|
||||
|
||||
TextFormField(
|
||||
controller: controller.name,
|
||||
controller: _phoneNumberController,
|
||||
readOnly: true,
|
||||
onTap: () {
|
||||
Navigator.pushNamed(context, '/newNumber');
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: Icon(Icons.phone_android),
|
||||
suffixIcon: Icon(Icons.edit_outlined),
|
||||
@@ -337,12 +414,9 @@ class _ProfileScreenState extends State<ProfileScreen> {
|
||||
margin: EdgeInsets.only(top: 170),
|
||||
child: ElevatedButton(
|
||||
onPressed: () {
|
||||
// AuthenticationRepository.instance.addNameAndEmail(
|
||||
// uid,
|
||||
// controller.name.text.trim(),
|
||||
// controller.email.text.trim(),
|
||||
// controller.city.text.trim(),
|
||||
// );
|
||||
String newName = _nameController.text;
|
||||
String newEmail = _emailController.text;
|
||||
updateInfo();
|
||||
},
|
||||
child: Text(
|
||||
'Guardar',
|
||||
|
||||
Reference in New Issue
Block a user