forked from yaso_meth/mih-project
Enable search function on patient manager
This commit is contained in:
@@ -3,9 +3,11 @@ import 'package:patient_manager/objects/patients.dart';
|
||||
|
||||
class BuildPatientsList extends StatefulWidget {
|
||||
final List<Patient> patients;
|
||||
final searchString;
|
||||
const BuildPatientsList({
|
||||
super.key,
|
||||
required this.patients,
|
||||
required this.searchString,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -18,11 +20,15 @@ class _BuildPatientsListState extends State<BuildPatientsList> {
|
||||
return ListView.builder(
|
||||
itemCount: widget.patients.length,
|
||||
itemBuilder: (context, index) {
|
||||
final patient = widget.patients[index];
|
||||
return ListTile(
|
||||
title: Text(patient.first_name + " " + patient.last_name),
|
||||
subtitle: Text(patient.id_no),
|
||||
);
|
||||
//final patient = widget.patients[index].id_no.contains(widget.searchString);
|
||||
return widget.patients[index].id_no.contains(widget.searchString)
|
||||
? ListTile(
|
||||
title: Text(widget.patients[index].first_name +
|
||||
" " +
|
||||
widget.patients[index].last_name),
|
||||
subtitle: Text(widget.patients[index].id_no),
|
||||
)
|
||||
: Container();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
42
Frontend/patient_manager/lib/components/mySearchInput.dart
Normal file
42
Frontend/patient_manager/lib/components/mySearchInput.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class MySearchField extends StatelessWidget {
|
||||
final controller;
|
||||
final String hintText;
|
||||
final void Function(String)? onChanged;
|
||||
|
||||
const MySearchField({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.hintText,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 25.0),
|
||||
child: TextField(
|
||||
onChanged: onChanged,
|
||||
controller: controller,
|
||||
obscureText: false,
|
||||
decoration: InputDecoration(
|
||||
fillColor: Colors.white,
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
filled: true,
|
||||
hintText: hintText,
|
||||
hintStyle: TextStyle(color: Colors.blueGrey[400]),
|
||||
enabledBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.blueAccent,
|
||||
width: 2.0,
|
||||
),
|
||||
),
|
||||
focusedBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(color: Colors.blue),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,24 +5,17 @@ import 'package:flutter/material.dart';
|
||||
import 'package:patient_manager/components/buildPatientList.dart';
|
||||
import 'package:patient_manager/components/myAppBar.dart';
|
||||
import 'package:patient_manager/components/myAppDrawer.dart';
|
||||
import 'package:patient_manager/main.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mysql_client/mysql_client.dart';
|
||||
import 'package:patient_manager/components/mySearchInput.dart';
|
||||
import 'package:patient_manager/objects/patients.dart';
|
||||
|
||||
Future<List<Patient>> fetchPatients(String endpoint) async {
|
||||
print("fetch patients");
|
||||
final response = await http.get(Uri.parse(endpoint));
|
||||
//print("Status Code: " + response.statusCode.toString());
|
||||
if (response.statusCode == 200) {
|
||||
Iterable l = jsonDecode(response.body);
|
||||
List<Patient> patients =
|
||||
List<Patient>.from(l.map((model) => Patient.fromJson(model)));
|
||||
print("convert response to json");
|
||||
//print(response.body);
|
||||
// final patientsData =
|
||||
// Patient.fromJson(jsonDecode(response.body)[0] as Map<String, dynamic>);
|
||||
print(patients);
|
||||
return patients;
|
||||
} else {
|
||||
throw Exception('failed to load patients');
|
||||
@@ -42,36 +35,25 @@ class PatientManager extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PatientManagerState extends State<PatientManager> {
|
||||
//String useremail = "";
|
||||
TextEditingController searchController = TextEditingController();
|
||||
String endpoint = "http://localhost:80/patients/user/";
|
||||
late MySQLConnection conn;
|
||||
String resultsofDB = "";
|
||||
late Future<List<Patient>> futurePatients;
|
||||
String searchString = "";
|
||||
|
||||
// Future<String> getuserEmail() async {
|
||||
// final res = await client.auth.getUser();
|
||||
// //final response = await http.get(Uri.parse(endpoint));
|
||||
// //print(json.decode(response.body));
|
||||
// if (res.user!.email != null) {
|
||||
// //print("User: " + res.user!.email.toString());
|
||||
// useremail = res.user!.email!;
|
||||
@override
|
||||
void initState() {
|
||||
futurePatients = fetchPatients(endpoint + widget.userEmail);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
// //print(useremail);
|
||||
// return useremail;
|
||||
// }
|
||||
// return "";
|
||||
// }
|
||||
|
||||
// @override
|
||||
// void initState() {
|
||||
// // String endpoint = "http://localhost:80/patients/";
|
||||
// print("init now");
|
||||
// print("Endpoint 1: " + endpoint);
|
||||
// //print(getuserEmail());
|
||||
// endpoint += userEmail;
|
||||
// print("Endpoint 1: " + endpoint);
|
||||
// futurePatients = fetchPatients(endpoint);
|
||||
// super.initState();
|
||||
// void filterSearchResults(String query) {
|
||||
// setState(() {
|
||||
// futurePatients = futurePatientsList
|
||||
// .where((item) => item.toLowerCase().contains(query.toLowerCase()))
|
||||
// .toList();
|
||||
// });
|
||||
// }
|
||||
|
||||
@override
|
||||
@@ -80,38 +62,39 @@ class _PatientManagerState extends State<PatientManager> {
|
||||
appBar: const MyAppBar(barTitle: "Patient Manager"),
|
||||
drawer: MyAppDrawer(drawerTitle: widget.userEmail),
|
||||
body: FutureBuilder(
|
||||
future: fetchPatients(endpoint + widget.userEmail),
|
||||
future: futurePatients,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const CircularProgressIndicator();
|
||||
} else if (snapshot.hasData) {
|
||||
final patientsList = snapshot.data!;
|
||||
return BuildPatientsList(
|
||||
patients: patientsList,
|
||||
);
|
||||
|
||||
return Column(children: [
|
||||
//spacer
|
||||
const SizedBox(height: 10),
|
||||
MySearchField(
|
||||
controller: searchController,
|
||||
hintText: "ID Search",
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
searchString = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
//spacer
|
||||
const SizedBox(height: 10),
|
||||
Expanded(
|
||||
child: BuildPatientsList(
|
||||
patients: patientsList,
|
||||
searchString: searchString,
|
||||
),
|
||||
),
|
||||
]);
|
||||
} else {
|
||||
return const MyAppDrawer(drawerTitle: "Error pulling email");
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
// FutureBuilder<Patient>(
|
||||
// future: futurePatients,
|
||||
// builder: (context, snapshot) {
|
||||
// if (snapshot.hasData) {
|
||||
// return Scaffold(
|
||||
// appBar: const MyAppBar(barTitle: "Patient Manager"),
|
||||
// body: Center(child: Text(snapshot.data.toString())),
|
||||
// drawer: MyAppDrawer(drawerTitle: useremail),
|
||||
// );
|
||||
// } else if (snapshot.hasError) {
|
||||
// print('${snapshot.error}');
|
||||
// return Text('${snapshot.error}');
|
||||
// } else {
|
||||
// return Center(child: CircularProgressIndicator());
|
||||
// }
|
||||
// },
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user