Password expired fixes & ITG Attachments implementation
							parent
							
								
									43e1e4df6b
								
							
						
					
					
						commit
						c7ff754dd8
					
				| @ -0,0 +1,25 @@ | ||||
| class ITGFormsAttachmentsModel { | ||||
|   String? fileBase64; | ||||
|   dynamic fileData; | ||||
|   String? fileName; | ||||
|   String? fileType; | ||||
| 
 | ||||
|   ITGFormsAttachmentsModel( | ||||
|       {this.fileBase64, this.fileData, this.fileName, this.fileType}); | ||||
| 
 | ||||
|   ITGFormsAttachmentsModel.fromJson(Map<String, dynamic> json) { | ||||
|     fileBase64 = json['fileBase64']; | ||||
|     fileData = json['fileData']; | ||||
|     fileName = json['fileName']; | ||||
|     fileType = json['fileType']; | ||||
|   } | ||||
| 
 | ||||
|   Map<String, dynamic> toJson() { | ||||
|     Map<String, dynamic> data = new Map<String, dynamic>(); | ||||
|     data['fileBase64'] = this.fileBase64; | ||||
|     data['fileData'] = this.fileData; | ||||
|     data['fileName'] = this.fileName; | ||||
|     data['fileType'] = this.fileType; | ||||
|     return data; | ||||
|   } | ||||
| } | ||||
| @ -0,0 +1,100 @@ | ||||
| import 'dart:convert'; | ||||
| import 'dart:io'; | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_svg/flutter_svg.dart'; | ||||
| import 'package:mohem_flutter_app/classes/utils.dart'; | ||||
| import 'package:mohem_flutter_app/extensions/int_extensions.dart'; | ||||
| import 'package:mohem_flutter_app/extensions/string_extensions.dart'; | ||||
| import 'package:mohem_flutter_app/extensions/widget_extensions.dart'; | ||||
| import 'package:mohem_flutter_app/models/itg_forms_models/itg_forms_attachments_model.dart'; | ||||
| import 'package:mohem_flutter_app/widgets/dialogs/confirm_dialog.dart'; | ||||
| import 'package:open_file/open_file.dart'; | ||||
| import 'package:path_provider/path_provider.dart'; | ||||
| import 'package:permission_handler/permission_handler.dart'; | ||||
| 
 | ||||
| class ITGAttachmentsFragment extends StatelessWidget { | ||||
|   List<ITGFormsAttachmentsModel> itgFormAttachmentsList; | ||||
| 
 | ||||
|   ITGAttachmentsFragment(this.itgFormAttachmentsList); | ||||
| 
 | ||||
|   @override | ||||
|   Widget build(BuildContext context) { | ||||
|     return ListView.separated( | ||||
|       itemCount: itgFormAttachmentsList.length, | ||||
|       itemBuilder: (context, index) { | ||||
|         return Row( | ||||
|           children: [ | ||||
|             SvgPicture.asset(determineFileIcon(itgFormAttachmentsList[index].fileType ?? "")), | ||||
|             12.width, | ||||
|             (itgFormAttachmentsList[index].fileName ?? "").toText16().expanded, | ||||
|           ], | ||||
|         ).objectContainerView().onPress(() async { | ||||
|           try { | ||||
|             Permission.storage.isGranted.then( | ||||
|               (isGranted) { | ||||
|                 if (!isGranted) { | ||||
|                   Permission.manageExternalStorage.request().then( | ||||
|                     (granted) async { | ||||
|                       if (granted == PermissionStatus.granted) { | ||||
|                         String path = await _createFileFromString(itgFormAttachmentsList[index].fileBase64 ?? "", itgFormAttachmentsList[index].fileType ?? ""); | ||||
|                         OpenFile.open(path); | ||||
|                       } else { | ||||
|                         showDialog( | ||||
|                           context: context, | ||||
|                           builder: (BuildContext cxt) => ConfirmDialog( | ||||
|                             message: "You need to give storage permission to view files.", | ||||
|                             onTap: () { | ||||
|                               Navigator.pop(context); | ||||
|                             }, | ||||
|                           ), | ||||
|                         ); | ||||
|                       } | ||||
|                     }, | ||||
|                   ); | ||||
|                 } | ||||
|               }, | ||||
|             ); | ||||
|           } catch (ex) { | ||||
|             Utils.showToast("Cannot open file."); | ||||
|           } | ||||
|         }); | ||||
|       }, | ||||
|       separatorBuilder: (BuildContext context, int index) => 12.height, | ||||
|     ).paddingAll(21); | ||||
|   } | ||||
| 
 | ||||
|   String determineFileIcon(String fileContentType) { | ||||
|     String icon = ""; | ||||
|     switch (fileContentType) { | ||||
|       case "pdf": | ||||
|         icon = "assets/images/pdf.svg"; | ||||
|         break; | ||||
|       case "xls": | ||||
|         icon = "assets/images/xls.svg"; | ||||
|         break; | ||||
|       case "xlsx": | ||||
|         icon = "assets/images/xls.svg"; | ||||
|         break; | ||||
|       case "png": | ||||
|         icon = "assets/images/png.svg"; | ||||
|         break; | ||||
|       case "jpg": | ||||
|         icon = "assets/images/jpg.svg"; | ||||
|         break; | ||||
|       case "jpeg": | ||||
|         icon = "assets/images/jpg.svg"; | ||||
|         break; | ||||
|     } | ||||
|     return icon; | ||||
|   } | ||||
| 
 | ||||
|   Future<String> _createFileFromString(String encodedStr, String ext) async { | ||||
|     Uint8List bytes = base64.decode(encodedStr); | ||||
|     String dir = (await getApplicationDocumentsDirectory()).path; | ||||
|     File file = File("$dir/" + DateTime.now().millisecondsSinceEpoch.toString() + "." + ext); | ||||
|     await file.writeAsBytes(bytes); | ||||
|     return file.path; | ||||
|   } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue