David PHAM-VAN

Add FullPage widget

@@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
17 - Implement Image orientation 17 - Implement Image orientation
18 - Add Exif reader 18 - Add Exif reader
19 - Add support for GreyScale Jpeg 19 - Add support for GreyScale Jpeg
  20 +- Add FullPage widget
20 21
21 ## 1.3.23 22 ## 1.3.23
22 23
@@ -666,3 +666,74 @@ class Builder extends StatelessWidget { @@ -666,3 +666,74 @@ class Builder extends StatelessWidget {
666 @override 666 @override
667 Widget build(Context context) => builder(context); 667 Widget build(Context context) => builder(context);
668 } 668 }
  669 +
  670 +class FullPage extends SingleChildWidget {
  671 + FullPage({
  672 + @required this.ignoreMargins,
  673 + Widget child,
  674 + }) : assert(ignoreMargins != null),
  675 + super(child: child);
  676 +
  677 + final bool ignoreMargins;
  678 +
  679 + BoxConstraints _getConstraints(Context context) {
  680 + return ignoreMargins
  681 + ? BoxConstraints.tightFor(
  682 + width: context.page.pageFormat.width,
  683 + height: context.page.pageFormat.height,
  684 + )
  685 + : BoxConstraints.tightFor(
  686 + width: context.page.pageFormat.availableWidth,
  687 + height: context.page.pageFormat.availableHeight,
  688 + );
  689 + }
  690 +
  691 + PdfRect _getBox(Context context) {
  692 + final PdfRect box = _getConstraints(context).constrainRect();
  693 + if (ignoreMargins) {
  694 + return box;
  695 + }
  696 +
  697 + return PdfRect.fromPoints(
  698 + PdfPoint(
  699 + context.page.pageFormat.marginLeft,
  700 + context.page.pageFormat.marginTop,
  701 + ),
  702 + box.size);
  703 + }
  704 +
  705 + @override
  706 + void layout(Context context, BoxConstraints constraints,
  707 + {bool parentUsesSize = false}) {
  708 + final BoxConstraints constraints = _getConstraints(context);
  709 +
  710 + if (child != null) {
  711 + child.layout(context, constraints, parentUsesSize: false);
  712 + assert(child.box != null);
  713 + }
  714 +
  715 + box = _getBox(context);
  716 + print('layout box: $box');
  717 + }
  718 +
  719 + @override
  720 + void debugPaint(Context context) {}
  721 +
  722 + @override
  723 + void paint(Context context) {
  724 + super.paint(context);
  725 +
  726 + if (child == null) {
  727 + return;
  728 + }
  729 +
  730 + final PdfRect box = _getBox(context);
  731 + final Matrix4 mat = Matrix4.tryInvert(context.canvas.getTransform());
  732 + mat.translate(box.x, box.y);
  733 + context.canvas
  734 + ..saveContext()
  735 + ..setTransform(mat);
  736 + child.paint(context);
  737 + context.canvas.restoreContext();
  738 + }
  739 +}
@@ -104,6 +104,14 @@ class Page { @@ -104,6 +104,14 @@ class Page {
104 page: _pdfPage, 104 page: _pdfPage,
105 canvas: canvas, 105 canvas: canvas,
106 ).inheritFrom(calculatedTheme); 106 ).inheritFrom(calculatedTheme);
  107 +
  108 + assert(() {
  109 + if (Document.debug) {
  110 + debugPaint(context);
  111 + }
  112 + return true;
  113 + }());
  114 +
107 if (pageTheme.buildBackground != null) { 115 if (pageTheme.buildBackground != null) {
108 final Widget child = pageTheme.buildBackground(context); 116 final Widget child = pageTheme.buildBackground(context);
109 if (child != null) { 117 if (child != null) {
@@ -111,6 +119,7 @@ class Page { @@ -111,6 +119,7 @@ class Page {
111 paint(child, context); 119 paint(child, context);
112 } 120 }
113 } 121 }
  122 +
114 if (_build != null) { 123 if (_build != null) {
115 final Widget child = _build(context); 124 final Widget child = _build(context);
116 if (child != null) { 125 if (child != null) {
@@ -118,6 +127,7 @@ class Page { @@ -118,6 +127,7 @@ class Page {
118 paint(child, context); 127 paint(child, context);
119 } 128 }
120 } 129 }
  130 +
121 if (pageTheme.buildForeground != null) { 131 if (pageTheme.buildForeground != null) {
122 final Widget child = pageTheme.buildForeground(context); 132 final Widget child = pageTheme.buildForeground(context);
123 if (child != null) { 133 if (child != null) {
@@ -144,13 +154,6 @@ class Page { @@ -144,13 +154,6 @@ class Page {
144 154
145 @protected 155 @protected
146 void paint(Widget child, Context context) { 156 void paint(Widget child, Context context) {
147 - assert(() {  
148 - if (Document.debug) {  
149 - debugPaint(context);  
150 - }  
151 - return true;  
152 - }());  
153 -  
154 if (child == null) { 157 if (child == null) {
155 return; 158 return;
156 } 159 }
@@ -25,7 +25,10 @@ void main() { @@ -25,7 +25,10 @@ void main() {
25 final Document pdf = Document(); 25 final Document pdf = Document();
26 26
27 final PageTheme pageTheme = PageTheme( 27 final PageTheme pageTheme = PageTheme(
28 - buildBackground: (Context context) => Watermark.text('DRAFT'), 28 + buildBackground: (Context context) => FullPage(
  29 + ignoreMargins: true,
  30 + child: Watermark.text('DRAFT'),
  31 + ),
29 buildForeground: (Context context) => Align( 32 buildForeground: (Context context) => Align(
30 alignment: Alignment.bottomLeft, 33 alignment: Alignment.bottomLeft,
31 child: SizedBox( 34 child: SizedBox(