David PHAM-VAN

Improve Build Context

... ... @@ -104,8 +104,11 @@ class Page extends BasePage {
final Theme calculatedTheme = theme ?? document.theme ?? Theme.base();
final Map<Type, Inherited> inherited = <Type, Inherited>{};
inherited[calculatedTheme.runtimeType] = calculatedTheme;
final Context context =
Context(page: pdfPage, canvas: canvas, inherited: inherited);
final Context context = Context(
document: document.document,
page: pdfPage,
canvas: canvas,
inherited: inherited);
if (_build != null) {
final Widget child = _build(context);
layout(child, context, constraints);
... ... @@ -187,7 +190,9 @@ class MultiPage extends Page {
double offsetEnd;
double offsetStart;
int index = 0;
final List<Widget> children = _buildList(Context(inherited: inherited));
final Context baseContext =
Context(document: document.document, inherited: inherited);
final List<Widget> children = _buildList(baseContext);
WidgetContext widgetContext;
while (index < children.length) {
... ... @@ -197,7 +202,7 @@ class MultiPage extends Page {
final PdfPage pdfPage =
PdfPage(document.document, pageFormat: pageFormat);
final PdfGraphics canvas = pdfPage.getGraphics();
context = Context(page: pdfPage, canvas: canvas, inherited: inherited);
context = baseContext.copyWith(page: pdfPage, canvas: canvas);
assert(() {
if (Document.debug) {
debugPaint(context);
... ...
... ... @@ -125,7 +125,7 @@ class Font {
PdfFont getFont(Context context) {
if (_pdfFont == null) {
final PdfDocument pdfDocument = context.page.pdfDocument;
final PdfDocument pdfDocument = context.document;
_pdfFont = buildFont(pdfDocument);
}
... ...
... ... @@ -18,7 +18,13 @@ part of widget;
@immutable
class Context {
const Context({this.page, this.canvas, this.inherited});
const Context({
@required this.document,
this.page,
this.canvas,
@required this.inherited,
}) : assert(document != null),
assert(inherited != null);
final PdfPage page;
... ... @@ -26,11 +32,19 @@ class Context {
final Map<Type, Inherited> inherited;
int get pageNumber => page.pdfDocument.pdfPageList.pages.indexOf(page) + 1;
final PdfDocument document;
int get pageNumber => document.pdfPageList.pages.indexOf(page) + 1;
int get pagesCount => document.pdfPageList.pages.length;
Context copyWith(
{PdfPage page, PdfGraphics canvas, Map<Type, Inherited> inherited}) {
{PdfPage page,
PdfGraphics canvas,
Matrix4 ctm,
Map<Type, Inherited> inherited}) {
return Context(
document: document,
page: page ?? this.page,
canvas: canvas ?? this.canvas,
inherited: inherited ?? this.inherited);
... ... @@ -48,12 +62,17 @@ class Inherited {}
abstract class Widget {
Widget();
/// The bounding box of this widget, calculated at layout time
PdfRect box;
/// First widget pass to calculate the children layout and
/// bounding [box]
@protected
void layout(Context context, BoxConstraints constraints,
{bool parentUsesSize = false});
/// Draw itself and its children, according to the calculated
/// [box.offset]
@protected
void paint(Context context) {
assert(() {
... ...