202 lines
5.8 KiB
Dart
202 lines
5.8 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:diacritic/diacritic.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:prosappco/src/authentication/authentication_repository.dart';
|
|
|
|
class CityScreen extends StatefulWidget {
|
|
const CityScreen({super.key});
|
|
|
|
@override
|
|
State<CityScreen> createState() => _CityScreenState();
|
|
}
|
|
|
|
final CollectionReference countriesCollection =
|
|
FirebaseFirestore.instance.collection('countries');
|
|
|
|
class City {
|
|
String? cityName;
|
|
String? stateOfCity;
|
|
String? countryOfCity;
|
|
|
|
City({this.cityName, this.stateOfCity, this.countryOfCity});
|
|
@override
|
|
String toString() {
|
|
return "${cityName ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}";
|
|
}
|
|
}
|
|
|
|
Future<List<City>> getCountries() 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, List<String>> states = {};
|
|
|
|
for (var entry in data.entries) {
|
|
String key = entry.key;
|
|
List<String> value = List<String>.from(entry.value);
|
|
states[key] = value;
|
|
}
|
|
|
|
for (var state in states.entries) {
|
|
var citysState = state.value
|
|
.map((cityName) => City(
|
|
cityName: cityName,
|
|
stateOfCity: state.key,
|
|
countryOfCity: countryName))
|
|
.toList();
|
|
|
|
citys.addAll(citysState);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print('xd $e');
|
|
}
|
|
|
|
return citys;
|
|
}
|
|
|
|
class _CityScreenState extends State<CityScreen> {
|
|
List<City>? filteredCities;
|
|
|
|
TextEditingController searchController = TextEditingController();
|
|
final User? user = FirebaseAuth.instance.currentUser;
|
|
|
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
|
List<City>? _cities;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
searchController.addListener(() {
|
|
setState(() {
|
|
if (_cities != null) {
|
|
if (searchController.text.isEmpty) {
|
|
filteredCities = _cities!;
|
|
} else {
|
|
filteredCities = _cities!
|
|
.where((city) => removeDiacritics(city.cityName!)
|
|
.toLowerCase()
|
|
.contains(
|
|
removeDiacritics(searchController.text.toLowerCase())))
|
|
.toList();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
if (_cities == null) {
|
|
getCountries().then((List<City> element) => setState(() {
|
|
_cities = element;
|
|
filteredCities = element;
|
|
}));
|
|
}
|
|
}
|
|
|
|
Future<void> updateCity(String cityName) async {
|
|
try {
|
|
await FirebaseFirestore.instance
|
|
.collection('users')
|
|
.doc(uid)
|
|
.update({'city': cityName});
|
|
|
|
print('Ciudad actualizada correctamente');
|
|
} catch (e) {
|
|
try {
|
|
await FirebaseFirestore.instance
|
|
.collection('users')
|
|
.doc(uid)
|
|
.set({'city': cityName});
|
|
} catch (e) {
|
|
print('Error al agregar la ciudad: $e');
|
|
}
|
|
|
|
print('Error al actualizar la ciudad: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (filteredCities == null) {
|
|
return const Center(
|
|
child: CircularProgressIndicator(
|
|
valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
|
|
),
|
|
);
|
|
}
|
|
var citys = filteredCities!;
|
|
|
|
return SafeArea(
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.white,
|
|
leading: IconButton(
|
|
icon: Icon(Icons.arrow_back),
|
|
onPressed: () {
|
|
Navigator.pushReplacementNamed(context, '/profile');
|
|
},
|
|
),
|
|
iconTheme: IconThemeData(
|
|
color: Colors.black,
|
|
),
|
|
title: Text(
|
|
'Seleccione su ciudad',
|
|
style: TextStyle(
|
|
color: Colors.black,
|
|
),
|
|
)),
|
|
body: Column(
|
|
children: [
|
|
Padding(
|
|
padding: EdgeInsets.only(left: 10, right: 10, top: 10),
|
|
child: TextField(
|
|
controller: searchController,
|
|
decoration: InputDecoration(
|
|
hintText: 'Buscar ciudad',
|
|
prefixIcon: Icon(Icons.near_me),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: citys.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return ListTile(
|
|
title: RichText(
|
|
text: TextSpan(
|
|
style: const TextStyle(
|
|
fontSize: 18.0,
|
|
color: Colors.black,
|
|
),
|
|
children: <TextSpan>[
|
|
TextSpan(
|
|
text: '${citys[index].cityName ?? ""}, ',
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
TextSpan(
|
|
text:
|
|
"${citys[index].stateOfCity ?? ""}, ${citys[index].countryOfCity ?? ""}",
|
|
style: TextStyle(color: Colors.grey[600]),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
onTap: () {
|
|
updateCity(citys[index].cityName ?? "");
|
|
Navigator.pushReplacementNamed(context, '/profile');
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|