Skip to content

Commit

Permalink
Allow generating page content to be asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamsmith committed Dec 2, 2024
1 parent d336536 commit 3bb3450
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
3 changes: 1 addition & 2 deletions pdf/lib/src/widgets/chart/bar_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import '../../../pdf.dart';
import '../flex.dart';
import '../geometry.dart';
import '../page.dart';
import '../widget.dart';
import 'chart.dart';
import 'grid_cartesian.dart';
Expand All @@ -39,7 +38,7 @@ class BarDataSet<T extends PointChartValue> extends PointDataSet<T> {
PdfColor? pointColor,
double pointSize = 3,
bool drawPoints = false,
BuildCallback? shape,
LegendBuildCallback? shape,
Widget Function(Context context, T value)? buildValue,
ValuePosition valuePosition = ValuePosition.auto,
}) : drawBorder = drawBorder ?? borderColor != null && color != borderColor,
Expand Down
3 changes: 1 addition & 2 deletions pdf/lib/src/widgets/chart/line_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import '../../../pdf.dart';
import '../geometry.dart';
import '../page.dart';
import '../widget.dart';
import 'chart.dart';
import 'grid_cartesian.dart';
Expand All @@ -38,7 +37,7 @@ class LineDataSet<T extends PointChartValue> extends PointDataSet<T> {
this.drawLine = true,
this.lineColor,
bool drawPoints = true,
BuildCallback? shape,
LegendBuildCallback? shape,
Widget Function(Context context, T value)? buildValue,
ValuePosition valuePosition = ValuePosition.auto,
this.drawSurface = false,
Expand Down
9 changes: 5 additions & 4 deletions pdf/lib/src/widgets/chart/point_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
import '../../../pdf.dart';
import '../basic.dart';
import '../geometry.dart';
import '../page.dart';
import '../widget.dart';
import 'chart.dart';

enum ValuePosition { left, top, right, bottom, auto }

typedef LegendBuildCallback = Widget Function(Context context);

class PointChartValue extends ChartValue {
const PointChartValue(this.x, this.y);
final double x;
Expand Down Expand Up @@ -56,7 +57,7 @@ class PointDataSet<T extends PointChartValue> extends Dataset {

final double pointSize;

final BuildCallback? shape;
final LegendBuildCallback? shape;

final Widget Function(Context context, T value)? buildValue;

Expand Down Expand Up @@ -94,7 +95,7 @@ class PointDataSet<T extends PointChartValue> extends Dataset {
}

@override
void paintForeground(Context context) {
Future<void> paintForeground(Context context) async {
super.paintForeground(context);

if (data.isEmpty) {
Expand All @@ -120,7 +121,7 @@ class PointDataSet<T extends PointChartValue> extends Dataset {
Widget.draw(
SizedBox.square(
dimension: pointSize * 2,
child: shape!(context),
child: await shape!(context),
),
offset: p,
alignment: Alignment.center,
Expand Down
19 changes: 10 additions & 9 deletions pdf/lib/src/widgets/multi_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ class MultiPage extends Page {
}

@override
void generate(Document document, {bool insert = true, int? index}) {
Future<void> generate(Document document,
{bool insert = true, int? index}) async {
assert(pageFormat.width > 0 && pageFormat.width < double.infinity);
assert(pageFormat.height > 0 && pageFormat.height < double.infinity);

Expand Down Expand Up @@ -248,7 +249,7 @@ class MultiPage extends Page {
if (pageTheme.textDirection != null)
InheritedDirectionality(pageTheme.textDirection),
]);
final children = _buildList(baseContext);
final children = await _buildList(baseContext);
WidgetContext? widgetContext;

while (_index < children.length) {
Expand Down Expand Up @@ -300,15 +301,15 @@ class MultiPage extends Page {
));

if (header != null) {
final headerWidget = header!(context);
final headerWidget = await header!(context);

headerWidget.layout(context, constraints, parentUsesSize: false);
assert(headerWidget.box != null);
offsetStart -= headerWidget.box!.height;
}

if (footer != null) {
final footerWidget = footer!(context);
final footerWidget = await footer!(context);

footerWidget.layout(context, constraints, parentUsesSize: false);
assert(footerWidget.box != null);
Expand Down Expand Up @@ -395,7 +396,7 @@ class MultiPage extends Page {
}

@override
void postProcess(Document document) {
Future<void> postProcess(Document document) async {
final _margin = resolvedMargin!;
final _mustRotate = mustRotate;
final pageHeight = _mustRotate ? pageFormat.width : pageFormat.height;
Expand All @@ -412,7 +413,7 @@ class MultiPage extends Page {
_mustRotate ? pageHeightMargin - _margin.left : _margin.bottom;

if (pageTheme.buildBackground != null) {
final child = pageTheme.buildBackground!(page.context);
final child = await pageTheme.buildBackground!(page.context);

child.layout(page.context, page.fullConstraints, parentUsesSize: false);
assert(child.box != null);
Expand Down Expand Up @@ -444,7 +445,7 @@ class MultiPage extends Page {
}

if (header != null) {
final headerWidget = header!(page.context);
final headerWidget = await header!(page.context);
headerWidget.layout(page.context, page.constraints,
parentUsesSize: false);
assert(headerWidget.box != null);
Expand All @@ -457,7 +458,7 @@ class MultiPage extends Page {
}

if (footer != null) {
final footerWidget = footer!(page.context);
final footerWidget = await footer!(page.context);
footerWidget.layout(page.context, page.constraints,
parentUsesSize: false);
assert(footerWidget.box != null);
Expand Down Expand Up @@ -580,7 +581,7 @@ class MultiPage extends Page {
}

if (pageTheme.buildForeground != null) {
final child = pageTheme.buildForeground!(page.context);
final child = await pageTheme.buildForeground!(page.context);

child.layout(page.context, page.fullConstraints, parentUsesSize: false);
assert(child.box != null);
Expand Down
13 changes: 7 additions & 6 deletions pdf/lib/src/widgets/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import 'dart:async';
import 'dart:math' as math;

import 'package:meta/meta.dart';
Expand All @@ -28,8 +29,8 @@ import 'text_style.dart';
import 'theme.dart';
import 'widget.dart';

typedef BuildCallback = Widget Function(Context context);
typedef BuildListCallback = List<Widget> Function(Context context);
typedef BuildCallback = FutureOr<Widget> Function(Context context);
typedef BuildListCallback = FutureOr<List<Widget>> Function(Context context);

enum PageOrientation { natural, landscape, portrait }

Expand Down Expand Up @@ -112,7 +113,7 @@ class Page {
}
}

void postProcess(Document document) {
Future<void> postProcess(Document document) async {
final canvas = _pdfPage!.getGraphics();
canvas.reset();
final _margin = resolvedMargin;
Expand All @@ -139,7 +140,7 @@ class Page {
Widget? content;
Widget? foreground;

content = _build(context);
content = await _build(context);

final size = layout(content, context, constraints);

Expand All @@ -156,12 +157,12 @@ class Page {
}

if (pageTheme.buildBackground != null) {
background = pageTheme.buildBackground!(context);
background = await pageTheme.buildBackground!(context);
layout(background, context, constraints);
}

if (pageTheme.buildForeground != null) {
foreground = pageTheme.buildForeground!(context);
foreground = await pageTheme.buildForeground!(context);
layout(foreground, context, constraints);
}

Expand Down
22 changes: 14 additions & 8 deletions pdf/lib/src/widgets/widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,13 @@ class InheritedWidget extends SingleChildWidget {
Widget? _child;

@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
void layout(
Context context,
BoxConstraints constraints, {
bool parentUsesSize = false,
}) async {
_context = inherited != null ? context.inheritFrom(inherited!) : context;
_child = build!(_context!);
_child = await build!(_context!);
super.layout(_context!, constraints);
}

Expand All @@ -394,14 +397,17 @@ class DelayedWidget extends SingleChildWidget {
Widget? _child;

@override
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false}) {
_child = build(context);
Future<void> layout(
Context context,
BoxConstraints constraints, {
bool parentUsesSize = false,
}) async {
_child = await build(context);
super.layout(context, constraints);
}

void delayedPaint(Context context) {
_child = build(context);
Future<void> delayedPaint(Context context) async {
_child = await build(context);
child!.layout(
context,
BoxConstraints.tight(box!.size),
Expand Down

0 comments on commit 3bb3450

Please sign in to comment.