flutterdart-pdf

How to make table as full page width in PDF generation in Flutter?


I'm generating PDF in my flutter app using pdf package pdf pub dev link, I have a table in it and would like to stretch the table to full page size instead of having the following:

enter image description here

To make the table start from the most left of the page, till the most right of the page without the (e.g. use 1 & 2 sections in the picture instead of leaving empty)

My table code is as following:

pw.TableHelper.fromTextArray(
      border: null,
      headerDecoration: pw.BoxDecoration(
        borderRadius: const pw.BorderRadius.all(pw.Radius.circular(2)),
        color: PdfColor.fromHex('#108dcd'),
      ),
      headerCellDecoration: cellDecoration,
      cellPadding: const pw.EdgeInsets.symmetric(horizontal: 3),
      headerHeight: 25,
      cellHeight: 25,
      columnWidths: getTableColumnsWidth(tableHeaders),
      cellAlignments: getTableColumnAlignment(tableHeaders),
      cellDecoration: getTableCellDecoration,
      headers: List<pw.Text>.generate(
        tableHeaders.length,
        (col) => tableHeaders[col],
      ),
      data: getDataTable(tableHeaders, quotationRoomsList),
    ),

while the width of the columns is:

Map<int, pw.FlexColumnWidth> getTableColumnsWidth(tableHeaders) {
  return {
    0: const pw.FlexColumnWidth(0.05),
    1: const pw.FlexColumnWidth(0.20),
    2: const pw.FlexColumnWidth(0.31),
    3: const pw.FlexColumnWidth(0.14),
    4: const pw.FlexColumnWidth(0.13),
    5: const pw.FlexColumnWidth(0.17),
  };
}

Solution

  • I have wrapped the pw.TableHelper.fromTextArray with a Container and set the margin attribute of the container to negative number.

    pw.Container(
          margin: pw.EdgeInsets.symmetric(horizontal: -45),
          child: pw.TableHelper.fromTextArray(
            border: null,
            ...),
     ),
    

    what jarinox suggester in the comment will work if need to set margin for all the page, hence, all widgets in it will be impacted. in my case I wanted only the table to be stretched.