Flutter pdf 包复选框

问题描述 投票:0回答:1

我在我的 flutter 项目中创建了一个 pdf 文档。但复选框不会显示。该行在文本前面有空格,但它是空白的,没有复选框或任何东西。这是在我更新 pdf 包依赖项之后发生的,从那里开始,复选框将不会显示。首先,在我更新包之前,会出现复选框,但是即使我将值设置为true,复选框也没有标记,导出的pdf只有框但没有突出显示,我尝试返回到之前的包版本我更新了它,它至少显示了复选框,但任何其他版本都是相同的,该复选框不再显示。可能是什么原因?

这是我创建 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);
  }
}
flutter dart pdf checkbox
1个回答
0
投票

我使用了带有 BoxDecoration 的标准容器。

 Container(
              width: 10,
              height: 10,
              decoration: BoxDecoration(
                border: Border.all(
                  color: PdfColors.black,
                  width: 2.0,
                ),
              ),
            ),
© www.soinside.com 2019 - 2024. All rights reserved.