This commit is contained in:
Felipe
2023-11-17 11:58:11 -05:00
parent a0193bb43f
commit 6fe88bf31c
10 changed files with 266 additions and 781 deletions
+179 -43
View File
@@ -2,6 +2,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:io';
@@ -21,6 +22,7 @@ import 'package:prosappco/src/presentation/widgets/profile/gender_dropdown.dart'
import 'package:prosappco/src/presentation/widgets/shared/primary_button.dart';
import 'package:provider/provider.dart';
import '../../../components/photo_view.dart';
import 'package:universal_html/html.dart' as html;
class ProfileScreen extends StatefulWidget {
const ProfileScreen({Key? key}) : super(key: key);
@@ -302,6 +304,18 @@ class _ProfileScreenState extends State<ProfileScreen> {
}
}
String? selectedCity = _ciudad;
if (kIsWeb) {
try {
await FirebaseFirestore.instance.collection('users').doc(uid).update({
'city': selectedCity,
});
} catch (e) {
print('Error al actualizar la ciudad: $e');
}
}
if (enableLoginWithEmail) {
if (currentUser?.email != newEmail) {
if (newPassword.isNotEmpty) {
@@ -342,7 +356,6 @@ class _ProfileScreenState extends State<ProfileScreen> {
} else {
final uploaded = await uploadImage(imagen_to_upload!);
updateImage(photoTemp);
//image
}
} catch (e) {
print('Error al actualizar la imagen de perfil $e');
@@ -428,8 +441,6 @@ class _ProfileScreenState extends State<ProfileScreen> {
message:
'Verifica tu contraseña actual y asegúrate de que el nuevo correo electrónico no se haya utilizado previamente.',
);
print('Error al actualizar el correo electrónico: $e');
}
}
@@ -497,9 +508,10 @@ class _ProfileScreenState extends State<ProfileScreen> {
return true;
} catch (e) {
WarningSnackbar.show(
title: 'Inicia sesión de nuevo',
message:
'Para agregar un correo debes haber iniciado sesión recientemente.');
title: 'Inicia sesión de nuevo',
message: 'Inicia la sesión de nuevo para guardar los cambios.',
);
AuthenticationRepository.instance.logout(uid!);
}
}
return false;
@@ -507,6 +519,42 @@ class _ProfileScreenState extends State<ProfileScreen> {
bool enableLoginWithEmail = false;
Future<List<City>> _getCities() async {
List<City> citys = [];
if (kIsWeb) {
try {
QuerySnapshot countries = await countriesCollection.get();
for (DocumentSnapshot country in countries.docs) {
String countryName = country.id;
Map<String, dynamic> data = country.data() as Map<String, dynamic>;
Map<String, Map<String, String>> states = {};
for (var entry in data.entries) {
String key = entry.key;
Map<String, String> cityData =
Map<String, String>.from(entry.value);
states[key] = cityData;
}
for (var state in states.entries) {
var citysState = state.value.entries.map((city) => City(
cityName: city.key,
coordsOfCity: city.value,
stateOfCity: state.key,
countryOfCity: countryName,
));
citys.addAll(citysState);
}
}
} catch (e) {
print('Error obteniendo las ciudades: $e');
}
}
return citys;
}
Future<void> _showChoiceDialog(BuildContext context) async {
return showDialog(
context: context,
@@ -560,10 +608,11 @@ class _ProfileScreenState extends State<ProfileScreen> {
return Scaffold(
appBar: PopAppbar(
onPressed: () {
Navigator.pop(context);
},
label: 'Perfil'),
onPressed: () {
Navigator.pop(context);
},
label: 'Perfil',
),
body: SingleChildScrollView(
reverse: true,
child: Center(
@@ -599,37 +648,76 @@ class _ProfileScreenState extends State<ProfileScreen> {
return null;
},
decoration: const InputDecoration(
prefixIcon: Icon(Icons.person_outline),
hintText: 'Nombre (Obligatorio)'),
),
const SizedBox(),
TextFormField(
readOnly: true,
onTap: () async {
final String? ciudad = await Navigator.push(
context,
CupertinoPageRoute(
builder: (BuildContext context) {
return const CityScreen();
},
),
) as String?;
if (ciudad != null) {
setState(() {
_ciudad = ciudad;
});
}
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.near_me),
suffixIcon: const Icon(Icons.arrow_drop_down),
hintStyle: city == ''
? const TextStyle()
: const TextStyle(color: Colors.black87),
hintText: city == '' ? 'Ciudad' : city,
prefixIcon: Icon(Icons.person_outline),
hintText: 'Nombre (Obligatorio)',
),
),
const SizedBox(),
kIsWeb
? FutureBuilder<List<City>>(
future: _getCities(),
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
} else if (snapshot.hasError) {
return const Center(
child:
Text('Error al obtener las ciudades'),
);
} else {
List<City> filteredCities = snapshot.data!;
return DropdownButtonFormField<String>(
value: _ciudad,
onChanged: (String? newValue) {
setState(() {
_ciudad = newValue!;
});
},
items: filteredCities.map((City city) {
return DropdownMenuItem<String>(
value: city.cityName,
child: Text(city.cityName ?? ''),
);
}).toList(),
decoration: InputDecoration(
prefixIcon: const Icon(Icons.near_me),
hintText: _ciudad,
),
);
}
},
)
: TextFormField(
readOnly: true,
onTap: () async {
final String? ciudad = await Navigator.push(
context,
CupertinoPageRoute(
builder: (BuildContext context) {
return const CityScreen();
},
),
) as String?;
if (ciudad != null) {
setState(() {
_ciudad = ciudad;
});
}
},
decoration: InputDecoration(
prefixIcon: const Icon(Icons.near_me),
suffixIcon: const Icon(Icons.arrow_drop_down),
hintStyle: city == ''
? const TextStyle()
: const TextStyle(color: Colors.black87),
hintText: city == '' ? 'Ciudad' : city,
),
),
const SizedBox(height: 20.0),
TextFormField(
controller: _phoneNumberController,
@@ -732,8 +820,9 @@ class _ProfileScreenState extends State<ProfileScreen> {
}
},
decoration: const InputDecoration(
prefixIcon: Icon(Icons.email_outlined),
hintText: 'Email'),
prefixIcon: Icon(Icons.email_outlined),
hintText: 'Email',
),
),
const SizedBox(height: 20.0),
_email != null
@@ -777,6 +866,47 @@ class _ProfileScreenState extends State<ProfileScreen> {
_email != null
? const SizedBox.shrink()
: const SizedBox(height: 20),
Container(
margin: const EdgeInsets.only(
left: 5,
right: 5,
top: 5,
bottom: 5,
),
padding: const EdgeInsets.symmetric(
horizontal: 10, vertical: 8),
decoration: BoxDecoration(
color: const Color(0xFFD6F4FF),
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 1,
blurRadius: 5,
offset: const Offset(1, 3),
),
],
),
child: const Row(
children: [
Icon(
Icons.error_outline,
size: 20,
color: Colors.black54,
),
SizedBox(width: 10),
Expanded(
child: Text(
'Al habilitar el inicio de sesión con correo, se cerrara la sesión actual.',
style: TextStyle(
color: Colors.black54,
fontSize: 13,
),
),
),
],
),
)
],
),
)
@@ -808,11 +938,17 @@ class _ProfileScreenState extends State<ProfileScreen> {
minHeight: 45,
)
: const SizedBox(height: 20),
const SizedBox(height: 60),
const SizedBox(height: 40),
PrimaryButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {
await updateInfo();
if (kIsWeb) {
await updateInfo().whenComplete(() {
html.window.location.reload();
});
} else {
await updateInfo();
}
}
await userProvider.updateUserDataAndScores();