Files
mih-project/Frontend/patient_manager/lib/pages/patientManager.dart
2024-07-05 17:48:37 +02:00

148 lines
4.5 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:patient_manager/components/buildPatientList.dart';
import 'package:patient_manager/components/myAppBar.dart';
import 'package:http/http.dart' as http;
import 'package:patient_manager/components/mySearchInput.dart';
import 'package:patient_manager/components/patManAppDrawer.dart';
import 'package:patient_manager/objects/patients.dart';
class PatientManager extends StatefulWidget {
final String userEmail;
const PatientManager({
super.key,
required this.userEmail,
});
@override
State<PatientManager> createState() => _PatientManagerState();
}
class _PatientManagerState extends State<PatientManager> {
TextEditingController searchController = TextEditingController();
String endpoint = "http://localhost:80/patients/user/";
late Future<List<Patient>> futurePatients;
// late Widget displayWidget;
//List<Patient> tempList;
String searchString = "";
Future<List<Patient>> fetchPatients(String endpoint) async {
//print("Patien manager page: $endpoint");
final response = await http.get(Uri.parse(endpoint));
//print(response.statusCode);
if (response.statusCode == 200) {
Iterable l = jsonDecode(response.body);
List<Patient> patients =
List<Patient>.from(l.map((model) => Patient.fromJson(model)));
return patients;
} else {
throw Exception('failed to load patients');
}
}
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
void initState() {
futurePatients = fetchPatients(endpoint + widget.userEmail);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const MyAppBar(barTitle: "Patient Manager"),
drawer: PatManAppDrawer(userEmail: widget.userEmail),
//floatingActionButtonLocation: FloatingActionButtonLocation.endTop,
floatingActionButton: FloatingActionButton.extended(
label: const Text(
"Add Patient",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
backgroundColor: Colors.blueAccent,
onPressed: () {
Navigator.of(context)
.pushNamed('/patient-manager/add', arguments: widget.userEmail);
},
icon: const Icon(
Icons.add,
color: Colors.white,
),
),
body: Row(
children: [
Expanded(
child: Column(children: [
//spacer
const SizedBox(height: 10),
MySearchField(
controller: searchController,
hintText: "ID Search",
required: false,
editable: true,
onTap: () {},
onChanged: (value) {
setState(() {
searchString = value;
});
},
),
//spacer
const SizedBox(height: 10),
FutureBuilder(
future: futurePatients,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasData) {
List<Patient> patientsList;
if (searchString == "") {
patientsList = snapshot.data!;
} else {
patientsList =
filterSearchResults(snapshot.data!, searchString);
}
return Expanded(
child: displayList(patientsList, searchString),
);
} else {
return const PatManAppDrawer(
userEmail: "Error pulling email");
}
},
),
]),
),
],
),
);
}
}