update
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:diacritic/diacritic.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:prosappco/src/authentication/authentication_repository.dart';
|
||||
import 'package:prosappco/src/components/photo_view.dart';
|
||||
import 'package:prosappco/src/components/pop_appbar.dart';
|
||||
import 'package:prosappco/src/screens/professional_info.dart';
|
||||
|
||||
class ProfessionalScreen extends StatefulWidget {
|
||||
const ProfessionalScreen({super.key});
|
||||
|
||||
@override
|
||||
State<ProfessionalScreen> createState() => _ProfessionalScreenState();
|
||||
}
|
||||
|
||||
final FirebaseStorage storage = FirebaseStorage.instance;
|
||||
|
||||
final CollectionReference usersCollection =
|
||||
FirebaseFirestore.instance.collection('users');
|
||||
var _photo = '...';
|
||||
|
||||
class Professional {
|
||||
final Reference professionalRef;
|
||||
final String name;
|
||||
final String professionName;
|
||||
final String cityName;
|
||||
final String ubicacion;
|
||||
final List<String> professionalEspecializado;
|
||||
|
||||
Professional({
|
||||
required this.professionalRef,
|
||||
required this.name,
|
||||
required this.professionName,
|
||||
required this.cityName,
|
||||
required this.ubicacion,
|
||||
required this.professionalEspecializado,
|
||||
});
|
||||
|
||||
String getEspecializaciones() {
|
||||
return professionalEspecializado.join(',\n');
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Professional {\n'
|
||||
' professionalRef: $professionalRef,\n'
|
||||
' name: $name,\n'
|
||||
' professionName: $professionName,\n'
|
||||
' cityName: $cityName,\n'
|
||||
' ubicacion: $ubicacion,\n'
|
||||
' professionalEspecializado: ${getEspecializaciones()}\n'
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Professional>> getProfessionals() async {
|
||||
List<Professional> professionals = [];
|
||||
|
||||
try {
|
||||
QuerySnapshot users = await usersCollection.get();
|
||||
for (DocumentSnapshot user in users.docs) {
|
||||
// Acceder a los datos de cada usuario
|
||||
Map<String, dynamic> data = user.data() as Map<String, dynamic>;
|
||||
|
||||
// Crear un objeto Professional a partir de los datos del usuario
|
||||
_photo = await AuthenticationRepository.instance.getPhoto(user.id);
|
||||
|
||||
if (data['estado'] == 'activo') {
|
||||
List<String> especializaciones;
|
||||
|
||||
especializaciones = (data['especializaciones'] as List<dynamic>)
|
||||
.map((e) => e.toString())
|
||||
.toList();
|
||||
|
||||
Professional professional = Professional(
|
||||
name: data['name'],
|
||||
professionName: data['profesion'],
|
||||
cityName: data['city'],
|
||||
professionalRef: storage.ref().child(_photo),
|
||||
professionalEspecializado: especializaciones,
|
||||
ubicacion: data['ubicacion'],
|
||||
);
|
||||
professionals.add(professional);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error al obtener profesionales: $e');
|
||||
}
|
||||
|
||||
return professionals;
|
||||
}
|
||||
|
||||
class _ProfessionalScreenState extends State<ProfessionalScreen> {
|
||||
List<Professional>? filteredProfessionals;
|
||||
|
||||
TextEditingController searchController = TextEditingController();
|
||||
|
||||
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
||||
List<Professional>? _professionals;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
searchController.addListener(() {
|
||||
setState(() {
|
||||
if (_professionals != null) {
|
||||
if (searchController.text.isEmpty) {
|
||||
filteredProfessionals = _professionals!;
|
||||
} else {
|
||||
filteredProfessionals = _professionals!
|
||||
.where((professional) => removeDiacritics(professional.name)
|
||||
.toLowerCase()
|
||||
.contains(
|
||||
removeDiacritics(searchController.text.toLowerCase())))
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (_professionals == null) {
|
||||
getProfessionals().then((List<Professional> element) => setState(() {
|
||||
_professionals = element;
|
||||
filteredProfessionals = element;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (filteredProfessionals == null) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(
|
||||
valueColor: AlwaysStoppedAnimation<Color>(Color(0xFF2BA4EC)),
|
||||
),
|
||||
);
|
||||
}
|
||||
var professionals = filteredProfessionals!;
|
||||
|
||||
return SafeArea(
|
||||
child: Scaffold(
|
||||
appBar: PopAppbar(
|
||||
onPressed: () {
|
||||
Navigator.pop(context);
|
||||
},
|
||||
label: 'Seleccione un profesional'),
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: TextField(
|
||||
controller: searchController,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Escriba un nombre',
|
||||
prefixIcon: Icon(Icons.assignment_ind_rounded),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: professionals.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return ListTile(
|
||||
leading: GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
CupertinoPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
return ProfessionalInfoScreen(
|
||||
professional: professionals[index],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
child: ReferencePhoto(
|
||||
ref: professionals[index].professionalRef,
|
||||
sizeCircle: 50,
|
||||
size: 50,
|
||||
sizeIcon: 35,
|
||||
),
|
||||
),
|
||||
trailing: GestureDetector(
|
||||
child: const Icon(Icons.keyboard_arrow_right),
|
||||
onTap: () {
|
||||
Navigator.of(context).push(
|
||||
CupertinoPageRoute(
|
||||
builder: (BuildContext context) {
|
||||
return ProfessionalInfoScreen(
|
||||
professional: professionals[index],
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
title: RichText(
|
||||
text: TextSpan(
|
||||
style: const TextStyle(
|
||||
fontSize: 15.0,
|
||||
color: Colors.black,
|
||||
),
|
||||
children: <TextSpan>[
|
||||
TextSpan(
|
||||
text: '${professionals[index].name}, ',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
TextSpan(
|
||||
text:
|
||||
"${professionals[index].professionName}, ${professionals[index].cityName}",
|
||||
style: TextStyle(color: Colors.grey[600]),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
// Navigator.pop(context, professionals[index]);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user