Showing
2 changed files
with
43 additions
and
0 deletions
@@ -86,6 +86,44 @@ class BoxBorder { | @@ -86,6 +86,44 @@ class BoxBorder { | ||
86 | } | 86 | } |
87 | } | 87 | } |
88 | 88 | ||
89 | +@immutable | ||
90 | +class DecorationImage { | ||
91 | + const DecorationImage( | ||
92 | + {@required this.image, | ||
93 | + this.fit = BoxFit.cover, | ||
94 | + this.alignment = Alignment.center}) | ||
95 | + : assert(image != null), | ||
96 | + assert(fit != null), | ||
97 | + assert(alignment != null); | ||
98 | + | ||
99 | + final PdfImage image; | ||
100 | + final BoxFit fit; | ||
101 | + final Alignment alignment; | ||
102 | + | ||
103 | + void paintImage(Context context, PdfRect box) { | ||
104 | + final PdfPoint imageSize = | ||
105 | + PdfPoint(image.width.toDouble(), image.height.toDouble()); | ||
106 | + final FittedSizes sizes = applyBoxFit(fit, imageSize, box.size); | ||
107 | + final double scaleX = sizes.destination.x / sizes.source.x; | ||
108 | + final double scaleY = sizes.destination.y / sizes.source.y; | ||
109 | + final PdfRect sourceRect = alignment.inscribe( | ||
110 | + sizes.source, PdfRect.fromPoints(PdfPoint.zero, imageSize)); | ||
111 | + final PdfRect destinationRect = alignment.inscribe(sizes.destination, box); | ||
112 | + final Matrix4 mat = | ||
113 | + Matrix4.translationValues(destinationRect.x, destinationRect.y, 0) | ||
114 | + ..scale(scaleX, scaleY, 1) | ||
115 | + ..translate(-sourceRect.x, -sourceRect.y); | ||
116 | + | ||
117 | + context.canvas | ||
118 | + ..saveContext() | ||
119 | + ..drawRect(box.x, box.y, box.width, box.height) | ||
120 | + ..clipPath() | ||
121 | + ..setTransform(mat) | ||
122 | + ..drawImage(image, 0, 0, imageSize.x, imageSize.y) | ||
123 | + ..restoreContext(); | ||
124 | + } | ||
125 | +} | ||
126 | + | ||
89 | enum BoxShape { circle, rectangle } | 127 | enum BoxShape { circle, rectangle } |
90 | 128 | ||
91 | @immutable | 129 | @immutable |
@@ -94,6 +132,7 @@ class BoxDecoration { | @@ -94,6 +132,7 @@ class BoxDecoration { | ||
94 | {this.color, | 132 | {this.color, |
95 | this.border, | 133 | this.border, |
96 | this.borderRadius, | 134 | this.borderRadius, |
135 | + this.image, | ||
97 | this.shape = BoxShape.rectangle}); | 136 | this.shape = BoxShape.rectangle}); |
98 | 137 | ||
99 | /// The color to fill in the background of the box. | 138 | /// The color to fill in the background of the box. |
@@ -101,6 +140,7 @@ class BoxDecoration { | @@ -101,6 +140,7 @@ class BoxDecoration { | ||
101 | final BoxBorder border; | 140 | final BoxBorder border; |
102 | final double borderRadius; | 141 | final double borderRadius; |
103 | final BoxShape shape; | 142 | final BoxShape shape; |
143 | + final DecorationImage image; | ||
104 | 144 | ||
105 | void paintBackground(Context context, PdfRect box) { | 145 | void paintBackground(Context context, PdfRect box) { |
106 | assert(box.x != null); | 146 | assert(box.x != null); |
@@ -150,11 +190,13 @@ class DecoratedBox extends SingleChildWidget { | @@ -150,11 +190,13 @@ class DecoratedBox extends SingleChildWidget { | ||
150 | super.paint(context); | 190 | super.paint(context); |
151 | if (position == DecorationPosition.background) { | 191 | if (position == DecorationPosition.background) { |
152 | decoration.paintBackground(context, box); | 192 | decoration.paintBackground(context, box); |
193 | + decoration.image?.paintImage(context, box); | ||
153 | decoration.border?.paintBorders(context, box); | 194 | decoration.border?.paintBorders(context, box); |
154 | } | 195 | } |
155 | paintChild(context); | 196 | paintChild(context); |
156 | if (position == DecorationPosition.foreground) { | 197 | if (position == DecorationPosition.foreground) { |
157 | decoration.paintBackground(context, box); | 198 | decoration.paintBackground(context, box); |
199 | + decoration.image?.paintImage(context, box); | ||
158 | decoration.border?.paintBorders(context, box); | 200 | decoration.border?.paintBorders(context, box); |
159 | } | 201 | } |
160 | } | 202 | } |
-
Please register or login to post a comment