DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.41k stars 630 forks source link

How to avoid widget split when use MultiPage? #1687

Open silva6 opened 4 months ago

silva6 commented 4 months ago

hi guys~ I have define a widget with a border, when I use multipage, this widget is split not as expected: the first page has a blank rectangle, like this:

image

it's the code:

var block = pw.Container(
      padding: pw.EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 5),
      margin: pw.EdgeInsets.only(right: 10, top: 10, bottom: 5),
      width: 600.rpx,
      decoration: pw.BoxDecoration(
          shape: pw.BoxShape.rectangle,
          borderRadius: pw.BorderRadius.all(pw.Radius.circular(10)),
          border: pw.Border.all(width: 1, style: pw.BorderStyle.dashed)),
      child: pw.Wrap(
        spacing: 20.0,
        runSpacing: 10.0,
        children: wordsList,
      ),
    );

how to remove this blank rectangle, and is there any way to avoid split this widget?

Many thanks!

CatHood0 commented 3 months ago

Use a Wrap widget as the parent of your "box" because Container is a SpanningWidget that can span to the next page making (accidentally) an unexpected displaying of some custom widgets.

Code:

var block = pw.Wrap(
  spacing: 20.0,
  runSpacing: 10.0,
  children: [
    pw.Container(
      padding: pw.EdgeInsets.only(left: 10, right: 10, top: 5, bottom: 5),
      margin: pw.EdgeInsets.only(right: 10, top: 10, bottom: 5),
      width: 600.rpx,
      decoration: pw.BoxDecoration(
        shape: pw.BoxShape.rectangle,
        borderRadius: pw.BorderRadius.all(pw.Radius.circular(10)),
        border: pw.Border.all(width: 1, style: pw.BorderStyle.dashed)),
      child: pw.Column(
        children: wordsList,
     ),
   ),
  ],
);