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:healthcare_user/common/zoom_image.dart'; import 'package:image_picker/image_picker.dart'; class ReportImages extends StatefulWidget { var imageDetails; var recordId; ReportImages({this.imageDetails,this.recordId}); @override State createState() => _ReportImagesState(); } class _ReportImagesState extends State { final ImagePicker imagePicker = ImagePicker(); List imageFileList = []; List uiFindingsImages = []; final ImagePicker _picker = ImagePicker(); /*Future pickImageFromGallery() async { imageFileList = []; final List? selectedImages = await imagePicker.pickMultiImage(); AppSettings.preLoaderDialog(context); if (selectedImages!.isNotEmpty) { imageFileList.addAll(selectedImages); } var res = await AppSettings.uploadReportsGallery(imageFileList); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); setState(() { widget.imageDetails = jsonDecode(res)['reports']; }); } 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.uploadReportsCamera(image); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); setState(() { widget.imageDetails = jsonDecode(res)['reports']; }); } on PlatformException catch (e) { print('Failed to pick image: $e'); } }*/ Future pickImageFromGalleryForUpdate() async { imageFileList = []; final List? selectedImages = await imagePicker.pickMultiImage(); AppSettings.preLoaderDialog(context); if (selectedImages!.isNotEmpty) { imageFileList.addAll(selectedImages); } var res = await AppSettings.updateReportsGallery(imageFileList,widget.recordId); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); setState(() { widget.imageDetails = jsonDecode(res)['reports']; }); } Future takeImageFromCameraForUpdate() 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.updateReportsCamera(image,widget.recordId); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); setState(() { widget.imageDetails = jsonDecode(res)['reports']; }); } on PlatformException catch (e) { print('Failed to pick image: $e'); } } Widget renderUi() { if(widget.imageDetails.length!=0){ return Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded(child: GridView.builder( itemCount: widget.imageDetails.length, gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 3, crossAxisSpacing: 2.0, mainAxisSpacing: 2.0, ), itemBuilder: (BuildContext context, int index) { return GestureDetector( onTap: () { Navigator.push( context, new MaterialPageRoute( builder: (__) => new ImageZoomPage( imageName: 'Findings', imageDetails: widget.imageDetails[index]['url']))); /*gridOntap(index);*/ }, 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(widget.imageDetails[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 = widget.imageDetails[index]['url'].split('/').last; var payload = new Map(); payload["urlType"] = 'reports'; payload["url"] = widget.imageDetails[index]['url']; try{ var res = await AppSettings.deleteRecordsNew(payload,widget.recordId); print(jsonDecode(res)); Navigator.of(context, rootNavigator: true).pop(); Navigator.of(context).pop(true); AppSettings.longSuccessToast("Image deleted Successfully"); setState(() { widget.imageDetails = 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, )), ), ], ), ); }, ), /* color: Colors.pinkAccent, width: 35, height: 35,*/ ), ) ]), ), //Image.network(widget.imageDetails[index]['url']), ); }, )), Padding( padding: EdgeInsets.fromLTRB(8, 8, 8, 8), child: CircleAvatar( backgroundColor: primaryColor, radius: 40, child: Column( mainAxisSize: MainAxisSize.min, children: [ IconButton( iconSize: 40, icon: const Icon( Icons.add, color: Colors.white, ), 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 takeImageFromCameraForUpdate(); Navigator.pop(context); }, ), SizedBox( width: MediaQuery.of(context).size.width * .20, ), GestureDetector( child: Icon( Icons.photo, size: 100, color: primaryColor, ), onTap: () async { await pickImageFromGalleryForUpdate(); Navigator.pop(context); }, ), ], ), ), ); }); }, ), ], ), ), ), ], ); } 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 Report'), SizedBox( height: 20, ), CircleAvatar( backgroundColor: primaryColor, radius: 40, child: IconButton( iconSize: 40, icon: const Icon( Icons.add, color: Colors.white, ), 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 takeImageFromCameraForUpdate(); Navigator.pop(context); }, ), SizedBox( width: MediaQuery.of(context).size.width * .20, ), GestureDetector( child: Icon( Icons.photo, size: 100, color: primaryColor, ), onTap: () async { await pickImageFromGalleryForUpdate(); Navigator.pop(context); }, ), ], ), ), ); }); }, ), ) ], ), ) ); } } @override Widget build(BuildContext context) { return Scaffold( appBar:AppSettings.appBar('Repoprt Images'), body: Container(padding: EdgeInsets.all(12.0), child: renderUi()), ); } }