David PHAM-VAN

Fix Image shape inside BoxDecoration

# Changelog
## 1.3.15
* Fix Image shape inside BoxDecoration
## 1.3.14
* Add Document ID
... ...
... ... @@ -42,7 +42,7 @@ class BoxBorder {
/// The width of the
final double width;
void paintBorders(Context context, PdfRect box) {
void paintRect(Context context, PdfRect box) {
assert(box.x != null);
assert(box.y != null);
assert(box.width != null);
... ... @@ -85,6 +85,34 @@ class BoxBorder {
context.canvas.strokePath();
}
}
void paintEllipse(Context context, PdfRect box) {
assert(box.x != null);
assert(box.y != null);
assert(box.width != null);
assert(box.height != null);
context.canvas
..setStrokeColor(color)
..setLineWidth(width)
..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,
box.width / 2.0, box.height / 2.0)
..strokePath();
}
void paintRRect(Context context, PdfRect box, double borderRadius) {
assert(box.x != null);
assert(box.y != null);
assert(box.width != null);
assert(box.height != null);
context.canvas
..setStrokeColor(color)
..setLineWidth(width)
..drawRRect(
box.x, box.y, box.width, box.height, borderRadius, borderRadius)
..strokePath();
}
}
@immutable
... ... @@ -101,7 +129,7 @@ class DecorationImage {
final BoxFit fit;
final Alignment alignment;
void paintImage(Context context, PdfRect box) {
void paint(Context context, PdfRect box) {
final PdfPoint imageSize =
PdfPoint(image.width.toDouble(), image.height.toDouble());
final FittedSizes sizes = applyBoxFit(fit, imageSize, box.size);
... ... @@ -134,7 +162,8 @@ class BoxDecoration {
this.border,
this.borderRadius,
this.image,
this.shape = BoxShape.rectangle});
this.shape = BoxShape.rectangle})
: assert(shape != null);
/// The color to fill in the background of the box.
final PdfColor color;
... ... @@ -143,7 +172,7 @@ class BoxDecoration {
final BoxShape shape;
final DecorationImage image;
void paintBackground(Context context, PdfRect box) {
void paint(Context context, PdfRect box) {
assert(box.x != null);
assert(box.y != null);
assert(box.width != null);
... ... @@ -168,6 +197,44 @@ class BoxDecoration {
..setFillColor(color)
..fillPath();
}
if (image != null) {
context.canvas.saveContext();
switch (shape) {
case BoxShape.circle:
context.canvas
..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,
box.width / 2.0, box.height / 2.0)
..clipPath();
break;
case BoxShape.rectangle:
if (borderRadius != null) {
context.canvas
..drawRRect(box.x, box.y, box.width, box.height, borderRadius,
borderRadius)
..clipPath();
}
break;
}
image.paint(context, box);
context.canvas.restoreContext();
}
if (border != null) {
switch (shape) {
case BoxShape.circle:
border.paintEllipse(context, box);
break;
case BoxShape.rectangle:
if (borderRadius != null) {
border.paintRRect(context, box, borderRadius);
} else {
border.paintRect(context, box);
}
break;
}
}
}
}
... ... @@ -190,15 +257,11 @@ class DecoratedBox extends SingleChildWidget {
void paint(Context context) {
super.paint(context);
if (position == DecorationPosition.background) {
decoration.paintBackground(context, box);
decoration.image?.paintImage(context, box);
decoration.border?.paintBorders(context, box);
decoration.paint(context, box);
}
paintChild(context);
if (position == DecorationPosition.foreground) {
decoration.paintBackground(context, box);
decoration.image?.paintImage(context, box);
decoration.border?.paintBorders(context, box);
decoration.paint(context, box);
}
}
}
... ...
... ... @@ -53,10 +53,9 @@ class TableBorder extends BoxBorder {
final bool horizontalInside;
final bool verticalInside;
@override
void paintBorders(Context context, PdfRect box,
void paint(Context context, PdfRect box,
[List<double> widths, List<double> heights]) {
super.paintBorders(context, box);
super.paintRect(context, box);
if (verticalInside) {
double offset = box.x;
... ... @@ -307,7 +306,7 @@ class Table extends Widget implements SpanningWidget {
context.canvas.restoreContext();
if (border != null) {
border.paintBorders(context, box, _widths, _heights);
border.paint(context, box, _widths, _heights);
}
}
}
... ...
... ... @@ -4,7 +4,7 @@ description: A pdf producer for Dart. It can create pdf files for both web or fl
homepage: https://github.com/DavBfr/dart_pdf/tree/master/pdf
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 1.3.14
version: 1.3.15
environment:
sdk: ">=2.1.0 <3.0.0"
... ...
... ... @@ -90,6 +90,80 @@ void main() {
));
});
test('Container Widgets BoxShape Image', () {
final Uint32List img = generateBitmap(100, 100);
final PdfImage image = PdfImage(pdf.document,
image: img.buffer.asUint8List(), width: 100, height: 100);
pdf.addPage(Page(
build: (Context context) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 200.0,
width: 200.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(image: image, fit: BoxFit.cover),
),
),
Container(
height: 200.0,
width: 200.0,
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: 40,
image: DecorationImage(image: image, fit: BoxFit.cover),
),
),
],
),
),
));
});
test('Container Widgets BoxShape Border', () {
pdf.addPage(Page(
build: (Context context) => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 200.0,
width: 200.0,
decoration: const BoxDecoration(
shape: BoxShape.circle,
border: BoxBorder(
bottom: true,
top: true,
left: true,
right: true,
color: PdfColors.blue,
width: 3),
),
),
Container(
height: 200.0,
width: 200.0,
decoration: const BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: 40,
border: BoxBorder(
bottom: true,
top: true,
left: true,
right: true,
color: PdfColors.blue,
width: 3),
),
),
],
),
),
));
});
tearDownAll(() {
final File file = File('widgets-container.pdf');
file.writeAsBytesSync(pdf.save());
... ...
... ... @@ -13,7 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
pdf: "^1.3.14"
pdf: "^1.3.15"
dev_dependencies:
flutter_test:
... ...