dropdown ciudad
This commit is contained in:
@@ -27,6 +27,7 @@ class _ProfileWebScreenState extends State<ProfileWebScreen> {
|
|||||||
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
||||||
final FirebaseStorage storage = FirebaseStorage.instance;
|
final FirebaseStorage storage = FirebaseStorage.instance;
|
||||||
late final FirebaseAuth _auth;
|
late final FirebaseAuth _auth;
|
||||||
|
List<City>? filteredCities;
|
||||||
|
|
||||||
// variables imagen
|
// variables imagen
|
||||||
String selectedImage = '';
|
String selectedImage = '';
|
||||||
@@ -194,6 +195,18 @@ class _ProfileWebScreenState extends State<ProfileWebScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
try {
|
||||||
await FirebaseFirestore.instance
|
await FirebaseFirestore.instance
|
||||||
.collection('users')
|
.collection('users')
|
||||||
@@ -263,6 +276,38 @@ class _ProfileWebScreenState extends State<ProfileWebScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<List<City>> _getCities() async {
|
||||||
|
List<City> citys = [];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
String city = _ciudad.toString();
|
String city = _ciudad.toString();
|
||||||
@@ -384,32 +429,42 @@ class _ProfileWebScreenState extends State<ProfileWebScreen> {
|
|||||||
_email != null
|
_email != null
|
||||||
? const SizedBox.shrink()
|
? const SizedBox.shrink()
|
||||||
: const SizedBox(height: 20.0),
|
: const SizedBox(height: 20.0),
|
||||||
TextFormField(
|
FutureBuilder<List<City>>(
|
||||||
readOnly: true,
|
future: _getCities(),
|
||||||
onTap: () async {
|
builder: (context, snapshot) {
|
||||||
final String? ciudad = await Navigator.push(
|
if (snapshot.connectionState ==
|
||||||
context,
|
ConnectionState.waiting) {
|
||||||
CupertinoPageRoute(
|
return Center(
|
||||||
builder: (BuildContext context) {
|
child: CircularProgressIndicator(),
|
||||||
return const CityScreen();
|
);
|
||||||
},
|
} else if (snapshot.hasError) {
|
||||||
),
|
return Center(
|
||||||
) as String?;
|
child: Text('Error al obtener las ciudades'),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
List<City> filteredCities = snapshot.data!;
|
||||||
|
|
||||||
if (ciudad != null) {
|
return DropdownButtonFormField<String>(
|
||||||
setState(() {
|
value: _ciudad, // Valor seleccionado actualmente
|
||||||
_ciudad = ciudad;
|
onChanged: (String? newValue) {
|
||||||
});
|
setState(() {
|
||||||
|
_ciudad =
|
||||||
|
newValue!; // Actualizar el valor seleccionado
|
||||||
|
});
|
||||||
|
},
|
||||||
|
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 == 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),
|
const SizedBox(height: 20.0),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
|
|||||||
Reference in New Issue
Block a user