David PHAM-VAN

Add dashed lines to Decoration Widgets

@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 4
5 - Implement Linear and Radial gradients in BoxDecoration 5 - Implement Linear and Radial gradients in BoxDecoration
6 - Fix PdfColors.shade() 6 - Fix PdfColors.shade()
  7 +- Add dashed lines to Decoration Widgets
7 8
8 ## 1.6.2 9 ## 1.6.2
9 10
@@ -700,4 +700,9 @@ class PdfGraphics { @@ -700,4 +700,9 @@ class PdfGraphics {
700 PdfNum(limit).output(buf); 700 PdfNum(limit).output(buf);
701 buf.putString(' M\n'); 701 buf.putString(' M\n');
702 } 702 }
  703 +
  704 + void setLineDashPattern([List<int> array = const <int>[], int phase = 0]) {
  705 + PdfArray.fromNum(array).output(buf);
  706 + buf.putString(' $phase d\n');
  707 + }
703 } 708 }
@@ -20,24 +20,30 @@ part of widget; @@ -20,24 +20,30 @@ part of widget;
20 20
21 enum DecorationPosition { background, foreground } 21 enum DecorationPosition { background, foreground }
22 22
  23 +enum BorderStyle { none, solid, dashed, dotted }
  24 +
23 @immutable 25 @immutable
24 class BoxBorder { 26 class BoxBorder {
25 - const BoxBorder(  
26 - {this.left = false, 27 + const BoxBorder({
  28 + this.left = false,
27 this.top = false, 29 this.top = false,
28 this.right = false, 30 this.right = false,
29 this.bottom = false, 31 this.bottom = false,
30 this.color = PdfColors.black, 32 this.color = PdfColors.black,
31 - this.width = 1.0})  
32 - : assert(color != null), 33 + this.width = 1.0,
  34 + this.style = BorderStyle.solid,
  35 + }) : assert(color != null),
33 assert(width != null), 36 assert(width != null),
34 - assert(width >= 0.0); 37 + assert(width >= 0.0),
  38 + assert(style != null);
35 39
36 final bool top; 40 final bool top;
37 final bool bottom; 41 final bool bottom;
38 final bool left; 42 final bool left;
39 final bool right; 43 final bool right;
40 44
  45 + final BorderStyle style;
  46 +
41 /// The color of the 47 /// The color of the
42 final PdfColor color; 48 final PdfColor color;
43 49
@@ -50,7 +56,27 @@ class BoxBorder { @@ -50,7 +56,27 @@ class BoxBorder {
50 assert(box.width != null); 56 assert(box.width != null);
51 assert(box.height != null); 57 assert(box.height != null);
52 58
53 - if (top || bottom || left || right) { 59 + if (!(top || bottom || left || right)) {
  60 + return;
  61 + }
  62 +
  63 + switch (style) {
  64 + case BorderStyle.none:
  65 + return;
  66 + case BorderStyle.solid:
  67 + break;
  68 + case BorderStyle.dashed:
  69 + context.canvas
  70 + ..saveContext()
  71 + ..setLineDashPattern(const <int>[3, 3]);
  72 + break;
  73 + case BorderStyle.dotted:
  74 + context.canvas
  75 + ..saveContext()
  76 + ..setLineDashPattern(const <int>[1, 1]);
  77 + break;
  78 + }
  79 +
54 context.canvas 80 context.canvas
55 ..setStrokeColor(color) 81 ..setStrokeColor(color)
56 ..setLineWidth(width); 82 ..setLineWidth(width);
@@ -85,6 +111,8 @@ class BoxBorder { @@ -85,6 +111,8 @@ class BoxBorder {
85 } 111 }
86 112
87 context.canvas.strokePath(); 113 context.canvas.strokePath();
  114 + if (style != BorderStyle.solid) {
  115 + context.canvas.restoreContext();
88 } 116 }
89 } 117 }
90 118
@@ -349,6 +377,8 @@ class RadialGradient extends Gradient { @@ -349,6 +377,8 @@ class RadialGradient extends Gradient {
349 377
350 enum BoxShape { circle, rectangle } 378 enum BoxShape { circle, rectangle }
351 379
  380 +enum PaintPhase { all, background, foreground }
  381 +
352 @immutable 382 @immutable
353 class BoxDecoration { 383 class BoxDecoration {
354 const BoxDecoration( 384 const BoxDecoration(
@@ -368,12 +398,17 @@ class BoxDecoration { @@ -368,12 +398,17 @@ class BoxDecoration {
368 final DecorationImage image; 398 final DecorationImage image;
369 final Gradient gradient; 399 final Gradient gradient;
370 400
371 - void paint(Context context, PdfRect box) { 401 + void paint(
  402 + Context context,
  403 + PdfRect box, [
  404 + PaintPhase phase = PaintPhase.all,
  405 + ]) {
372 assert(box.x != null); 406 assert(box.x != null);
373 assert(box.y != null); 407 assert(box.y != null);
374 assert(box.width != null); 408 assert(box.width != null);
375 assert(box.height != null); 409 assert(box.height != null);
376 410
  411 + if (phase == PaintPhase.all || phase == PaintPhase.background) {
377 if (color != null) { 412 if (color != null) {
378 switch (shape) { 413 switch (shape) {
379 case BoxShape.rectangle: 414 case BoxShape.rectangle:
@@ -435,7 +470,9 @@ class BoxDecoration { @@ -435,7 +470,9 @@ class BoxDecoration {
435 image.paint(context, box); 470 image.paint(context, box);
436 context.canvas.restoreContext(); 471 context.canvas.restoreContext();
437 } 472 }
  473 + }
438 474
  475 + if (phase == PaintPhase.all || phase == PaintPhase.foreground) {
439 if (border != null) { 476 if (border != null) {
440 switch (shape) { 477 switch (shape) {
441 case BoxShape.circle: 478 case BoxShape.circle:
@@ -451,4 +488,5 @@ class BoxDecoration { @@ -451,4 +488,5 @@ class BoxDecoration {
451 } 488 }
452 } 489 }
453 } 490 }
  491 + }
454 } 492 }