Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
10 kB
1
Indexable
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:printing/printing.dart';
import 'package:tailorx/src/view/pdf/pdf_export.dart';
import 'package:pdf/widgets.dart' as p;
import '../../utils/utils.dart';
import '../invoice/get_measurement_details_model.dart';

import 'package:flutter/services.dart' show Uint8List, rootBundle;

class PdfPreviewPage extends StatefulWidget {
  const PdfPreviewPage({super.key});

  @override
  State<PdfPreviewPage> createState() => _PdfPreviewPageState();
}

class _PdfPreviewPageState extends State<PdfPreviewPage> {
  GetMeasurementDetailsResult measurementDetailsResult = Get.arguments;
  List<p.Widget> widgets = [];

  @override
  void initState() {
    addWidgets();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('PDF Preview'),
      ),
      body: PdfPreview(
        build: (context) => makePdf(measurementDetailsResult, widgets),
      ),
    );
  }

  void addWidgets() async {
    widgets.add(p.SizedBox(height: 20)); //some space beneath image
    final imageLogo = p.MemoryImage(
        (await rootBundle.load('assets/app_logo.jpeg')).buffer.asUint8List());
    widgets.add(p.Row(
      mainAxisAlignment: p.MainAxisAlignment.spaceBetween,
      children: [
        p.Column(
          children: [
            p.Text("Invoice No: ${measurementDetailsResult.invNo}"),
            p.Text(
                "Invoice Date: ${Utils.convertToReadableDate(measurementDetailsResult.bDate!)}"),
            p.Text(
                "Delivery Date: ${Utils.convertToReadableDate(measurementDetailsResult.dDate!)}"),
            p.Text("Customer Id: ${measurementDetailsResult.customerID}"),
            p.Text("Customer Name: ${measurementDetailsResult.customerName}"),
            p.Text("Mobile No: ${measurementDetailsResult.mobile}"),
          ],
          crossAxisAlignment: p.CrossAxisAlignment.start,
        ),
        p.SizedBox(height: 150, width: 150, child: p.Image(imageLogo))
      ],
    ));

    widgets.add(p.SizedBox(height: 10));

    String data = measurementDetailsResult.qRCode!;

    Uint8List bytes;
    bytes = const Base64Decoder().convert(data);
    final imageQR = p.MemoryImage((bytes));

    widgets.add(
      p.Column(
        children: [
          p.Container(height: 50),
          p.Padding(
            child: p.Text(
              'INVOICE DETAILS',
              textAlign: p.TextAlign.center,
            ),
            padding: const p.EdgeInsets.all(20),
          ),
          p.Divider(
            height: 1,
            borderStyle: p.BorderStyle.dashed,
          ),
          p.Table(
            children: [
              p.TableRow(
                children: [
                  p.Padding(
                    padding: const p.EdgeInsets.all(10),
                    child: p.Text(
                      'Order Id',
                      textAlign: p.TextAlign.left,
                    ),
                  ),
                  p.Padding(
                    padding: const p.EdgeInsets.all(10),
                    child: p.Text(
                      'Product',
                      textAlign: p.TextAlign.left,
                    ),
                  ),
                  p.Padding(
                    padding: const p.EdgeInsets.all(10),
                    child: p.Text(
                      'Qty',
                      textAlign: p.TextAlign.left,
                    ),
                  ),
                  p.Padding(
                    padding: const p.EdgeInsets.all(10),
                    child: p.Text(
                      'Price',
                      textAlign: p.TextAlign.left,
                    ),
                  ),
                  p.Padding(
                    padding: const p.EdgeInsets.all(10),
                    child: p.Text(
                      'Amount',
                      textAlign: p.TextAlign.left,
                    ),
                  ),
                ],
              ),
              ...measurementDetailsResult.items!.map(
                (e) => p.TableRow(
                  children: [
                    p.Padding(
                      padding: const p.EdgeInsets.all(10),
                      child: p.Text(
                        e.orderNo.toString(),
                        textAlign: p.TextAlign.left,
                      ),
                    ),
                    p.Padding(
                      padding: const p.EdgeInsets.all(10),
                      child: p.Text(
                        e.clothName!,
                        textAlign: p.TextAlign.left,
                      ),
                    ),
                    p.Padding(
                      padding: const p.EdgeInsets.all(10),
                      child: p.Text(
                        e.quantity.toString(),
                        textAlign: p.TextAlign.left,
                      ),
                    ),
                    p.Padding(
                      padding: const p.EdgeInsets.all(10),
                      child: p.Text(
                        e.amount.toString(),
                        textAlign: p.TextAlign.left,
                      ),
                    ),
                    p.Padding(
                      padding: const p.EdgeInsets.all(10),
                      child: p.Text(
                        e.amountWithVAT.toString(),
                        textAlign: p.TextAlign.left,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );

    widgets.add(
      p.Column(
        children: [
          p.Divider(
            height: 1,
            borderStyle: p.BorderStyle.dashed,
          ),
          p.Table(children: [
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    'Sub Total',
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.amount.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    "Vat Total",
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.vATValue.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    'Total with Vat',
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.amountWithVAT.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    'Discount',
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.discount.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    'Net Amount',
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.netAmount.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    'Paid Amount',
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.paid.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
            p.TableRow(
              children: [
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    'Balance',
                  ),
                ),
                p.Padding(
                  padding: const p.EdgeInsets.all(10),
                  child: p.Text(
                    measurementDetailsResult.balance.toString(),
                    textAlign: p.TextAlign.right,
                  ),
                ),
              ],
            ),
          ]),
        ],
      ),
    );

    widgets.add(p.Column(children: [
      p.Divider(
        height: 1,
        borderStyle: p.BorderStyle.dashed,
      ),
      p.Container(height: 50),
      p.SizedBox(
        height: 150,
        width: 150,
        child: p.Image(
          imageQR,
        ),
      ),
    ]));
  }
}