Untitled
unknown
dart
3 years ago
44 kB
8
Indexable
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'dart:io';
import 'package:pdf/widgets.dart' as pw;
import 'package:permission_handler/permission_handler.dart';
import 'package:printing/printing.dart';
import 'package:open_filex/open_filex.dart';
import 'package:property_wallet_app/Network/Api.dart';
import 'package:property_wallet_app/components/app_colors.dart';
import 'package:property_wallet_app/components/text_extensions.dart';
import 'controller/inventryPdfController.dart';
class InventoryPdf extends StatefulWidget {
const InventoryPdf({Key? key}) : super(key: key);
@override
State<InventoryPdf> createState() => _InventoryPdfState();
}
class _InventoryPdfState extends State<InventoryPdf> {
bool? permissionGranted;
Future _getStoragePermission() async {
if (await Permission.storage.request().isGranted) {
setState(() {
permissionGranted = true;
});
} else {}
}
@override
void initState() {
_getStoragePermission();
super.initState();
}
Widget build(BuildContext context) {
double w = MediaQuery.of(context).size.width;
double h = MediaQuery.of(context).size.height;
return Scaffold(
body: GetBuilder<InventoryPdfController>(
init: InventoryPdfController(),
builder: (invPdfCtrl) => invPdfCtrl.isLoader.isTrue
? Center(
child: CircularProgressIndicator(),
)
: GestureDetector(
onTap: () {
genaratePdf(
context: context,
mainImgList: invPdfCtrl.mainImages,
agencyUrl: invPdfCtrl
.inventoryDetailModel!.data!.profile!.agency!.logoUrl,
propDetailItems: invPdfCtrl.propertyDetailItem,
propName:
invPdfCtrl.inventoryDetailModel!.data!.projectName,
agencyName: invPdfCtrl.inventoryDetailModel!.data!.profile!
.agency!.agencyName,
address: invPdfCtrl.inventoryDetailModel!.data!.address,
contact: invPdfCtrl.contact,
price: invPdfCtrl
.inventoryDetailModel!.data!.inventory![0].price,
propDesc:
invPdfCtrl.inventoryDetailModel!.data!.description,
featureList: invPdfCtrl.featuresList,
screenwidth: MediaQuery.of(context).size.width);
},
child: Container(
child: Center(
child: Container(
color: Colors.pink,
height: 50,
width: 200,
child: Text(
"${invPdfCtrl.inventoryDetailModel!.data!.projectName ?? ""}"),
)),
color: Colors.white,
),
),
));
}
}
var primaryColor = "#27a3a3";
var secondaryColor = "#777777";
Future genaratePdf(
{context,
projectTypeIcon,
projectSubTypeIcon,
mainImgList,
agencyUrl,
propDetailItems,
propName,
propDesc,
featureList,
price,
address,
agencyName,
agentName,
contact,
required double screenwidth}) async {
double w = MediaQuery.of(context).size.width;
double h = MediaQuery.of(context).size.height;
try {
showDialog(
context: context,
builder: (context) => Center(
child: const CircularProgressIndicator(
color: DynamicColor.primaryColor,
)));
final pdf = pw.Document();
await loadImages(
mainImgList, agencyUrl, projectTypeIcon, projectSubTypeIcon);
var font = await PdfGoogleFonts.poppinsRegular();
var fontBold = await PdfGoogleFonts.poppinsMedium();
final List<pw.Widget> propDetail = List.generate(
propDetailItems.length,
// 5,
(index) => pw.Padding(
padding: pw.EdgeInsets.symmetric(horizontal: 4),
child: pw.Container(
width: 92,
height: 52,
child: pw.Center(
child: pw.Column(
mainAxisAlignment: pw.MainAxisAlignment.center,
crossAxisAlignment: pw.CrossAxisAlignment.center,
children: [
pw.Image(
propIcons[index],
width: 20,
height: 20,
),
pw.FittedBox(
fit: pw.BoxFit.scaleDown,
child: pw.Text("${propDetailItems[index]}",
style: pw.TextStyle(
font: fontBold,
color: PdfColor.fromHex("#ffffff"),
fontSize: 10,
)),
)
]),
),
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius: pw.BorderRadius.circular(8),
))));
final List<pw.Widget> featureItems = List.generate(
featureList.length > 22 ? 22 : featureList.length,
(index) => pw.Container(
height: 10,
width: 100,
// color: PdfColor.fromHex(secondaryColor),
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.start,
crossAxisAlignment: pw.CrossAxisAlignment.start,
children: [
pw.Image(
commonIcons[0],
width: 10,
height: 10,
),
pw.SizedBox(width: 10),
pw.Container(
width: 60,
child: pw.Align(
alignment: pw.Alignment.centerLeft,
child: pw.FittedBox(
fit: pw.BoxFit.scaleDown,
child: pw.Text(" ${featureList[index]}",
style: pw.TextStyle(
font: font,
color: PdfColor.fromHex(secondaryColor),
fontSize: 14)),
)))
])),
);
pdf.addPage(
pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) => pw.FullPage(
ignoreMargins: true,
child: pw.Center(
child: pw.Container(
width: 560,
height: 810,
// color:PdfColor.fromHex("#008c85"),
child: pw.Stack(children: [
pw.Column(children: [
pw.Column(
crossAxisAlignment: pw.CrossAxisAlignment.end,
children: [
pw.Container(
height: 300,
child: pw.Stack(
alignment: pw.Alignment.centerRight,
children: [
pw.Row(children: [
pw.Container(
width: topMainimages.length >= 3
? 275
: 560,
decoration: pw.BoxDecoration(
image: pw.DecorationImage(
image: topMainimages[0],
fit: pw.BoxFit.fill,
),
borderRadius:
pw.BorderRadius.circular(10),
// boxShadow: [
// // pw.BoxShadow(
// // color: PdfColor.fromHex("#0d09e3"),
// // blurRadius: 4,
// // offset:
// // PdfPoint(6.0, 4.0), // Shadow position
// // ),
// ],
)),
]),
pw.Column(children: [
topMainimages.length >= 3
? pw.Container(
// margin: pw.EdgeInsets.only(bottom: 5),
height: 147,
width: 280,
decoration: pw.BoxDecoration(
image: pw.DecorationImage(
image: topMainimages[1],
fit: pw.BoxFit.fill,
),
borderRadius:
pw.BorderRadius.circular(10),
// boxShadow: [
// pw.BoxShadow(
// color: PdfColor.fromHex("#0d09e3"),
// blurRadius: 4,
// offset: PdfPoint(
// 6.0, 4.0), // Shadow position
// ),
// ],
))
: pw.SizedBox.shrink(),
topMainimages.length >= 3
? pw.SizedBox(
height: 6,
)
: pw.SizedBox.shrink(),
topMainimages.length >= 3
? pw.Container(
// margin: pw.EdgeInsets.only(top: 5),
// foregroundDecoration: BoxDecoration(
// image: DecorationImage(image: topMainimages[2])
// ),
height: 147,
width: 280,
decoration: pw.BoxDecoration(
// color: PdfColor.fromHex("880808"),
image: pw.DecorationImage(
image: topMainimages[2],
fit: pw.BoxFit.cover,
),
borderRadius:
pw.BorderRadius.circular(10),
// boxShadow: [
// pw.BoxShadow(
// color: PdfColor.fromHex("#0d09e3"),
// blurRadius: 4,
// offset: PdfPoint(
// 6.0, 4.0), // Shadow position
// ),
// ],
))
: pw.SizedBox.shrink(),
]),
])),
]),
///----Content----///
pw.Container(
width: 560,
// color: PdfColor.fromHex("#066b21"),
child: pw.Center(
child: pw.Column(
mainAxisAlignment: pw.MainAxisAlignment.center,
children: [
// pw.SizedBox(height: 30),
pw.Padding(
padding: pw.EdgeInsets.only(top: 15),
child: pw.Container(
// width: 180,
child: pw.Row(
mainAxisAlignment:
pw.MainAxisAlignment.center,
children: [
pw.Row(children: [
pw.Image(
commonIcons[2],
width: 40,
height: 40,
),
pw.Container(
// width: 187,
child: pw.Text(" ${agencyName}",
style: pw.TextStyle(
fontSize: 21,
font: fontBold,
color: PdfColor.fromHex(
secondaryColor))),
),
])
]),
),
),
pw.SizedBox(height: 5),
pw.Row(
mainAxisAlignment:
pw.MainAxisAlignment.center,
children: [
pw.Container(
width: 6,
height: 6,
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius:
pw.BorderRadius.circular(3),
)),
pw.SizedBox(width: 5),
pw.Container(
color: PdfColor.fromHex(primaryColor),
height: 2,
width: 30),
pw.SizedBox(width: 5),
pw.SizedBox(
// width: 100,
child: pw.Text("$propName",
// overflow: pw.TextOverflow.clip,
style: pw.TextStyle(
color: PdfColor.fromHex(
primaryColor),
fontSize: 22,
font: fontBold)),
),
pw.SizedBox(width: 5),
pw.Container(
color: PdfColor.fromHex(primaryColor),
height: 2,
width: 30),
pw.SizedBox(width: 5),
pw.Container(
width: 6,
height: 6,
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius:
pw.BorderRadius.circular(3),
)),
]),
pw.SizedBox(
height: 10,
),
pw.SizedBox(
height: 50,
width: 450,
child: pw.Text("$propDesc",
textAlign: pw.TextAlign.center,
style: pw.TextStyle(
fontSize: 14.43,
font: fontBold,
color: PdfColor.fromHex(secondaryColor),
))),
pw.SizedBox(
height: 20,
),
///-----2nd row----///
pw.Row(
mainAxisAlignment:
pw.MainAxisAlignment.center,
children: [
pw.Container(
width: 6,
height: 6,
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius:
pw.BorderRadius.circular(3),
)),
pw.SizedBox(width: 5),
pw.Container(
color: PdfColor.fromHex(primaryColor),
height: 2,
width: 30),
pw.SizedBox(width: 5),
pw.Text("Property Detail",
style: pw.TextStyle(
color:
PdfColor.fromHex(primaryColor),
fontSize: 22,
font: fontBold)),
pw.SizedBox(width: 5),
pw.Container(
color: PdfColor.fromHex(primaryColor),
height: 2,
width: 30),
pw.SizedBox(width: 5),
pw.Container(
width: 6,
height: 6,
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius:
pw.BorderRadius.circular(3),
)),
]),
pw.SizedBox(height: 10),
pw.Wrap(
// runSpacing: ,
// spacing: 2,
children: propDetail),
// pw.Container(
// width: 500,
// child:
// pw.Center(
// child: pw.GridView(
// crossAxisCount: 5,
// childAspectRatio: 1 / 2,
// children: propDetail),
// )
//
// ),
///---3rd row Features--///
pw.SizedBox(height: 10),
pw.Row(
mainAxisAlignment:
pw.MainAxisAlignment.center,
children: [
pw.Container(
width: 6,
height: 6,
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius:
pw.BorderRadius.circular(3),
)),
pw.SizedBox(width: 5),
pw.Container(
color: PdfColor.fromHex(primaryColor),
height: 2,
width: 30),
pw.SizedBox(width: 5),
pw.Text("Features",
style: pw.TextStyle(
color:
PdfColor.fromHex(primaryColor),
fontSize: 22,
font: fontBold)),
pw.SizedBox(width: 5),
pw.Container(
color: PdfColor.fromHex(primaryColor),
height: 2,
width: 30),
pw.SizedBox(width: 5),
pw.Container(
width: 6,
height: 6,
decoration: pw.BoxDecoration(
color: PdfColor.fromHex(primaryColor),
borderRadius:
pw.BorderRadius.circular(3),
)),
]),
pw.SizedBox(height: 4),
pw.Column(
mainAxisAlignment:
pw.MainAxisAlignment.spaceBetween,
children: [
// pw.Expanded(
// flex: 2,
// child: pw.Row(
// children: featureItems
// ),
// ),
pw.Container(
width: 520,
// color: PdfColor.fromHex(primaryColor),
child: pw.Wrap(
direction: pw.Axis.horizontal,
runSpacing: 5,
spacing: 2,
alignment: pw.WrapAlignment.center,
children: featureItems),
// pw.Center(
// child: pw.GridView(
// padding: pw.EdgeInsets.zero,
// crossAxisCount: 4,
// childAspectRatio: 1 / 8,
// children: featureItems)
// )
),
pw.SizedBox(height: 20),
pw.Column(children: [
pw.Container(
width: 150,
child: pw.FittedBox(
fit: pw.BoxFit.scaleDown,
child: pw.Row(
mainAxisAlignment:
pw.MainAxisAlignment.center,
children: [
pw.Text("Price: ",
style: pw.TextStyle(
color:
PdfColor.fromHex(
primaryColor),
font: fontBold,
fontSize: 22)),
pw.Text(
"${price ?? "0,00,000,000"}"
.replaceAllMapped(
new RegExp(
r'(\d{1,3})(?=(\d{3})+(?!\d))'),
(Match m) =>
"${m[1]},"),
style: pw.TextStyle(
color: PdfColor.fromHex(
secondaryColor),
font: fontBold,
fontSize: 22))
]),
)),
pw.SizedBox(
height: 20,
),
])
]),
pw.SizedBox(height: 4),
]),
)),
]),
///----Footer----///
pw.Positioned(
bottom: 1,
child: pw.Container(
width: 565,
child: pw.Row(
crossAxisAlignment: pw.CrossAxisAlignment.end,
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Container(
width: 187,
child: pw.FittedBox(
fit: pw.BoxFit.scaleDown,
child: pw.Column(
crossAxisAlignment:
pw.CrossAxisAlignment.center,
children: [
pw.Container(
width: 187,
child: pw.Row(children: [
pw.SizedBox(width: 5),
pw.Image(
commonIcons[1],
width: 20,
height: 20,
),
pw.Container(
width: 187,
child: pw.Text(" $address",
style: pw.TextStyle(
fontSize: 9,
font: fontBold,
color: PdfColor.fromHex(
secondaryColor))),
),
]),
),
pw.SizedBox(height: 5),
pw.Container(
width: 187,
height: 30,
color: PdfColor.fromHex(primaryColor))
]),
),
),
pw.Padding(
padding: pw.EdgeInsets.only(top: 10),
child: pw.Image(
logosImg[0],
width: 100,
height: 500,
),
),
pw.SizedBox(
width: w / 2.5,
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.start,
children: [
pw.Container(
// width: 187,
child: pw.FittedBox(
fit: pw.BoxFit.scaleDown,
child: pw.Column(
crossAxisAlignment:
pw.CrossAxisAlignment.start,
// mainAxisAlignment: pw.MainAxisAlignment.start,
children: [
agentName != null
? pw.Padding(
padding: pw.EdgeInsets.only(
top: 15),
child: pw.Container(
// width: 180,
child: pw.Row(
mainAxisAlignment: pw
.MainAxisAlignment
.start,
crossAxisAlignment: pw
.CrossAxisAlignment
.start,
children: [
pw.Row(children: [
pw.Image(
commonIcons[2],
width: 20,
height: 40,
),
pw.Container(
width: 190,
child: pw.Text(
" ${agentName}"
.shortSentence(
wordCounts:
13),
maxLines: 3,
style: pw.TextStyle(
fontSize:
13,
font:
fontBold,
color: PdfColor
.fromHex(
secondaryColor))),
),
])
]),
),
)
: pw.SizedBox.shrink(),
pw.Container(
// width: 187,
child: pw.Row(
mainAxisAlignment:
pw.MainAxisAlignment.start,
children: [
pw.Image(
commonIcons[3],
width: 20,
height: 20,
),
pw.Container(
width: 187,
child: pw.Text(" $contact",
style: pw.TextStyle(
fontSize: 16,
font: fontBold,
color: PdfColor.fromHex(
secondaryColor))),
),
]),
),
pw.SizedBox(height: 5),
pw.Container(
width: 187,
height: 30,
color: PdfColor.fromHex(
primaryColor))
]),
)),
]),
),
]),
),
),
///---------Logos----///
pw.Positioned(
top: 250,
child: pw.Container(
width: 560,
// color: PdfColor.fromHex("#3d0000"),
child: pw.Padding(
padding: pw.EdgeInsets.symmetric(horizontal: 28),
child: pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.start,
children: [
// pw.Container(
// height: 100,
// width: 100,
// child: pw.Image(
// logosImg[0],
// ),
// decoration: pw.BoxDecoration(
// color: PdfColor.fromHex("#fcfafa"),
// borderRadius: pw.BorderRadius.circular(50),
// )),
pw.Stack(
alignment: pw.Alignment.center,
children: [
pw.Container(
height: 100,
width: 100,
decoration: pw.BoxDecoration(
image: pw.DecorationImage(
image: logosImg[1]),
color: PdfColor.fromHex("#ffffff"),
borderRadius:
pw.BorderRadius.circular(50),
)),
// pw.Image(logosImg[1],
// height: 80, width: 80)
])
// pw.Container(
// height: 100,
// width: 100,
//
//
// child: pw.SizedBox(
// height: 100,
// width: 100,
// child: pw.Image(
// logosImg[1],height: 50,
// width: 50,
// ),
//
// ),
//
//
//
// decoration: pw.BoxDecoration(
// color: PdfColor.fromHex("#fcfafa"),
// borderRadius: pw.BorderRadius.circular(50),
// )),
])),
)),
]),
),
),
),
),
);
// var status = await Permission.storage.status;
// if (!status.isGranted) {
// await Permission.storage.request();
var path;
var fileName;
if (Platform.isAndroid) {
path = await getExternalStorageDirectory();
fileName = path.path + "/Inventory.pdf";
} else {
path = await getApplicationDocumentsDirectory();
fileName = path.path + "/Inventory.pdf";
}
final file = File(fileName);
await file.writeAsBytes(await pdf.save());
Navigator.of(context).pop();
if (Api().sp.read('token') == null) {
num freeToolUsed = await Api().sp.read('freeToolUsed') == null
? 0
: num.parse(Api().sp.read('freeToolUsed').toString());
Api().sp.write('freeToolUsed', freeToolUsed + 1);
print(Api().sp.read('freeToolUsed'));
}
await OpenFilex.open(file.path);
} catch (e) {}
}
List topMainimages = [];
List propIcons = [];
List commonIcons = [];
List logosImg = [];
Future loadImages(
mainImagesApi, agencyImage, projectTypeIcon, projectSubTypeIcon) async {
topMainimages.clear();
propIcons.clear();
commonIcons.clear();
logosImg.clear();
try {
///-----Main Top Images----///
for (int i = 0; mainImagesApi.length > i; i++) {
final mainImage =
await flutterImageProvider(NetworkImage("${mainImagesApi[i]}"));
topMainimages.add(mainImage);
}
///-----Logos----///
final logoApp = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/imagelogo%20%281%29-1673529321463.png"));
logosImg.add(logoApp);
final logoCompany = await flutterImageProvider(NetworkImage(agencyImage !=
null
? agencyImage
: "https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/imageplaceholder-1674740376589.png"));
logosImg.add(logoCompany);
///----Property Icons----///
final iconArea = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-08-1673005698040.png"));
propIcons.add(iconArea);
final iconBed = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-04-1673005698106.png"));
propIcons.add(iconBed);
final iconWash = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-03-1673005698151.png"));
propIcons.add(iconWash);
final iconHome = await flutterImageProvider(NetworkImage(projectTypeIcon ==
null
? "https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-03-1673005698151.png"
: projectTypeIcon));
propIcons.add(iconHome);
final iconKitchen = await flutterImageProvider(NetworkImage(
projectSubTypeIcon == null
? "https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-03-1673005698151.png"
: projectSubTypeIcon));
propIcons.add(iconKitchen);
///----Common Icons----///
final checkMark = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/imagearrow-1673528888011.png"));
commonIcons.add(checkMark);
final mapIcon = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-02-1673005962794.png"));
commonIcons.add(mapIcon);
final profileIcon = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-05-1673005962853.png"));
commonIcons.add(profileIcon);
final contactIcon = await flutterImageProvider(NetworkImage(
"https://property-wallet-website.s3.ap-south-1.amazonaws.com/backend-data/image1-01-1673185536639.png"));
commonIcons.add(contactIcon);
} catch (e) {
return;
}
}
Editor is loading...