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'; import 'package:prosappco/src/components/pop_appbar.dart'; import 'package:prosappco/src/screens/support.dart'; final CollectionReference professionsCollection = FirebaseFirestore.instance.collection('professions'); Future> getProfessions() async { try { DocumentSnapshot profession = await professionsCollection.doc('professions').get(); Map data = profession.data() as Map; var professionsList = (data['professions'] as List) .map((e) => e.toString()) .toList(); return professionsList; } catch (e) { print('$e'); } return []; } class ProfessionScreen extends StatefulWidget { const ProfessionScreen({super.key}); @override State createState() => _ProfessionScreenState(); } class _ProfessionScreenState extends State { List? filteredProfessions; TextEditingController searchController = TextEditingController(); final User? user = FirebaseAuth.instance.currentUser; List? _professions; final uid = AuthenticationRepository.instance.getCurrentUserUid(); @override void initState() { super.initState(); searchController.addListener(() { setState(() { if (_professions != null) { if (searchController.text.isEmpty) { filteredProfessions = _professions!; } else { filteredProfessions = _professions! .where((profession) => removeDiacritics(profession) .toLowerCase() .contains( removeDiacritics(searchController.text.toLowerCase()))) .toList(); } } }); }); if (_professions == null) { getProfessions().then((List element) => setState(() { _professions = element; filteredProfessions = element; })); } } Future updateProfession(String profession) async { try { await FirebaseFirestore.instance .collection('users') .doc(uid) .update({'profesion': profession}); } catch (e) { try { await FirebaseFirestore.instance .collection('users') .doc(uid) .set({'profesion': profession}); } catch (e) { print('Error al agregar la profesion: $e'); } print('Error al actualizar la profesion: $e'); } } @override Widget build(BuildContext context) { if (filteredProfessions == null) { return const Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation(Color(0xFF2BA4EC)), ), ); } var professions = filteredProfessions!; return SafeArea( child: Scaffold( appBar: PopAppbar( onPressed: () { Navigator.pop(context); }, label: 'Seleccione su profesión', ), body: Column( children: [ GestureDetector( onTap: () { // Aquí puedes agregar la navegación a la vista deseada Navigator.push( context, MaterialPageRoute( builder: (context) => const SupportScreen()), // Reemplaza "OtraVista()" con el nombre de tu vista ); }, child: Container( padding: const EdgeInsets.all(10), child: const Text( 'Si tu profesión no aparece, haz clic aquí', style: TextStyle( fontSize: 16.0, color: Colors.blue, ), textAlign: TextAlign.center, ), ), ), Padding( padding: const EdgeInsets.only(left: 10, right: 10, top: 0), child: TextField( controller: searchController, decoration: const InputDecoration( hintText: 'Busca tu profesión', prefixIcon: Icon(Icons.assignment_ind_rounded), ), ), ), Expanded( child: ListView.builder( itemCount: professions.length, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text( professions[index], style: const TextStyle( fontSize: 18.0, color: Colors.black, ), ), onTap: () { updateProfession(professions[index]); Navigator.pop(context, professions[index]); }, ); }, ), ), ], ), ), ); } }