dropdown ciudad

This commit is contained in:
Felipe
2023-07-25 17:32:24 -05:00
parent 0f74d616f7
commit 87696bf1d7
+78 -23
View File
@@ -27,6 +27,7 @@ class _ProfileWebScreenState extends State<ProfileWebScreen> {
final uid = AuthenticationRepository.instance.getCurrentUserUid();
final FirebaseStorage storage = FirebaseStorage.instance;
late final FirebaseAuth _auth;
List<City>? filteredCities;
// variables imagen
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 {
await FirebaseFirestore.instance
.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
Widget build(BuildContext context) {
String city = _ciudad.toString();
@@ -384,32 +429,42 @@ class _ProfileWebScreenState extends State<ProfileWebScreen> {
_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<List<City>>(
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<City> filteredCities = snapshot.data!;
if (ciudad != null) {
setState(() {
_ciudad = ciudad;
});
return DropdownButtonFormField<String>(
value: _ciudad, // Valor seleccionado actualmente
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),
TextFormField(