diff --git a/lib/src/screens/profile_web.dart b/lib/src/screens/profile_web.dart index 927e7a5..51589d4 100644 --- a/lib/src/screens/profile_web.dart +++ b/lib/src/screens/profile_web.dart @@ -27,6 +27,7 @@ class _ProfileWebScreenState extends State { final uid = AuthenticationRepository.instance.getCurrentUserUid(); final FirebaseStorage storage = FirebaseStorage.instance; late final FirebaseAuth _auth; + List? filteredCities; // variables imagen String selectedImage = ''; @@ -194,6 +195,18 @@ class _ProfileWebScreenState extends State { } } + String? selectedCity = _ciudad; + + // Guardar la ciudad seleccionada en el documento del usuario + try { + await FirebaseFirestore.instance.collection('users').doc(uid).update({ + 'city': selectedCity, + }); + print('Ciudad actualizada correctamente'); + } catch (e) { + print('Error al actualizar la ciudad: $e'); + } + try { await FirebaseFirestore.instance .collection('users') @@ -263,6 +276,38 @@ class _ProfileWebScreenState extends State { } } + Future> _getCities() async { + List citys = []; + try { + QuerySnapshot countries = await countriesCollection.get(); + for (DocumentSnapshot country in countries.docs) { + String countryName = country.id; + Map data = country.data() as Map; + Map> states = {}; + + for (var entry in data.entries) { + String key = entry.key; + Map cityData = Map.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; + } + @override Widget build(BuildContext context) { String city = _ciudad.toString(); @@ -384,32 +429,42 @@ class _ProfileWebScreenState extends State { _email != null ? const SizedBox.shrink() : const SizedBox(height: 20.0), - TextFormField( - readOnly: true, - onTap: () async { - final String? ciudad = await Navigator.push( - context, - CupertinoPageRoute( - builder: (BuildContext context) { - return const CityScreen(); - }, - ), - ) as String?; + FutureBuilder>( + future: _getCities(), + builder: (context, snapshot) { + if (snapshot.connectionState == + ConnectionState.waiting) { + return Center( + child: CircularProgressIndicator(), + ); + } else if (snapshot.hasError) { + return Center( + child: Text('Error al obtener las ciudades'), + ); + } else { + List filteredCities = snapshot.data!; - if (ciudad != null) { - setState(() { - _ciudad = ciudad; - }); + return DropdownButtonFormField( + value: _ciudad, // Valor seleccionado actualmente + onChanged: (String? newValue) { + setState(() { + _ciudad = + newValue!; // Actualizar el valor seleccionado + }); + }, + items: filteredCities.map((City city) { + return DropdownMenuItem( + value: city.cityName, + child: Text(city.cityName ?? ''), + ); + }).toList(), + decoration: InputDecoration( + prefixIcon: const Icon(Icons.near_me), + hintText: _ciudad == null ? '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(