Patient Manager search function fixed to display only matches

This commit is contained in:
2024-07-05 15:25:27 +02:00
parent 712a7db2e8
commit 3a509dd721
2 changed files with 74 additions and 41 deletions

View File

@@ -15,18 +15,17 @@ class BuildPatientsList extends StatefulWidget {
State<BuildPatientsList> createState() => _BuildPatientsListState(); State<BuildPatientsList> createState() => _BuildPatientsListState();
} }
int indexOn = 0;
class _BuildPatientsListState extends State<BuildPatientsList> { class _BuildPatientsListState extends State<BuildPatientsList> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListView.separated( return ListView.separated(
separatorBuilder: (BuildContext context, int index) { separatorBuilder: (BuildContext context, index) {
return const Divider(); return const Divider();
}, },
itemCount: widget.patients.length, itemCount: widget.patients.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
//final patient = widget.patients[index].id_no.contains(widget.searchString); //final patient = widget.patients[index].id_no.contains(widget.searchString);
//print(index);
return widget.patients[index].id_no.contains(widget.searchString) return widget.patients[index].id_no.contains(widget.searchString)
? ListTile( ? ListTile(
title: Text( title: Text(
@@ -40,7 +39,7 @@ class _BuildPatientsListState extends State<BuildPatientsList> {
}, },
trailing: const Icon(Icons.arrow_forward), trailing: const Icon(Icons.arrow_forward),
) )
: Container(); : null;
}, },
); );
} }

View File

@@ -25,6 +25,8 @@ class _PatientManagerState extends State<PatientManager> {
TextEditingController searchController = TextEditingController(); TextEditingController searchController = TextEditingController();
String endpoint = "http://localhost:80/patients/user/"; String endpoint = "http://localhost:80/patients/user/";
late Future<List<Patient>> futurePatients; late Future<List<Patient>> futurePatients;
// late Widget displayWidget;
//List<Patient> tempList;
String searchString = ""; String searchString = "";
Future<List<Patient>> fetchPatients(String endpoint) async { Future<List<Patient>> fetchPatients(String endpoint) async {
@@ -41,9 +43,32 @@ class _PatientManagerState extends State<PatientManager> {
} }
} }
List<Patient> filterSearchResults(List<Patient> mainList, String query) {
return mainList
.where((tempList) => tempList.id_no.contains(query.toLowerCase()))
.toList();
}
// Widget displayList(List<Patient> patientsList, String searchString) {
// if (searchString.isNotEmpty && searchString != "") {
// return BuildPatientsList(
// patients: patientsList,
// searchString: searchString,
// );
// }
// return const Center(
// child: Text(
// "Please search for a Patient.",
// style: TextStyle(fontSize: 25, color: Colors.grey),
// textAlign: TextAlign.center,
// ),
// );
// }
@override @override
void initState() { void initState() {
futurePatients = fetchPatients(endpoint + widget.userEmail); futurePatients = fetchPatients(endpoint + widget.userEmail);
//displayWidget = displayList(tempList, searchString);
super.initState(); super.initState();
} }
@@ -71,15 +96,7 @@ class _PatientManagerState extends State<PatientManager> {
color: Colors.white, color: Colors.white,
), ),
), ),
body: FutureBuilder( body: Row(
future: futurePatients,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasData) {
final patientsList = snapshot.data!;
return Row(
children: [ children: [
Expanded( Expanded(
child: Column(children: [ child: Column(children: [
@@ -99,20 +116,37 @@ class _PatientManagerState extends State<PatientManager> {
), ),
//spacer //spacer
const SizedBox(height: 10), const SizedBox(height: 10),
Expanded( //Expanded(child: displayWidget),
FutureBuilder(
future: futurePatients,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasData) {
List<Patient> patientsList;
//print(patientsList);
if (searchString == "") {
patientsList = snapshot.data!;
} else {
patientsList =
filterSearchResults(snapshot.data!, searchString);
}
return Expanded(
child: BuildPatientsList( child: BuildPatientsList(
patients: patientsList, patients: patientsList,
searchString: searchString, searchString: searchString,
), ),
);
} else {
return const PatManAppDrawer(
userEmail: "Error pulling email");
}
},
), ),
]), ]),
), ),
], ],
);
} else {
return const PatManAppDrawer(userEmail: "Error pulling email");
}
},
), ),
); );
} }