David PHAM-VAN

Set a proper value to context.pagesCount

... ... @@ -6,6 +6,7 @@
* Fix RichText Widget word spacing
* Improve Theme and TextStyle
* Implement properly RichText.softWrap
* Set a proper value to context.pagesCount
# 1.3.7
* Add Pdf Creation date
... ...
... ... @@ -30,7 +30,7 @@ void main() {
return Container(
alignment: Alignment.centerRight,
margin: const EdgeInsets.only(top: 1.0 * PdfPageFormat.cm),
child: Text('Page ${context.pageNumber}',
child: Text('Page ${context.pageNumber} of ${context.pagesCount}',
style: Theme.of(context)
.defaultTextStyle
.copyWith(color: PdfColors.grey)));
... ...
... ... @@ -50,11 +50,19 @@ class Document {
final Theme theme;
final List<Page> _pages = <Page>[];
void addPage(Page page) {
page.generate(this);
_pages.add(page);
}
List<int> save() => document.save();
List<int> save() {
for (Page page in _pages) {
page.postProcess(this);
}
return document.save();
}
}
typedef BuildCallback = Widget Function(Context context);
... ... @@ -63,7 +71,7 @@ typedef BuildListCallback = List<Widget> Function(Context context);
enum PageOrientation { natural, landscape, portrait }
class Page {
const Page(
Page(
{this.pageFormat = PdfPageFormat.standard,
BuildCallback build,
this.theme,
... ... @@ -89,6 +97,8 @@ class Page {
(orientation == PageOrientation.portrait &&
pageFormat.width > pageFormat.height);
PdfPage _pdfPage;
EdgeInsets get margin {
if (_margin != null) {
if (mustRotate) {
... ... @@ -127,8 +137,12 @@ class Page {
@protected
void generate(Document document) {
final PdfPage pdfPage = PdfPage(document.document, pageFormat: pageFormat);
final PdfGraphics canvas = pdfPage.getGraphics();
_pdfPage = PdfPage(document.document, pageFormat: pageFormat);
}
@protected
void postProcess(Document document) {
final PdfGraphics canvas = _pdfPage.getGraphics();
final EdgeInsets _margin = margin;
final BoxConstraints constraints = mustRotate
? BoxConstraints(
... ... @@ -143,7 +157,7 @@ class Page {
inherited[calculatedTheme.runtimeType] = calculatedTheme;
final Context context = Context(
document: document.document,
page: pdfPage,
page: _pdfPage,
canvas: canvas,
inherited: inherited);
if (_build != null) {
... ...
... ... @@ -36,8 +36,19 @@ class NewPage extends Widget {
}
}
class _MultiPageInstance {
const _MultiPageInstance(
{@required this.context,
@required this.constraints,
@required this.offsetStart});
final Context context;
final BoxConstraints constraints;
final double offsetStart;
}
class MultiPage extends Page {
const MultiPage(
MultiPage(
{PdfPageFormat pageFormat = PdfPageFormat.a4,
BuildListCallback build,
this.crossAxisAlignment = CrossAxisAlignment.start,
... ... @@ -61,6 +72,8 @@ class MultiPage extends Page {
final BuildCallback footer;
final List<_MultiPageInstance> _pages = <_MultiPageInstance>[];
void _paintChild(
Context context, Widget child, double x, double y, double pageHeight) {
if (mustRotate) {
... ... @@ -139,13 +152,17 @@ class MultiPage extends Page {
offsetEnd =
_mustRotate ? pageHeightMargin - _margin.left : _margin.bottom;
_pages.add(_MultiPageInstance(
context: context,
constraints: constraints,
offsetStart: offsetStart,
));
if (header != null) {
final Widget headerWidget = header(context);
if (headerWidget != null) {
headerWidget.layout(context, constraints, parentUsesSize: false);
assert(headerWidget.box != null);
_paintChild(context, headerWidget, _margin.left,
offsetStart - headerWidget.box.height, pageFormat.height);
offsetStart -= headerWidget.box.height;
}
}
... ... @@ -155,8 +172,6 @@ class MultiPage extends Page {
if (footerWidget != null) {
footerWidget.layout(context, constraints, parentUsesSize: false);
assert(footerWidget.box != null);
_paintChild(context, footerWidget, _margin.left, _margin.bottom,
pageFormat.height);
offsetEnd += footerWidget.box.height;
}
}
... ... @@ -218,4 +233,33 @@ class MultiPage extends Page {
index++;
}
}
@override
void postProcess(Document document) {
final EdgeInsets _margin = margin;
for (_MultiPageInstance page in _pages) {
if (header != null) {
final Widget headerWidget = header(page.context);
if (headerWidget != null) {
headerWidget.layout(page.context, page.constraints,
parentUsesSize: false);
assert(headerWidget.box != null);
_paintChild(page.context, headerWidget, _margin.left,
page.offsetStart - headerWidget.box.height, pageFormat.height);
}
}
if (footer != null) {
final Widget footerWidget = footer(page.context);
if (footerWidget != null) {
footerWidget.layout(page.context, page.constraints,
parentUsesSize: false);
assert(footerWidget.box != null);
_paintChild(page.context, footerWidget, _margin.left, _margin.bottom,
pageFormat.height);
}
}
}
}
}
... ...
... ... @@ -36,6 +36,9 @@ class Context {
int get pageNumber => document.pdfPageList.pages.indexOf(page) + 1;
/// Number of pages in the document.
/// This value is not available in a MultiPage body and will be equal to pageNumber.
/// But can be used in Header and Footer.
int get pagesCount => document.pdfPageList.pages.length;
Context copyWith(
... ...