我创建了一个电子商务应用程序。
在我的应用中有超过 5000 种产品,在我的购物车中有超过 300 多种产品。
在我的购物车中有一个下载按钮,可以将数据(图像、id、名称、数量)下载到 pdf 中。
我已经使用https://pub.dev/packages/syncfusion_flutter_pdf这个插件来创建 pdf。
问题是当购物车中有 10 到 20 个产品时,它会很快下载,但当有超过 300 个产品时,它的下载速度会非常慢。
我在代码中添加的逻辑是首先它将从存储我的数据的firebase cloud fire store中获取所有数据,然后在单击下载按钮时将其存储在列表中我正在使用for在我的pdf中添加该列表环形。
我希望当单击下载按钮时它应该在后台运行该循环并创建 pdf,并且在创建它时我想显示一个进度通知,其中谷歌驱动器显示它的进度通知。单击该通知完成后,它应该打开下载的pdf。
我在谷歌上搜索但没有得到任何解决方案。
如果我能解决这个问题,那将有很大帮助。
请帮助伙计们!提前致谢!!。
这是单击下载按钮时的pdf代码。
Future<void> generateInvoice() async {
//Create a PDF document.
final PdfDocument document = PdfDocument();
//Add page to the PDF
final PdfPage page = document.pages.add();
//Get page client size
final Size pageSize = page.getClientSize();
//Create a PdfGrid
Future<List<int>> _readImageData(String name) async {
final ByteData data = await rootBundle.load('assets/$name');
return data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
}
page.graphics.drawImage(
PdfBitmap(await _readImageData('images/unnamed.png')),
Rect.fromLTWH(0, 0, pageSize.width, 100)
);
//Generate PDF grid.
PdfGrid grid = PdfGrid();
//Secify the columns count to the grid.
grid.columns.add(count: 4);
//Create the header row of the grid.
final PdfGridRow headerRow = grid.headers.add(1)[0];
//Set style
headerRow.style.backgroundBrush = PdfSolidBrush(PdfColor(22, 111, 105));
headerRow.style.textBrush = PdfSolidBrush(PdfColor(187, 167, 85));
headerRow.cells[0].value = 'SR NO';
headerRow.cells[0].stringFormat.alignment = PdfTextAlignment.center;
headerRow.cells[1].value = 'PRODUCT IMAGE';
headerRow.cells[1].stringFormat.alignment = PdfTextAlignment.center;
headerRow.cells[2].value = 'PRODUCT DETAILS';
headerRow.cells[2].stringFormat.alignment = PdfTextAlignment.center;
headerRow.cells[3].value = 'QUANTITY';
headerRow.cells[3].stringFormat.alignment = PdfTextAlignment.center;
//Add rows
int sr_no = 1;
for (int i = 0; i < _totalItems; i++) {
final PdfGridRow row = grid.rows.add();
var imageResponse1 = await get(Uri.parse(photoArray[i]));
sr.add(sr_no);
print(sr[i]);
sr_no = sr_no + 1;
print(sr_no);
row.cells[0].value = sr[i].toString();
row.cells[0].stringFormat.alignment = PdfTextAlignment.center;
row.cells[0].stringFormat.lineAlignment = PdfVerticalAlignment.middle;
row.cells[1].value = PdfBitmap(imageResponse1.bodyBytes.toList());
//row.cells[1].stringFormat.alignment = PdfTextAlignment.center;
row.cells[1].stringFormat.lineAlignment = PdfVerticalAlignment.middle;
row.cells[2].value =
' DESIGN NO - ${skuArray[i]}\n G. WT - ${metalArray[i]}\n D. WT - ${stoneArray[i]}\n COLOUR - ${goldColorArray[i]}';
row.cells[2].stringFormat.alignment = PdfTextAlignment.left;
row.cells[2].stringFormat.lineAlignment = PdfVerticalAlignment.middle;
row.cells[3].value = dropdown[i].toString();
row.cells[3].stringFormat.alignment = PdfTextAlignment.center;
row.cells[3].stringFormat.lineAlignment = PdfVerticalAlignment.middle;
grid.rows[i].height = 160;
}
grid.columns[0].width = 70;
grid.columns[3].width = 70;
for (int i = 0; i < headerRow.cells.count; i++) {
headerRow.cells[i].style.cellPadding =
PdfPaddings(bottom: 5, left: 5, right: 5, top: 5);
}
final PdfStringFormat format = PdfStringFormat(
alignment: PdfTextAlignment.center,
lineAlignment: PdfVerticalAlignment.middle);
//Add a new row
final PdfGridRow totalRow = grid.rows.add();
totalRow.cells[0].value = 'TOTAL QUANTITY';
//Set column span
totalRow.cells[0].columnSpan = 3;
totalRow.cells[0].style.stringFormat = format;
totalRow.height = 25;
int sum = dropdown.fold(0, (p, c) => p + c);
totalRow.cells[3].value = sum.toString();
totalRow.cells[3].stringFormat.alignment = PdfTextAlignment.center;
totalRow.cells[3].stringFormat.lineAlignment = PdfVerticalAlignment.middle;
grid.draw(
page: page, bounds: Rect.fromLTWH(0, 120, 0, 0));
//Save the PDF document
final List<int> bytes = document.save();
//Dispose the document.
document.dispose();
//Get external storage directory
Directory directory = (await getApplicationDocumentsDirectory());
//Get directory path
String path = directory.path;
print(path);
//Create an empty file to write PDF data
File file = File('$path/CART-CATALOGUE.pdf');
//Write PDF data
await file.writeAsBytes(bytes, flush: true);
setState(() {
isApiCallProcess = false;
});
//Open the PDF document in mobile
OpenFile.open('$path/CART-CATALOGUE.pdf');
}