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.
healthcare-frontend/lib/prescriptions/add_prescriptions.dart

409 lines
17 KiB

import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:healthcare_user/common/settings.dart';
import 'package:image_picker/image_picker.dart';
import 'package:photo_view/photo_view.dart';
import 'package:pinch_zoom/pinch_zoom.dart';
class AddPrescription extends StatefulWidget {
const AddPrescription({Key? key}) : super(key: key);
@override
State<AddPrescription> createState() => _AddPrescriptionState();
}
class _AddPrescriptionState extends State<AddPrescription> {
final ImagePicker _picker = ImagePicker();
String Url = '';
final _transformationController = TransformationController();
TapDownDetails _doubleTapDetails=TapDownDetails();
TextEditingController prescriptionNameController = TextEditingController();
TextEditingController patientNameController = TextEditingController();
TextEditingController patientAgeController = TextEditingController();
String? prescriptionFor;
String? gender;
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);
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);
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');
}
}
Widget zoomPhoto(var imageUrl){
return Container(
width: MediaQuery.of(context).size.width * .18,
height: MediaQuery.of(context).size.height * .10,
child: PhotoView(
imageProvider: NetworkImage(imageUrl) as ImageProvider,
)
);
}
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();
},
),
],
);
});
},
);
}
void _handleDoubleTap() {
if (_transformationController.value != Matrix4.identity()) {
_transformationController.value = Matrix4.identity();
} else {
final position = _doubleTapDetails.localPosition;
// For a 3x zoom
_transformationController.value = Matrix4.identity()
..translate(-position.dx * 2, -position.dy * 2)
..scale(3.0);
// Fox a 2x zoom
// ..translate(-position.dx, -position.dy)
// ..scale(2.0);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppSettings.appBar('Add Prescription'),
body:SingleChildScrollView(
child: Container(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: primaryColor, // background
onPrimary: Colors.white, // foreground
),
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 takeImageFromCamera();
Navigator.pop(context);
},
),
SizedBox(
width: MediaQuery.of(context).size.width * .20,
),
GestureDetector(
child: Icon(
Icons.photo,
size: 100,
color: primaryColor,
),
onTap: () async {
await pickImageFromGallery();
Navigator.pop(context);
},
),
],
),
),
);
});
},
child: const Text('Select image'),
),
SizedBox(height:MediaQuery.of(context).size.height * .01,),
Visibility(
visible: Url != '',
child: GestureDetector(
child:
/*,*/
/*Container(
width: MediaQuery.of(context).size.width * .18,
height: MediaQuery.of(context).size.height * .10,
child: PhotoView(
imageProvider: NetworkImage(Url) as ImageProvider,
maxScale: PhotoViewComputedScale.contained * 4.0,
minScale: PhotoViewComputedScale.contained,
initialScale: PhotoViewComputedScale.contained,
basePosition: Alignment.center,
)
),*/
Container(
width: MediaQuery.of(context).size.width * .18,
height: MediaQuery.of(context).size.height * .10,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
image: DecorationImage(
image: NetworkImage(Url) as ImageProvider, // picked file
fit: BoxFit.fill)),
),
onTap: (){
showPicDialog(Url);
},
)
),
SizedBox(height:MediaQuery.of(context).size.height * .02,),
Container(
child: TextFormField(
cursorColor: greyColor,
controller: prescriptionNameController,
decoration: textFormFieldDecoration(Icons.edit,'Enter Prescription name'),
),
),
Row(
children: [
Expanded(child: RadioListTile(
title: Text("For Yourself"),
value: "self",
groupValue: prescriptionFor,
activeColor: primaryColor,
onChanged: (value){
setState(() {
prescriptionFor = value.toString();
});
},
),),
Expanded(child: RadioListTile(
title: Text("For Family"),
value: "others",
groupValue: prescriptionFor,
activeColor: primaryColor,
onChanged: (value){
setState(() {
prescriptionFor = value.toString();
});
},
),),
],
),
Visibility(
visible:prescriptionFor.toString().toLowerCase()=='others' ,
child: Container(
child: TextFormField(
cursorColor: greyColor,
controller: patientNameController,
decoration: textFormFieldDecoration(Icons.person,'Enter patient name'),
),
),),
SizedBox(height:MediaQuery.of(context).size.height * .02,),
Visibility(
visible:prescriptionFor.toString().toLowerCase()=='others' ,
child: Container(
child: TextFormField(
cursorColor: greyColor,
keyboardType: TextInputType.number,
controller: patientAgeController,
decoration: textFormFieldDecoration(Icons.person,'Enter patient age'),
),
),),
Visibility(
visible:prescriptionFor.toString().toLowerCase()=='others' ,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(child: RadioListTile(
title: Text("Male",style: TextStyle(fontSize: 10)),
value: "male",
groupValue: gender,
activeColor: primaryColor,
onChanged: (value){
setState(() {
gender = value.toString();
});
},
),),
Expanded(child: RadioListTile(
title: Text("Female",style: TextStyle(fontSize: 10),),
value: "female",
groupValue: gender,
activeColor: primaryColor,
onChanged: (value){
setState(() {
gender = value.toString();
});
},
),),
Expanded(child: RadioListTile(
title: Text("Others",style: TextStyle(fontSize: 10)),
value: "other",
groupValue: gender,
activeColor: primaryColor,
onChanged: (value){
setState(() {
gender = value.toString();
});
},
),),
],
),),
SizedBox(height:MediaQuery.of(context).size.height * .02,),
Container(
width:double.infinity,
height: MediaQuery.of(context).size.height * .06,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: buttonColors, // background
onPrimary: Colors.black, // foreground
),
onPressed: () async{
if(Url!=''&& prescriptionNameController.text!=''&&prescriptionFor!=''){
String _name='';
String _age='';
String? _gender='';
if(prescriptionFor.toString().toLowerCase()=='others'){
if(patientNameController!=''&& patientAgeController.text!=''&&gender!=''){
_name=patientNameController.text;
_age=patientAgeController.text;
_gender=gender;
}
else{
AppSettings.longFailedToast('Please enter details');
}
}
else{
_name=AppSettings.userName;
_age=AppSettings.age;
_gender=AppSettings.gender;
}
AppSettings.preLoaderDialog(context);
bool isOnline = await AppSettings.internetConnectivity();
if(isOnline){
var payload = new Map<String, dynamic>();
payload["name"] = prescriptionNameController.text.toString();
payload["pictureUrl"] = Url.toString();
payload["patientType"] = prescriptionFor.toString();
payload["others"] ={
"name": _name,
"age": int.parse(_age),
"gender": _gender.toString().toLowerCase()
};
bool uploadStatus = await AppSettings.addPrescription(payload);
try{
if(uploadStatus){
Navigator.of(context,rootNavigator: true).pop();
AppSettings.longSuccessToast('Prescription added successfully');
}
else{
Navigator.of(context,rootNavigator: true).pop();
AppSettings.longFailedToast('Fail to add prescription details');
}
}
catch(e){
print(e);
Navigator.of(context,rootNavigator: true).pop();
AppSettings.longFailedToast('Fail to add prescription details');
}
}
else{
AppSettings.longFailedToast('Please check internet');
}
}
else{
AppSettings.longFailedToast('Please upload image');
}
},
child: const Text('Upload'),
)),
],
),
)) ,
)
);
}
}