Skip to content

feat: reduce boilerplate for top-level inherited data #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kszczek
Copy link

@kszczek kszczek commented Mar 6, 2025

First of all, I want to thank you for developing this package. It has significantly simplified PDF generation in my Flutter apps and I much prefer using this package over rendering HTML templates to PDF, which I previously did.

However, I’ve encountered a slight usability issue with inherited data. Recently, while working on an invoice PDF, I defined several inherited classes:

class PdfLocale extends Inherited { ... }
class PdfColorScheme extends Inherited { ... }
class PdfAssets extends Inherited { ... }
class PdfInvoiceData extends Inherited { ... }

I wanted to make all of this data accessible throughout the document’s widget tree, but I found that it required a lot of boilerplate. Currently, each data object must be wrapped in an InheritedWidget leading to deeply nested structures like this:

Page(
  build: (final pw.Context context) => pw.InheritedWidget(
    inherited: colorScheme,
    build: (final pw.Context context) => pw.InheritedWidget(
      inherited: assets,
      build: (final pw.Context context) => pw.InheritedWidget(
        inherited: data,
        build: (final pw.Context context) => pw.InheritedWidget(
          inherited: locale,
          build: (final pw.Context context) => PdfInvoice(),
        ),
      ),
    ),
  ),
)

And it only gets worse when using MultiPage, as every individual widget needs to be wrapped in the same manner.

To address this, I’ve introduced a new inherited data list for both Page and MultiPage, allowing inherited data to be inserted directly into the page’s base context. This significantly reduces boilerplate:

Page(
  inherited: [colorScheme, assets, data, locale],
  build: (final pw.Context context) => PdfInvoice(),
)

I’d love to hear your thoughts on this approach and any further improvements needed to get this merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant