mardi 9 mai 2023

Flutter pdf package checkbox

I created a pdf document in my flutter project. but the checkbox won't show up. The row has space in front of the text, but it is blank, no checkbox or anything. This happened after I updated the pdf package dependency and starting from there the checkbox won't show up. At first, before I update the package, the checkbox shows up but the checkbox doesn't have a mark even if I set the value to true, the exported pdf only has box but not highlighted, I tried going back to the package version before I update it where it at least shows the checkbox, but any other version is the same, the checkbox did not show up ever again. What could be the cause?

Here is the class where I have the creation of pdf:

// ignore_for_file: prefer_interpolation_to_compose_strings

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:open_file/open_file.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:provider/provider.dart';
import 'package:qvin/utils/labelFormat.dart';

import '../../providers/dvirProvider.dart';

class PdfApi {
  static Future<File> generatePDF(
      BuildContext context,
      String? vehicleNo,
      String? remarks,
      bool isCondition,
      bool isAbove,
      bool isAboveDefect,
      bool isNoResult) async {
    final pdf = pw.Document();
    final font = await rootBundle.load("assets/hind.ttf");
    final ttf = pw.Font.ttf(font);

    DateTime dateToday = DateTime.now();
    final provider = Provider.of<DVIRProvider>(context, listen: false);
    final datasTruck = provider.checklistTruck;
    final datasTrailer = provider.checklistTrailer;

    final headers = [
      'TITLE',
      'STATUS',
    ];

    final dataTruck = datasTruck
        .map((e) => [
              e.title,
              e.isCheck == true ? 'x' : '/',
            ])
        .toList();

    final dataTrailer = datasTrailer
        .map((e) => [
              e.title,
              e.isCheck == true ? 'x' : '/',
            ])
        .toList();

    pdf.addPage(
      pw.MultiPage(
        theme: pw.ThemeData(defaultTextStyle: pw.TextStyle(font: ttf)),
        build: (context) => <pw.Widget>[
          pw.Center(
            child: pw.Text(
              "Driver's Vehicle Inspection Report",
              style: pw.TextStyle(fontSize: 24, fontWeight: pw.FontWeight.bold),
            ),
          ),
          pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
          pw.Center(
            child: pw.Text(
              "Check ANY Defective Item and Give Details under 'Remarks.'",
              style: const pw.TextStyle(
                fontSize: 14,
              ),
            ),
          ),
          pw.SizedBox(height: 2 * PdfPageFormat.cm),
          pw.Column(crossAxisAlignment: pw.CrossAxisAlignment.start, children: [
            pw.Text(
              "DATE: ${LabelFormat.formatDate(dateToday)}",
              style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold),
            ),
            pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
            pw.Text(
              "TRUCK/TRACTOR NO. $vehicleNo",
              style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold),
            ),
            pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
          ]),
          pw.Table.fromTextArray(
            data: dataTruck,
            headers: headers,
            cellHeight: 30,
            headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
            cellAlignments: {
              0: pw.Alignment.centerLeft,
              1: pw.Alignment.center,
            },
          ),
          pw.Column(crossAxisAlignment: pw.CrossAxisAlignment.start, children: [
            pw.Text(
              "TRAILER(S) NO (S). $vehicleNo",
              style: pw.TextStyle(fontSize: 12, fontWeight: pw.FontWeight.bold),
            ),
            pw.SizedBox(height: 0.5 * PdfPageFormat.cm),
          ]),
          pw.Table.fromTextArray(
            data: dataTrailer,
            headers: headers,
            cellHeight: 30,
            headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
            cellAlignments: {
              0: pw.Alignment.centerLeft,
              1: pw.Alignment.center,
            },
          ),
          pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
          pw.Text(
            "REMARKS: ${remarks ?? '--'}",
            style: pw.TextStyle(
              fontSize: 12,
              fontWeight: pw.FontWeight.bold,
            ),
          ),
          pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
          if (isNoResult == true)
            pw.Column(children: [
              pw.Row(children: [
                pw.Checkbox(
                  name: 'condition',
                  value: isCondition,
                ),
                pw.SizedBox(width: 0.8 * PdfPageFormat.cm),
                pw.Text(
                  'Condition of the above vehicle is satisfactory',
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
              ]),
              pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
              pw.Row(children: [
                pw.Text(
                  "Driver's Signature",
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
                pw.Spacer(),
                pw.Text(
                  'Date: ${LabelFormat.formatDate(dateToday)}',
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
              ]),
              pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
            ])
          else
            pw.Column(children: [
              pw.Row(children: [
                pw.Checkbox(
                  name: 'aboveDefectsCorrected',
                  value: true,
                ),
                pw.SizedBox(width: 0.8 * PdfPageFormat.cm),
                pw.Text(
                  'Above Defects Corrected',
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
                pw.Checkbox(
                  height: 20,
                  width: 20,
                  name: 'sample',
                  value: true,
                  tristate: true,
                  decoration: pw.BoxDecoration(
                    color: PdfColors.blue,
                  ),
                ),
              ]),
              pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
              pw.Row(children: [
                pw.Checkbox(
                  name: 'aboveDefectsNeed',
                  value: true,
                ),
                pw.SizedBox(width: 0.8 * PdfPageFormat.cm),
                pw.Text(
                  'Above Defects Need NOT Be Corrected For Safe Operation Of Vehicle',
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
              ]),
              pw.SizedBox(height: 0.8 * PdfPageFormat.cm),
              pw.Row(children: [
                pw.Text(
                  "Mechanic's Signature",
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
                pw.Spacer(),
                pw.Text(
                  'Date: ${LabelFormat.formatDate(dateToday)}',
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
              ]),
              pw.SizedBox(height: 0.4 * PdfPageFormat.cm),
              pw.Row(children: [
                pw.Text(
                  "Driver's Signature",
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
                pw.Spacer(),
                pw.Text(
                  'Date: ${LabelFormat.formatDate(dateToday)}',
                  style: const pw.TextStyle(
                    fontSize: 14,
                  ),
                ),
              ]),
            ]),
        ],
      ),
    );
    return saveDocument(name: 'sample.pdf', pdf: pdf);
  }

  static Future<File> getDocument({
    required String name,
    required pw.Document pdf,
  }) async {
    final bytes = await pdf.save();
    final dir = await getApplicationSupportDirectory();
    final file = File('${dir.path}/$name');
    await file.writeAsBytes(bytes);
    print({'file:$file'});
    return file;
  }

  static Future<File> saveDocument({
    required String name,
    required pw.Document pdf,
  }) async {
    final bytes = await pdf.save();
    final directory = await getApplicationDocumentsDirectory();
    final dvirDirectory = Directory('/storage/emulated/0/dvir documents');
    await dvirDirectory.create(recursive: true);
    final file = File('${dvirDirectory.path}/$name');
    await file.writeAsBytes(bytes);
    print('file saved to: ${file.path}');
    return file;
  }

  static Future openFIle(File file) async {
    final url = file.path;
    await OpenFile.open(url);
  }
}



Aucun commentaire:

Enregistrer un commentaire