You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/ui/screens/mowadhafhi/request_details.dart

301 lines
13 KiB
Dart

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: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 MowadhafhiRequestDetails extends StatefulWidget {
const MowadhafhiRequestDetails({Key? key}) : super(key: key);
@override
_RequestDetailsState createState() => _RequestDetailsState();
}
class _RequestDetailsState extends State<MowadhafhiRequestDetails> {
String? itgTicketID;
List<GetTicketDetailsByEmployee> getTicketsByEmployeeList = [];
List<GetTicketTransactions> 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: <Widget>[
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);
Utils.hideLoading(context);
handleTransactionAttachment();
} catch (ex) {
Utils.hideLoading(context);
Utils.handleException(ex, context, null);
}
}
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;
}
void handleTransactionAttachment() async {
Permission.storage.isGranted.then((isGranted) {
if (!isGranted) {
Permission.manageExternalStorage.request().then((granted) async {
if (granted == PermissionStatus.granted) {
String ext = '';
String? rFile = getTransactionAttachmentModel!.base64String;
String? rFileExt = getTransactionAttachmentModel!.fileName;
ext = rFileExt!.split(".").last.toLowerCase();
try {
String path = await _createFileFromString(rFile!.split("base64,").last ?? "", ext ?? "");
await OpenFile.open(path).catchError((err) {
print(err);
});
} catch (ex) {
Utils.showToast("Cannot open file.");
}
} else {
showDialog(
context: context,
builder: (BuildContext cxt) => ConfirmDialog(
message: "You need to give storage permission to view files.",
onTap: () {
Navigator.pop(context);
},
),
);
}
});
}
});
}
}