为什么pdf CustomPaint不渲染,除非渲染其他内容



我不知道为什么渲染CustomPaint只有在它之后渲染其他东西时才能工作。

我使用flutter pdf,这是应该更容易复制的代码:

static pw.Widget buildBarcode() {
return pw.BarcodeWidget(
color: PdfColor.fromHex("#000000"),
barcode: pw.Barcode.qrCode(),
data: "mailto:someone@somewhere",
height: 80,
width: 80);
}
static pw.Widget buildCustomPaint() {
final paint = pw.CustomPaint(
size: const PdfPoint(85 * PdfPageFormat.mm, 55 * PdfPageFormat.mm),
painter: (PdfGraphics canvas, PdfPoint size) {
canvas
..setStrokeColor(PdfColors.black)
..setFillColor(PdfColors.lightBlue)
..setColor(PdfColors.lightBlue)
..drawRRect(
0, 0, 85 * PdfPageFormat.mm, 55 * PdfPageFormat.mm, 6, 6);
});
return paint;
}

class PdfGridApi {
static Future<File> generate(List list) async {
final pdf = pw.Document();
pdf.addPage(pw.MultiPage(
pageFormat: PdfPageFormat.a4,
build: (context) => [ 
buildCustomPaint(),
buildBarcode()
],
));
return PdfApi.saveDocument(name: 'my_contactcard.pdf', pdf: pdf);
}

如果两者都被渲染,一切都如预期,但如果我删除buildBarcode((,就不会再渲染任何东西。

CustomPaint中缺少什么?

您需要调用canvas.strokePath()canvas.fillPath()来绘制先前定义的矩形的轮廓。

最新更新