import 'dart:convert'; import 'dart:io'; import 'dart:io' as Io; import 'dart:typed_data'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:mohem_flutter_app/api/mowadhafhi/mowadhafhi_api_client.dart'; import 'package:mohem_flutter_app/classes/colors.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/generated/locale_keys.g.dart'; import 'package:mohem_flutter_app/main.dart'; import 'package:mohem_flutter_app/models/mowadhafhi/get_ticket_details.dart'; import 'package:mohem_flutter_app/models/mowadhafhi/get_ticket_transactions.dart'; import 'package:mohem_flutter_app/models/mowadhafhi/get_transaction_attachment_model.dart'; import 'package:mohem_flutter_app/ui/screens/mowadhafhi/view_transaction_attachment.dart'; import 'package:mohem_flutter_app/widgets/app_bar_widget.dart'; import 'package:path_provider/path_provider.dart'; class MowadhafhiRequestDetails extends StatefulWidget { const MowadhafhiRequestDetails({Key? key}) : super(key: key); @override _RequestDetailsState createState() => _RequestDetailsState(); } class _RequestDetailsState extends State { String? itgTicketID; List getTicketsByEmployeeList = []; List getTicketTransactionsList = []; GetTransactionAttachmentModel? getTransactionAttachmentModel; late File imageFile; @override void initState() { // TODO: implement initState super.initState(); } void getRequestID() { if (itgTicketID == null) { itgTicketID = ModalRoute.of(context)?.settings.arguments as String; debugPrint(itgTicketID); getTicketDetails(); } } @override Widget build(BuildContext context) { getRequestID(); return Scaffold( backgroundColor: Colors.white, appBar: AppBarWidget( context, title: LocaleKeys.mowadhafhiRequest.tr(), ), body: SingleChildScrollView( child: getTicketsByEmployeeList.length != 0 ? Column( children: [ Container( width: double.infinity, padding: const EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 12), margin: const EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 12), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(10), boxShadow: [ BoxShadow( color: const Color(0xff000000).withOpacity(.05), blurRadius: 26, offset: const Offset(0, -3), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ getTicketsByEmployeeList![0].ticketStatusName!.toText14(color: MyColors.gradiantEndColor), getTicketsByEmployeeList![0].created!.split(" ")[0].toText12(color: MyColors.grey70Color), ], ), 8.height, LocaleKeys.ticketReference.tr().toText12(color: MyColors.grey98Color), getTicketsByEmployeeList![0].ticketReferenceNo!.toText14(color: MyColors.grey57Color), 8.height, LocaleKeys.section.tr().toText12(color: MyColors.grey98Color), getTicketsByEmployeeList![0].sectionName!.toText14(color: MyColors.grey57Color), 8.height, LocaleKeys.topic.tr().toText12(color: MyColors.grey98Color), getTicketsByEmployeeList![0].topicName!.toText14(color: MyColors.grey57Color), 8.height, LocaleKeys.description.tr().toText12(color: MyColors.grey98Color), getTicketsByEmployeeList![0].description!.toText14(color: MyColors.grey57Color), ], ), ), Container( width: double.infinity, padding: const EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 12), margin: const EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 12), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(10), boxShadow: [ BoxShadow( color: const Color(0xff000000).withOpacity(.05), blurRadius: 26, offset: const Offset(0, -3), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ ListView.builder( shrinkWrap: true, physics: const ScrollPhysics(), itemBuilder: (BuildContext context, int index) { return Stack( children: [ Padding( padding: const EdgeInsets.only(left: 50.0), child: Card( elevation: 0.0, margin: const EdgeInsets.fromLTRB(0.0, 20.0, 20.0, 10.0), child: Container( padding: const EdgeInsets.all(12.0), width: double.infinity, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ LocaleKeys.actionBy.tr().toText14(color: MyColors.grey57Color), ": ".toText14(), getTicketTransactionsList![index].actionBy!.toText14(color: MyColors.grey57Color), ], ), Row( children: [ LocaleKeys.actions.tr().toText14(color: MyColors.grey57Color), ": ".toText14(), getTicketTransactionsList![index].statusDisplayText!.toText14(color: MyColors.grey57Color), ], ), getTicketTransactionsList![index].comments!.toText14(color: MyColors.grey98Color), 12.height, if (getTicketTransactionsList[index].attachments != null) InkWell( onTap: () { print(getTicketTransactionsList[index].attachments![0].attachmentId); getTransactionAttachment(getTicketTransactionsList[index].attachments![0].attachmentId!); }, child: LocaleKeys.attachments.tr().toText14(color: MyColors.gradiantEndColor, isUnderLine: true), ), Row( mainAxisAlignment: MainAxisAlignment.end, children: [ getTicketTransactionsList![index].actionDate!.split(" ")[0].toText12(color: MyColors.grey70Color), ], ), ], ), ), ), ), Positioned( top: 0.0, bottom: 0.0, left: 28.0, child: Container( height: double.infinity, width: 3.0, color: MyColors.lightTextColor.withOpacity(0.5), ), ), Positioned( top: 0.0, left: 15.0, child: Container( height: 30.0, width: 30.0, decoration: const BoxDecoration( shape: BoxShape.circle, color: Colors.white, ), child: Container( margin: const EdgeInsets.all(5.0), height: 30.0, width: 30.0, decoration: const BoxDecoration(shape: BoxShape.circle, color: MyColors.gradiantEndColor), ), ), ) ], ); }, itemCount: getTicketTransactionsList.length ?? 0, ) ], ), ), ], ) : Container(), ), ); } void getTicketDetails() async { try { Utils.showLoading(context); getTicketsByEmployeeList = await MowadhafhiApiClient().getTicketDetailsByEmployee(itgTicketID); Utils.hideLoading(context); setState(() {}); getTicketTransactions(); } catch (ex) { Utils.hideLoading(context); Utils.handleException(ex, context, null); } } void getTicketTransactions() async { try { Utils.showLoading(context); getTicketTransactionsList = await MowadhafhiApiClient().getTicketTransactions(itgTicketID); debugPrint(getTicketTransactionsList![0].actionDate); Utils.hideLoading(context); setState(() {}); } catch (ex) { Utils.hideLoading(context); Utils.handleException(ex, context, null); } } void getTransactionAttachment(int attachmentID) async { try { Utils.showLoading(context); getTransactionAttachmentModel = await MowadhafhiApiClient().getTransactionAttachments(attachmentID); debugPrint(getTransactionAttachmentModel?.fileName); Utils.hideLoading(context); setState(() {}); handleTransactionAttachment(); } catch (ex) { Utils.hideLoading(context); Utils.handleException(ex, context, null); } } void handleTransactionAttachment() async { String ext = ''; String? rFile = getTransactionAttachmentModel!.base64String; String? rFileExt = getTransactionAttachmentModel!.fileName; ext = "." + rFileExt!.split(".").last.toLowerCase(); if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".tiff") { try { Uint8List decodedBytes = base64Decode(rFile!.split("base64,").last); Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); // 1 imageFile = Io.File("${appDocumentsDirectory.path}/addImage$ext"); imageFile.writeAsBytesSync(decodedBytes); Navigator.push( context, MaterialPageRoute( builder: (BuildContext context) => ViewTransactionAttachment( imageFile: imageFile, ), ), ); } catch (e) { logger.d(e); } } else { Utils.showErrorDialog(context: context, onOkTapped: () {}, message: "Unable to view attachment"); } } }