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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pdf/lib/src/widgets/multi_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class MultiPage extends Page {
PageOrientation? orientation,
EdgeInsetsGeometry? margin,
TextDirection? textDirection,
List<Inherited>? inherited,
}) : _buildList = build,
assert(maxPages > 0),
super(
Expand All @@ -171,6 +172,7 @@ class MultiPage extends Page {
theme: theme,
orientation: orientation,
textDirection: textDirection,
inherited: inherited,
);

final BuildListCallback _buildList;
Expand Down Expand Up @@ -247,6 +249,7 @@ class MultiPage extends Page {
calculatedTheme,
if (pageTheme.textDirection != null)
InheritedDirectionality(pageTheme.textDirection),
...inherited,
]);
final children = _buildList(baseContext);
WidgetContext? widgetContext;
Expand Down
5 changes: 5 additions & 0 deletions pdf/lib/src/widgets/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Page {
EdgeInsetsGeometry? margin,
bool clip = false,
TextDirection? textDirection,
List<Inherited>? inherited,
}) : assert(
pageTheme == null ||
(pageFormat == null &&
Expand All @@ -61,6 +62,7 @@ class Page {
clip: clip,
textDirection: textDirection,
),
inherited = inherited ?? <Inherited>[],
_build = build;

final PageTheme pageTheme;
Expand All @@ -81,6 +83,8 @@ class Page {

EdgeInsets? get resolvedMargin => margin?.resolve(pageTheme.textDirection);

final List<Inherited> inherited;

@protected
void debugPaint(Context context) {
final _margin = resolvedMargin!;
Expand Down Expand Up @@ -133,6 +137,7 @@ class Page {
calculatedTheme,
if (pageTheme.textDirection != null)
InheritedDirectionality(pageTheme.textDirection),
...inherited,
]);

Widget? background;
Expand Down
131 changes: 131 additions & 0 deletions pdf/test/inherited_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2025, Kamil Szczęk <kamil@szczek.dev>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'dart:io';

import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:test/test.dart';

class Locale extends Inherited {
Locale({required this.languageCode});

final String languageCode;

static String of(final Context context) {
return context.dependsOn<Locale>()!.languageCode;
}
}

class ColorScheme extends Inherited {
ColorScheme({
required this.primary,
required this.secondary,
});

final PdfColor primary;
final PdfColor secondary;

static ColorScheme of(final Context context) {
return context.dependsOn<ColorScheme>()!;
}
}

class Greeting extends StatelessWidget {
@override
Widget build(final Context context) {
final languageCode = Locale.of(context);
final colorScheme = ColorScheme.of(context);
return Text(
languageCode == 'pl' ? 'Witaj' : 'Welcome',
style: TextStyle(
color: colorScheme.primary,
background: BoxDecoration(color: colorScheme.secondary),
),
textAlign: TextAlign.center,
);
}
}

void main() {
late Document pdf;

setUpAll(() {
Document.debug = true;
RichText.debug = true;
pdf = Document();
});

test('Page should pass inherited data down the widget tree', () {
pdf.addPage(
Page(
inherited: <Inherited>[
Locale(languageCode: 'en'),
ColorScheme(primary: PdfColors.red, secondary: PdfColors.yellow),
],
build: (final Context context) => Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Greeting(),
InheritedWidget(
inherited: ColorScheme(
primary: PdfColors.green,
secondary: PdfColors.black,
),
build: (final Context context) => Greeting(),
),
InheritedWidget(
inherited: Locale(languageCode: 'pl'),
build: (final Context context) => Greeting(),
),
],
),
),
);
});

test('MultiPage should pass inherited data down the widget tree', () {
pdf.addPage(
MultiPage(
crossAxisAlignment: CrossAxisAlignment.center,
inherited: <Inherited>[
Locale(languageCode: 'en'),
ColorScheme(primary: PdfColors.red, secondary: PdfColors.yellow),
],
build: (final Context context) => <Widget>[
Greeting(),
InheritedWidget(
inherited: ColorScheme(
primary: PdfColors.green,
secondary: PdfColors.black,
),
build: (final Context context) => Greeting(),
),
InheritedWidget(
inherited: Locale(languageCode: 'pl'),
build: (final Context context) => Greeting(),
),
],
),
);
});

tearDownAll(() async {
final file = File('inherited.pdf');
await file.writeAsBytes(await pdf.save());
});
}
Binary file added test/golden/inherited.pdf
Binary file not shown.