import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:doctor/common/settings.dart'; import 'package:doctor/patient_dashboard/prescriptions/prescriptions.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 { String? customerId; String? patName; String? patAge; String? patGender; AddPrescription({this.customerId,this.patName,this.patAge,this.patGender}); @override State createState() => _AddPrescriptionState(); } class _AddPrescriptionState extends State { 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,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'); } } 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: [ 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: [ 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( context: context, builder: (BuildContext context) { return SizedBox( height: 200, child: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ 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=widget.patName!; _age=widget.patAge!; _gender=widget.patGender!; } AppSettings.preLoaderDialog(context); bool isOnline = await AppSettings.internetConnectivity(); if(isOnline){ var payload = new Map(); 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,widget.customerId); try{ if(uploadStatus){ Navigator.of(context,rootNavigator: true).pop(); AppSettings.longSuccessToast('Prescription added successfully'); Navigator.pop(context); /* await Navigator.push( context, MaterialPageRoute( builder: (context) => Prescriptions()), );*/ } 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'), )), ], ), )) , ) ); } }