David PHAM-VAN

Add DecorationSvgImage

@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 ## 2.1.0 3 ## 2.1.0
4 4
5 - Fix SVG fit alignment 5 - Fix SVG fit alignment
  6 +- Add DecorationSvgImage
6 7
7 ## 2.0.0 8 ## 2.0.0
8 9
@@ -31,7 +31,14 @@ import 'widget.dart'; @@ -31,7 +31,14 @@ import 'widget.dart';
31 enum DecorationPosition { background, foreground } 31 enum DecorationPosition { background, foreground }
32 32
33 @immutable 33 @immutable
34 -class DecorationImage { 34 +abstract class DecorationGraphic {
  35 + const DecorationGraphic();
  36 +
  37 + void paint(Context context, PdfRect box);
  38 +}
  39 +
  40 +@immutable
  41 +class DecorationImage extends DecorationGraphic {
35 const DecorationImage({ 42 const DecorationImage({
36 @required this.image, 43 @required this.image,
37 this.fit = BoxFit.cover, 44 this.fit = BoxFit.cover,
@@ -46,6 +53,7 @@ class DecorationImage { @@ -46,6 +53,7 @@ class DecorationImage {
46 final Alignment alignment; 53 final Alignment alignment;
47 final double dpi; 54 final double dpi;
48 55
  56 + @override
49 void paint(Context context, PdfRect box) { 57 void paint(Context context, PdfRect box) {
50 final _image = image.resolve(context, box.size, dpi: dpi); 58 final _image = image.resolve(context, box.size, dpi: dpi);
51 59
@@ -314,7 +322,7 @@ class BoxDecoration { @@ -314,7 +322,7 @@ class BoxDecoration {
314 final BoxBorder border; 322 final BoxBorder border;
315 final BorderRadius borderRadius; 323 final BorderRadius borderRadius;
316 final BoxShape shape; 324 final BoxShape shape;
317 - final DecorationImage image; 325 + final DecorationGraphic image;
318 final Gradient gradient; 326 final Gradient gradient;
319 final List<BoxShadow> boxShadow; 327 final List<BoxShadow> boxShadow;
320 328
@@ -132,3 +132,28 @@ class SvgImage extends Widget { @@ -132,3 +132,28 @@ class SvgImage extends Widget {
132 context.canvas.restoreContext(); 132 context.canvas.restoreContext();
133 } 133 }
134 } 134 }
  135 +
  136 +@immutable
  137 +class DecorationSvgImage extends DecorationGraphic {
  138 + const DecorationSvgImage({
  139 + @required this.svg,
  140 + this.fit = BoxFit.cover,
  141 + this.alignment = Alignment.center,
  142 + }) : assert(svg != null),
  143 + assert(fit != null),
  144 + assert(alignment != null);
  145 +
  146 + final String svg;
  147 + final BoxFit fit;
  148 + final Alignment alignment;
  149 +
  150 + @override
  151 + void paint(Context context, PdfRect box) {
  152 + Widget.draw(
  153 + SvgImage(svg: svg, fit: fit, alignment: alignment),
  154 + offset: box.offset,
  155 + context: context,
  156 + constraints: BoxConstraints.tight(box.size),
  157 + );
  158 + }
  159 +}
@@ -176,6 +176,25 @@ void main() { @@ -176,6 +176,25 @@ void main() {
176 ); 176 );
177 }); 177 });
178 178
  179 + test('SVG Decoration', () {
  180 + const svg =
  181 + '<?xml version="1.0" encoding="UTF-8"?><svg version="1.1" viewBox="10 20 200 200" xmlns="http://www.w3.org/2000/svg"><defs><linearGradient id="c" x1="104.74" x2="124.51" y1="139.75" y2="207.77" gradientUnits="userSpaceOnUse"><stop stop-color="#00b11f" offset="0"/><stop stop-color="#b7feb2" offset="1"/></linearGradient><radialGradient id="b" cx="111.9" cy="-15.337" r="109.52" gradientTransform="matrix(-1.3031 .011643 -.009978 -1.1168 257.57 3.3384)" gradientUnits="userSpaceOnUse"><stop stop-color="#2b005f" offset="0"/><stop stop-color="#000d2f" stop-opacity=".96863" offset="1"/></radialGradient><radialGradient id="a" cx="44.694" cy="60.573" r="18.367" gradientTransform="matrix(1.3554 -.02668 .025662 1.3036 -17.437 -17.229)" gradientUnits="userSpaceOnUse"><stop stop-color="#fff" offset="0"/><stop stop-color="#fff" offset=".26769"/><stop offset="1"/></radialGradient></defs><rect x="1.0204" y="15.918" width="219.05" height="137.96" fill="url(#b)"/><path d="m7.3366 145.63c18.171-6.1454 39.144-16.294 58.552-17.914 9.8733-0.82444 17.181 2.9663 26.609 3.7683 6.6883 0.5689 13.518-1.197 20.165-1.628 6.1933-0.40163 12.496 0.51633 18.68 0 14.232-1.1883 28.257-5.7867 41.947-9.5924 4.7147-1.3106 9.3415-3.1133 14.216-3.7037 9.5303-1.1544 17.923 2.038 26.533 2.038l-1.0187 108.26-206.51-4.6042z" fill="url(#c)"/><ellipse cx="49.864" cy="56.735" rx="18.367" ry="18.231" fill="url(#a)"/></svg> ';
  182 +
  183 + pdf.addPage(
  184 + Page(
  185 + build: (context) => Container(
  186 + decoration: const BoxDecoration(
  187 + color: PdfColors.blue100,
  188 + image: DecorationSvgImage(svg: svg),
  189 + borderRadius: BorderRadius.all(Radius.circular(20)),
  190 + ),
  191 + padding: const EdgeInsets.all(20),
  192 + child: PdfLogo(),
  193 + ),
  194 + ),
  195 + );
  196 + });
  197 +
179 tearDownAll(() async { 198 tearDownAll(() async {
180 final file = File('widgets-svg.pdf'); 199 final file = File('widgets-svg.pdf');
181 await file.writeAsBytes(await pdf.save()); 200 await file.writeAsBytes(await pdf.save());