You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

977 lines
37 KiB

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:doctor/patient_dashboard//Reports/add_reports.dart';
import 'package:doctor/common/settings.dart';
import 'package:doctor/common/zoom_image.dart';
import 'package:doctor/models/reports_model.dart';
import 'package:image_picker/image_picker.dart';
import 'package:photo_view/photo_view.dart';
import 'package:intl/intl.dart';
class Prescriptions extends StatefulWidget {
String? customerId;
String? patName;
String? patAge;
String? patGender;
Prescriptions({this.customerId,this.patName,this.patAge,this.patGender});
@override
State<Prescriptions> createState() => _PrescriptionsState();
}
class _PrescriptionsState extends State<Prescriptions> {
final ImagePicker _picker = ImagePicker();
String Url = '';
List<ReportsModel> reportsList = [];
List<ReportsModel> reportsListOriginal = [];
bool isPharmacyDataLoading = false;
bool isPrescriptionsDataLoading = false;
bool isSereverIssue = false;
double lat = 0;
double lng = 0;
String userAddress = '';
String dropdownArea = '2';
var AreaItems = ['2', '5', '10', '25', '50', '100'];
List pharmaciesCheckboxes = [];
bool isLoading=false;
final ImagePicker imagePicker = ImagePicker();
List imageFileList = [];
List uiPrescriptionImages = [];
TextEditingController searchController = TextEditingController();
TextEditingController dateInput = TextEditingController();
String dropdownSearchType = 'Problem';
var typeOfSearchItems = [
'Problem',
'Doctor',
'Date',
];
Future<void> getAllRecords() async {
isPrescriptionsDataLoading=true;
try {
var response = await AppSettings.getAllRecords(widget.customerId);
setState(() {
reportsListOriginal = ((jsonDecode(response)) as List)
.map((dynamic model) {
return ReportsModel.fromJson(model);
}).toList();
reportsList=reportsListOriginal.reversed.toList();
isPrescriptionsDataLoading = false;
});
} catch (e) {
setState(() {
isPrescriptionsDataLoading = false;
isSereverIssue = true;
});
}
}
Future<void> getRecordsByProblemName(var problem) async {
isPrescriptionsDataLoading=true;
try {
var response = await AppSettings.getAllRecords(widget.customerId);
setState(() {
reportsListOriginal = ((jsonDecode(response)) as List)
.map((dynamic model) {
return ReportsModel.fromJson(model);
}).toList();
reportsList=reportsListOriginal.reversed.toList();
reportsList= reportsListOriginal.where(
(x) => x.problem.toString().toLowerCase().contains(problem.toString().toLowerCase())
).toList();
isPrescriptionsDataLoading = false;
});
} catch (e) {
setState(() {
isPrescriptionsDataLoading = false;
isSereverIssue = true;
});
}
}
Future<void> getRecordsByDoctorName(var doctor) async {
isPrescriptionsDataLoading=true;
try {
var response = await AppSettings.getAllRecords(widget.customerId);
setState(() {
reportsListOriginal = ((jsonDecode(response)) as List)
.map((dynamic model) {
return ReportsModel.fromJson(model);
}).toList();
reportsList=reportsListOriginal.reversed.toList();
reportsList= reportsListOriginal.where(
(x) => x.doctorName.toString().toLowerCase().contains(doctor.toString().toLowerCase())
).toList();
isPrescriptionsDataLoading = false;
});
} catch (e) {
setState(() {
isPrescriptionsDataLoading = false;
isSereverIssue = true;
});
}
}
Future<void> getRecordsByDate(var date) async {
isPrescriptionsDataLoading=true;
try {
var response = await AppSettings.getAllRecords(widget.customerId);
setState(() {
reportsListOriginal = ((jsonDecode(response)) as List)
.map((dynamic model) {
return ReportsModel.fromJson(model);
}).toList();
reportsList=reportsListOriginal.reversed.toList();
reportsList= reportsListOriginal.where(
(x) => x.date.toString().toLowerCase().contains(date.toString().toLowerCase())
).toList();
isPrescriptionsDataLoading = false;
});
} catch (e) {
setState(() {
isPrescriptionsDataLoading = false;
isSereverIssue = true;
});
}
}
@override
void initState() {
getAllRecords();
super.initState();
}
Future pickImageFromGallery() async {
try {
final image = await _picker.pickImage(source: ImageSource.gallery);
if (image == null) return;
final imageTemp = File(image.path);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.uploadImageHTTPForPrescriptions(image,widget.customerId);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
setState(() {
Url = jsonDecode(res)['pictures'][0];
});
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future takeImageFromCamera() async {
try {
final image = await _picker.pickImage(source: ImageSource.camera);
if (image == null) return;
final imageTemp = File(image.path);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.uploadImageHTTPForPrescriptions(image,widget.customerId);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
setState(() {
Url = jsonDecode(res)['pictures'][0];
});
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future pickImageFromGalleryForUpdate(var recordId) async {
imageFileList = [];
final List<XFile>? selectedImages = await imagePicker.pickMultiImage();
AppSettings.preLoaderDialog(context);
if (selectedImages!.isNotEmpty) {
imageFileList.addAll(selectedImages);
}
var res = await AppSettings.updatePrescriptionsGallery(imageFileList,recordId,widget.customerId);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
getAllRecords();
}
Future takeImageFromCameraForUpdate(var recordId) async {
try {
final image = await _picker.pickImage(source: ImageSource.camera);
if (image == null) return;
final imageTemp = File(image.path);
AppSettings.preLoaderDialog(context);
var res = await AppSettings.updatePrescriptionsCamera(image,recordId,widget.customerId);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
getAllRecords();
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
showPicDialog(var imageUrl){
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: const Text(''),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * .10,
height: MediaQuery.of(context).size.height * .50,
child: PhotoView(
imageProvider: NetworkImage(imageUrl) as ImageProvider,
maxScale: PhotoViewComputedScale.contained * 4.0,
minScale: PhotoViewComputedScale.contained,
initialScale: PhotoViewComputedScale.contained,
basePosition: Alignment.center,
)
)
],
),
),
actions: <Widget>[
TextButton(
child: Text('Close', style: textButtonStyle()),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
});
},
);
}
Widget prescriptions(var obj){
if(obj.prescriptionImages.length!=0){
return Container(
//color: secondaryColor,
width: double.infinity,
height: MediaQuery.of(context).size.height * .20,
child: Row(
children: [
Expanded(child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: obj.prescriptionImages.length,
itemBuilder: (context, index) {
return Row(
children: [
Card(
child: GestureDetector(
onTap: (){
//showPicDialog(obj.prescriptionImages[index]['url']);
Navigator.push(
context,
new MaterialPageRoute(
builder: (__) => new ImageZoomPage(imageName:'Prescriptions',imageDetails:obj.prescriptionImages[index]['url'])));},
child: Container(
width: MediaQuery.of(context).size.width * .30,
height: MediaQuery.of(context).size.height * .15,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
image: DecorationImage(
image: NetworkImage(
obj.prescriptionImages[index]['url'])
as ImageProvider, // picked file
fit: BoxFit.fill)),
child: Stack(children: [
Positioned(
right: 0,
child: Container(
child: IconButton(
iconSize: 30,
icon: const Icon(
Icons.delete,
color: Colors.red,
),
onPressed: () async {
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext context) => AlertDialog(
title:
const Text('Do you want to delete image?',
style: TextStyle(
color: primaryColor,
fontSize: 20,
)),
actionsAlignment:
MainAxisAlignment.spaceBetween,
actions: [
TextButton(
onPressed: () async {
AppSettings.preLoaderDialog(context);
String fileName = obj.prescriptionImages[index]['url']
.split('/')
.last;
var payload =
new Map<String, dynamic>();
payload["urlType"] = 'prescription';
payload["url"] = obj.prescriptionImages[index]['url'];
try {
var res = await AppSettings.deleteRecordsNew(payload, obj.recordId,widget.customerId);
print(jsonDecode(res));
Navigator.of(context, rootNavigator: true).pop();
Navigator.of(context).pop(true);
AppSettings.longSuccessToast("Image deleted Successfully");
setState(() {
obj.prescriptionImages =
jsonDecode(
res)['remainingUrls'];
});
} catch (e) {
print(e);
Navigator.of(context,
rootNavigator: true)
.pop();
Navigator.of(context).pop(true);
AppSettings.longFailedToast(
"Image deletion failed");
}
},
child: const Text('Yes',
style: TextStyle(
color: primaryColor,
fontSize: 20,
)),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: const Text('No',
style: TextStyle(
color: primaryColor,
fontSize: 20,
)),
),
],
),
);
},
),
),
)
]),
),
),
)
],
);
}),),
IconButton(
iconSize: 40,
icon: const Icon(
Icons.add,
color: Colors.green,
),
onPressed: () async {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return SizedBox(
height: 200,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Icon(
Icons.camera_alt_outlined,
size: 100,
color: primaryColor,
),
onTap: () async {
await takeImageFromCameraForUpdate(obj.recordId);
Navigator.pop(context);
},
),
SizedBox(
width: MediaQuery.of(context).size.width *
.20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: primaryColor,
),
onTap: () async {
await pickImageFromGalleryForUpdate(obj.recordId);
Navigator.pop(context);
},
),
],
),
),
);
});
},
)
],
)
);
}
else{
return Row(
children: [
IconButton(
iconSize: 40,
icon: const Icon(
Icons.add,
color: Colors.green,
),
onPressed: () async {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return SizedBox(
height: 200,
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
GestureDetector(
child: Icon(
Icons.camera_alt_outlined,
size: 100,
color: primaryColor,
),
onTap: () async {
await takeImageFromCameraForUpdate(obj.recordId);
Navigator.pop(context);
},
),
SizedBox(
width: MediaQuery.of(context).size.width *
.20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: primaryColor,
),
onTap: () async {
await pickImageFromGalleryForUpdate(obj.recordId);
Navigator.pop(context);
},
),
],
),
),
);
});
},
),
Text('Add Prescriptions',style: textButtonStyle(),)
],
);
}
}
Widget _filtereddata(){
if (reportsList.length != 0) {
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(child:ListView.builder(
padding: EdgeInsets.all(0),
itemCount: reportsList.length,
itemBuilder: (BuildContext context, int index) {
if(reportsList[index].patient_type.toString().toLowerCase()=='self'){
reportsList[index].age=widget.patAge!;
reportsList[index].gender=widget.patGender!;
reportsList[index].patient_name=widget.patName!;
}
else{
reportsList[index].age=reportsList[index].age;
reportsList[index].gender=reportsList[index].gender;
reportsList[index].patient_name=reportsList[index].patient_name;
}
return Visibility(
visible: true,
child: Card(
//color: prescriptionsList[index].cardColor,
child: Padding(
padding:EdgeInsets.all(8) ,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: MediaQuery.of(context).size.width * .55,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Problem: '+reportsList[index].problem.toString().toUpperCase(),style: problemTextStyle()),
Text(reportsList[index].doctorName.toString().toUpperCase(),style: valuesTextStyle()),
Text(reportsList[index].hospitalName.toString().toUpperCase(),style: valuesTextStyle()),
Text(reportsList[index].date.toString().toUpperCase(),style: valuesTextStyle()),
Text('Patient Details: ',style: problemTextStyle()),
Text(reportsList[index].patient_name.toString().toUpperCase(),style: valuesTextStyle()),
Row(
children: [
Text(reportsList[index].gender.toString().toUpperCase(),style: valuesTextStyle()),
SizedBox(width:MediaQuery.of(context).size.width * .05,),
Text(reportsList[index].age.toString().toUpperCase()+" Yrs",style: valuesTextStyle()),
],
),
],
),
),
],
),
SizedBox(height:MediaQuery.of(context).size.height * .02,),
prescriptions(reportsList[index]),
],
),
),
));
}) ),
]);
}
else{
return Padding(padding: EdgeInsets.fromLTRB(60,10,60,10),
child: Column(
children: [
Text('No prescriptions found related to your search'),
SizedBox(
height: 20,
),
CircleAvatar(
backgroundColor: Colors.red,
radius: 30,
child: const Icon(
Icons.info,
color: Colors.white,
),
)
],
),);
}
}
Widget _allPrescriptions(){
if(reportsListOriginal.length!=0){
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
padding: const EdgeInsets.fromLTRB(10, 10, 10, 0),
child: DropdownButtonFormField(
// Initial Value
value: dropdownSearchType,
isExpanded: true,
decoration: const InputDecoration(
prefixIcon: Icon(
Icons.search,
color: greyColor,
),
border: OutlineInputBorder(
borderSide: BorderSide(color: greyColor)),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: greyColor),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: greyColor),
),
labelText: 'Search By',
labelStyle: TextStyle(
color: greyColor, //<-- SEE HERE
),
),
hint: Text('Select Search Type'),
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: typeOfSearchItems.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
dropdownSearchType = newValue!;
});
},
),
),
Visibility(
visible:dropdownSearchType.toString().toLowerCase()=='problem' ,
child: Container(
height: MediaQuery.of(context).size.height * .07,
padding: EdgeInsets.all(5),
child: Center(child: TextField(
cursorColor: primaryColor,
controller: searchController,
onChanged: (string) {
if(string.length>=1){
getRecordsByProblemName(string);
}
else{
getAllRecords();
}
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.search,
color: primaryColor,
),
/*suffixIcon: Icon(
Icons.clear,
color: greyColor,
),*/
suffixIcon: searchController.text!=''?IconButton(
icon: Icon(
Icons.clear,
color: Colors.red,
),
onPressed: () {
setState(() {
searchController.text='';
});
getAllRecords();
},
):IconButton(
icon: Icon(
Icons.clear,
color: Colors.transparent,
),
onPressed: () {
},
),
border: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
//labelText: 'Search by phone number',
hintText: 'Search by problem',
labelStyle: TextStyle(
color: greyColor, //<-- SEE HERE
),
),
),)
),),
Visibility(
visible:dropdownSearchType.toString().toLowerCase()=='doctor' ,
child: Container(
height: MediaQuery.of(context).size.height * .07,
padding: EdgeInsets.all(5),
child: Center(child: TextField(
cursorColor: primaryColor,
controller: searchController,
onChanged: (string) {
if(string.length>=1){
getRecordsByDoctorName(string);
}
else{
getAllRecords();
}
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.search,
color: primaryColor,
),
/*suffixIcon: Icon(
Icons.clear,
color: greyColor,
),*/
suffixIcon: searchController.text!=''?IconButton(
icon: Icon(
Icons.clear,
color: Colors.red,
),
onPressed: () {
setState(() {
searchController.text='';
});
getAllRecords();
},
):IconButton(
icon: Icon(
Icons.clear,
color: Colors.transparent,
),
onPressed: () {
},
),
border: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
//labelText: 'Search by phone number',
hintText: 'Search by doctor',
labelStyle: TextStyle(
color: greyColor, //<-- SEE HERE
),
),
),)
),),
Visibility(
visible:dropdownSearchType.toString().toLowerCase()=='date' ,
child: Container(
height: MediaQuery.of(context).size.height * .07,
padding: EdgeInsets.all(5),
child: Center(child: TextField(
cursorColor: primaryColor,
controller: searchController,
onChanged: (string) {
if(string.length>=1){
getRecordsByDate(string);
}
else{
getAllRecords();
}
},
onTap: () async {
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1950),
lastDate: DateTime.now(),
builder: (BuildContext context, Widget? child) {
return Theme(
data: ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
primary: buttonColors,
onPrimary: Colors.white,
surface: buttonColors,
onSurface: Colors.white,
),
dialogBackgroundColor: primaryColor,
),
child: child!,
);
},
);
if (pickedDate != null) {
print(
pickedDate); //pickedDate output format => 2021-03-10 00:00:00.000
String formattedDate =
DateFormat('dd-MM-yyyy').format(pickedDate);
print(
formattedDate); //formatted date output using intl package => 2021-03-16
setState(() {
searchController.text = formattedDate; //set output date to TextField value.
});
getRecordsByDate(searchController.text);
} else {}
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.search,
color: primaryColor,
),
suffixIcon: searchController.text!=''?IconButton(
icon: Icon(
Icons.clear,
color: Colors.red,
),
onPressed: () {
setState(() {
searchController.text='';
});
getAllRecords();
},
):IconButton(
icon: Icon(
Icons.clear,
color: Colors.transparent,
),
onPressed: () {
},
),
border: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: primaryColor),
borderRadius: BorderRadius.circular(30),
),
//labelText: 'Search by phone number',
hintText: 'Search by date',
labelStyle: TextStyle(
color: greyColor, //<-- SEE HERE
),
),
),)
),
),
Expanded(child: _filtereddata()),
Padding(
padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
child: CircleAvatar(
backgroundColor: primaryColor,
radius: 40,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
iconSize: 40,
icon: const Icon(
Icons.add,
color: Colors.white,
),
onPressed: () async{
/*await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddPrescription()),
);*/
Navigator.push(context, MaterialPageRoute(builder: (context) => AddReports(customerId:widget.customerId,patName: widget.patName,patAge: widget.patAge,patGender: widget.patGender,))).then((value) {
getAllRecords();
});
//showBoreAddingDialog();
},
),
/* Padding(
padding: EdgeInsets.fromLTRB(5, 0, 5, 5),
child: Text(
'Add Tanks ',
style: TextStyle(color: Colors.white),
),
)*/
],
),
),
),
]);
}
else{
return Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(height: MediaQuery.of(context).size.height * .25,),
Text('Click below icon to add new Record'),
SizedBox(
height: 20,
),
CircleAvatar(
backgroundColor: primaryColor,
radius: 40,
child: IconButton(
iconSize: 40,
icon: const Icon(
Icons.add,
color: Colors.white,
),
onPressed: () async {
Navigator.push(context, MaterialPageRoute(builder: (context) => AddReports(customerId:widget.customerId,patName: widget.patName,patAge: widget.patAge,patGender: widget.patGender,))).then((value) {
getAllRecords();
});
},
),
)
],
),
)
);
}
}
/**/
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppSettings.appBar('Prescriptions'),
body: isPrescriptionsDataLoading?Center(
child: CircularProgressIndicator(
color: primaryColor,
strokeWidth: 5.0,
),
): _allPrescriptions(),
);
}
}