Flutter Horizontal & Vertical Listview To Take Screenshoot

46 Views Asked by At

I am trying to achieve a very common behavior nowadays which is vertical & Horizontal screenshoot for my reporting in pdf. Think of something like the report screen Of the app:-

Want to get vertical & Horizontal screenshoot for my reporting in pdf. i have atleast 10 columns and multiple rows how can i take screenshoot for it..? Help Me With It.

My Code is Like :-

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sales - ' + reptwise),
        actions: <Widget>[
          PopupMenuButton<String>(
            onSelected: handleClick,
            itemBuilder: (BuildContext context) {
              return {'Pdf', 'ScreenShoot'}.map((String choice) {
                return PopupMenuItem<String>(
                  value: choice,
                  child: Text(choice),
                );
              }).toList();
            },
          ),
        ],
      ),
      body: RepaintBoundary(
        key: _screenshotKey,
        child: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          //Use SingleChildScrollView instead of ListView for horizontal scrolling
          scrollDirection: Axis.horizontal,
          child: SizedBox(
            width: 810.0, // Set a fixed width or use a sized container
            child: ListView.builder(
              shrinkWrap: true,
              //scrollDirection: Axis.horizontal,
              //physics: BouncingScrollPhysics(),
              itemCount: reportData.length,
              itemBuilder: (context, reportIndex) {
                final party = reportData.keys.elementAt(reportIndex);
                final reportDetails = reportData[party];
                final totReportDetails = reportData[party] as List<SalesRecord>;
                Map<String, List<DataRow>> rowsByBillBll = {};
                double totalPcs = calculateTotalPcsForSales(totReportDetails);
                double totalQty = calculateTotalQtyForSales(totReportDetails);
                double totalSgst = calculateTotalSgstForSales(totReportDetails);
                double totalCgst = calculateTotalCgstForSales(totReportDetails);
                double totalIgst = calculateTotalIgstForSales(totReportDetails);
                double totalTcs = calculateTotalTcsForSales(totReportDetails);
                double totalTaxable =
                    calculateTotalTaxableForSales(totReportDetails);
                double totalNetTot =
                    calculateTotalNetTotForSales(totReportDetails);
                for (var bill in reportDetails!) {
                  for (var billDetail in bill.billDetails) {
                    var Current_bill = billDetail.bll;
                    late String partybrokervalue;

                    if (!rowsByBillBll.containsKey(Current_bill)) {
                      rowsByBillBll[Current_bill] = [];
                    }
                    if (reptwise == "PartyWise") {
                      partybrokervalue = billDetail.bcd;
                    } else {
                      partybrokervalue = bill.party;
                    }

                    rowsByBillBll[Current_bill]!.add(
                      DataRow(
                        cells: [
                          DataCell(Text(billDetail.bll)),
                          DataCell(Text(DateFormat('dd/MM/yyyy')
                              .format(DateTime.parse(billDetail.dt)))),
                          DataCell(
                            Container(
                              width: 100, // Set the desired width
                              child: Text(partybrokervalue),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 50, // Set the desired width
                              child: Text(billDetail.tps),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 50, // Set the desired width
                              child: Text(formatNumber(billDetail.tq)),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 50, // Set the desired width
                              child: Text(billDetail.sgsa),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 50, // Set the desired width
                              child: Text(billDetail.cgsa),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 50, // Set the desired width
                              child: Text(billDetail.igsa),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 50, // Set the desired width
                              child: Text(billDetail.ta),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 80, // Set the desired width
                              child: Text(billDetail.taa),
                            ),
                          ),
                          DataCell(
                            Container(
                              width: 80, // Set the desired width
                              child: Text(billDetail.na),
                            ),
                          ),
                        ],
                      ),
                    );
                  }
                }
                DataRow totalDataRow = DataRow(
                  color: MaterialStateProperty.all(Colors.transparent),
                  cells: [
                    DataCell(
                        Text('Total:', style: TextStyle(color: Colors.white))),
                    DataCell(Text('')), // Leave the BILL.DT cell empty
                    DataCell(Text('')), // Leave the BROKER cell empty
                    DataCell(Text(totalPcs.toStringAsFixed(2),
                        style: TextStyle(
                            color: Colors.white))), // Show the total T.PCS
                    DataCell(Text(totalQty.toStringAsFixed(2),
                        style: TextStyle(
                            color:
                                Colors.white))), // Leave the T.QTY cell empty
                    DataCell(Text(totalSgst.toStringAsFixed(2),
                        style: TextStyle(
                            color: Colors.white))), // Leave the SGST cell empty
                    DataCell(Text(totalCgst.toStringAsFixed(2),
                        style: TextStyle(
                            color: Colors.white))), // Leave the CGST cell empty
                    DataCell(Text(totalIgst.toStringAsFixed(2),
                        style: TextStyle(
                            color: Colors.white))), // Leave the IGST cell empty
                    DataCell(Text(totalTcs.toStringAsFixed(2),
                        style: TextStyle(
                            color: Colors.white))), // Leave the TCS cell empty
                    DataCell(Text(totalTaxable.toStringAsFixed(2),
                        style: TextStyle(
                            color:
                                Colors.white))), // Leave the TAXABLE cell empty
                    DataCell(Text(totalNetTot.toStringAsFixed(2),
                        style: TextStyle(
                            color:
                                Colors.white))), // Leave the NET.TOT cell empty
                  ],
                );

                List<DataRow> allRows =
                    rowsByBillBll.values.expand((rows) => rows).toList();
                allRows.add(totalDataRow);
                late String label;
                late String columnvalue;
                if (reptwise == "PartyWise") {
                  label = "Party :- ";
                  columnvalue = "BROKER";
                } else if (reptwise == "BrokerWise") {
                  label = "Broker :- ";
                  columnvalue = "PARTY";
                }
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        label + ' ${party}',
                        style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 18,
                            color: Colors.black),
                      ),
                    ),
                    DataTable(
                      horizontalMargin: 6.0,
                      columnSpacing: 12.0,
                      dataRowHeight: 32.0,
                      decoration: BoxDecoration(
                        color: Colors.blue,
                        border: Border.symmetric(),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      dataRowColor: MaterialStateProperty.all(Colors.white),
                      columns: <DataColumn>[
                        DataColumn(
                            label: Text('BILLNO',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('BILL.DT',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text(columnvalue,
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('T.PCS',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('T.QTY',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('SGST',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('CGST',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('IGST',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('TCS',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('TAXABLE',
                                style: TextStyle(color: Colors.white))),
                        DataColumn(
                            label: Text('NET.TOT',
                                style: TextStyle(color: Colors.white))),
                      ],
                      rows: allRows,
                    ),
                    Divider(),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }

  void _captureScreenshot() {
    final boundary = _screenshotKey.currentContext!.findRenderObject()
        as RenderRepaintBoundary;
    boundary.toImage().then((image) async {
      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      Uint8List uint8List = byteData!.buffer.asUint8List();
      getPdf(uint8List);
      // Now you can use the Uint8List for saving or displaying the screenshot.
    });
  }

Give Me Solution For It. Thanks In Advance.

enter image description here

I Tried Most Of all ways but Errors shows in code

  1. RenderBox was not laid out
  2. performResize()
  3. So Many Errors

Give me Solution On It.

0

There are 0 best solutions below