diff --git a/lib/src/components/drawer_menu.dart b/lib/src/components/drawer_menu.dart index 7a30f6e..30f7576 100644 --- a/lib/src/components/drawer_menu.dart +++ b/lib/src/components/drawer_menu.dart @@ -377,6 +377,8 @@ class DrawerMenu extends StatelessWidget { ElevatedButton( onPressed: () { AuthenticationRepository.instance.logout(uid!); + + userProvider.initUserProvider(); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.red, diff --git a/lib/src/screens/profession.dart b/lib/src/screens/profession.dart index 23fe04d..f5ea760 100644 --- a/lib/src/screens/profession.dart +++ b/lib/src/screens/profession.dart @@ -40,7 +40,9 @@ class _ProfessionScreenState extends State { TextEditingController searchController = TextEditingController(); final User? user = FirebaseAuth.instance.currentUser; List? _professions; + final ScrollController _scrollController = ScrollController(); final uid = AuthenticationRepository.instance.getCurrentUserUid(); + bool isNewProfessionAdded = false; @override void initState() { @@ -90,6 +92,53 @@ class _ProfessionScreenState extends State { } } + Future saveProfession(String newProfession) async { + final DocumentReference professionsDocRef = + professionsCollection.doc('professions'); + + try { + final DocumentSnapshot profession = + await professionsDocRef.get(); + + Map data = profession.data() as Map; + + List professions = []; + + if (data['professions'] != null) { + professions = List.from(data['professions']); + } + + professions.add(newProfession); + + await professionsDocRef.set({ + 'professions': professions, + }, SetOptions(merge: true)); + + setState(() { + getProfessions().then((List element) => setState(() { + _professions = element; + filteredProfessions = element; + int newIndex = professions.indexOf(newProfession); + if (newIndex != -1) { + _scrollController.animateTo( + newIndex * 50.0, + duration: const Duration(milliseconds: 600), + curve: Curves.easeIn, + ); + } + isNewProfessionAdded = true; + Future.delayed(const Duration(seconds: 2), () { + setState(() { + isNewProfessionAdded = false; + }); + }); + })); + }); + } catch (e) { + print('Error al guardar la profesión: $e'); + } + } + @override Widget build(BuildContext context) { if (filteredProfessions == null) { @@ -113,18 +162,38 @@ class _ProfessionScreenState extends State { 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 + String newProfession = ""; + + showDialog( + context: context, + builder: (context) { + return AlertDialog( + title: const Text("Agregar una profesión"), + content: TextField( + controller: TextEditingController(), + onChanged: (value) { + newProfession = value; + }, + ), + actions: [ + ElevatedButton( + onPressed: () { + if (newProfession.isNotEmpty) { + saveProfession(newProfession); + Navigator.pop(context); + } + }, + child: const Text("Guardar"), + ), + ], + ); + }, ); }, child: Container( padding: const EdgeInsets.all(10), child: const Text( - 'Si tu profesión no aparece, haz clic aquí', + 'Si tu profesión no aparece, presiona aquí', style: TextStyle( fontSize: 16.0, color: Colors.blue, @@ -145,16 +214,24 @@ class _ProfessionScreenState extends State { ), Expanded( child: ListView.builder( + controller: _scrollController, itemCount: professions.length, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text( professions[index], - style: const TextStyle( + style: TextStyle( fontSize: 18.0, - color: Colors.black, + color: isNewProfessionAdded && + index == professions.length - 1 + ? Colors.white + : Colors.black, ), ), + tileColor: + isNewProfessionAdded && index == professions.length - 1 + ? Colors.blue + : null, onTap: () { updateProfession(professions[index]); Navigator.pop(context, professions[index]);