David PHAM-VAN

Convert BorderStyle to a class

@@ -3,6 +3,8 @@ @@ -3,6 +3,8 @@
3 ## 3.0.0-nullsafety.1 3 ## 3.0.0-nullsafety.1
4 4
5 - Fix Table border 5 - Fix Table border
  6 +- Convert BorderStyle to a class
  7 +- Implement dashed Divider
6 8
7 ## 3.0.0-nullsafety.0 9 ## 3.0.0-nullsafety.0
8 10
@@ -21,7 +21,43 @@ import 'border_radius.dart'; @@ -21,7 +21,43 @@ import 'border_radius.dart';
21 import 'decoration.dart'; 21 import 'decoration.dart';
22 import 'widget.dart'; 22 import 'widget.dart';
23 23
24 -enum BorderStyle { none, solid, dashed, dotted } 24 +class BorderStyle {
  25 + const BorderStyle({
  26 + this.paint = true,
  27 + this.pattern,
  28 + this.phase = 0,
  29 + });
  30 +
  31 + static const none = BorderStyle(paint: false);
  32 + static const solid = BorderStyle();
  33 + static const dashed = BorderStyle(pattern: <int>[3, 3]);
  34 + static const dotted = BorderStyle(pattern: <int>[1, 1]);
  35 +
  36 + /// Paint this line
  37 + final bool paint;
  38 +
  39 + /// Lengths of alternating dashes and gaps. The numbers shall be nonnegative
  40 + /// and not all zero.
  41 + final List<num>? pattern;
  42 +
  43 + /// Specify the distance into the dash pattern at which to start the dash.
  44 + final int phase;
  45 +
  46 + void setStyle(Context context) {
  47 + if (paint && pattern != null) {
  48 + context.canvas
  49 + ..saveContext()
  50 + ..setLineCap(PdfLineCap.butt)
  51 + ..setLineDashPattern(pattern!, phase);
  52 + }
  53 + }
  54 +
  55 + void unsetStyle(Context context) {
  56 + if (paint && pattern != null) {
  57 + context.canvas.restoreContext();
  58 + }
  59 + }
  60 +}
25 61
26 @immutable 62 @immutable
27 abstract class BoxBorder { 63 abstract class BoxBorder {
@@ -41,51 +77,21 @@ abstract class BoxBorder { @@ -41,51 +77,21 @@ abstract class BoxBorder {
41 BorderRadius? borderRadius, 77 BorderRadius? borderRadius,
42 }); 78 });
43 79
44 - static void setStyle(Context context, BorderStyle style) {  
45 - switch (style) {  
46 - case BorderStyle.none:  
47 - case BorderStyle.solid:  
48 - break;  
49 - case BorderStyle.dashed:  
50 - context.canvas  
51 - ..saveContext()  
52 - ..setLineDashPattern(const <int>[3, 3]);  
53 - break;  
54 - case BorderStyle.dotted:  
55 - context.canvas  
56 - ..saveContext()  
57 - ..setLineDashPattern(const <int>[1, 1]);  
58 - break;  
59 - }  
60 - }  
61 -  
62 - static void unsetStyle(Context context, BorderStyle style) {  
63 - switch (style) {  
64 - case BorderStyle.none:  
65 - case BorderStyle.solid:  
66 - break;  
67 - case BorderStyle.dashed:  
68 - case BorderStyle.dotted:  
69 - context.canvas.restoreContext();  
70 - break;  
71 - }  
72 - }  
73 -  
74 static void _paintUniformBorderWithCircle( 80 static void _paintUniformBorderWithCircle(
75 Context context, PdfRect box, BorderSide side) { 81 Context context, PdfRect box, BorderSide side) {
76 - setStyle(context, side.style); 82 + side.style.setStyle(context);
77 context.canvas 83 context.canvas
78 ..setStrokeColor(side.color) 84 ..setStrokeColor(side.color)
79 ..setLineWidth(side.width) 85 ..setLineWidth(side.width)
80 ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0, 86 ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,
81 box.width / 2.0, box.height / 2.0) 87 box.width / 2.0, box.height / 2.0)
82 ..strokePath(); 88 ..strokePath();
83 - unsetStyle(context, side.style); 89 + side.style.unsetStyle(context);
84 } 90 }
85 91
86 static void _paintUniformBorderWithRadius(Context context, PdfRect box, 92 static void _paintUniformBorderWithRadius(Context context, PdfRect box,
87 BorderSide side, BorderRadius borderRadius) { 93 BorderSide side, BorderRadius borderRadius) {
88 - setStyle(context, side.style); 94 + side.style.setStyle(context);
89 context.canvas 95 context.canvas
90 ..setLineJoin(PdfLineJoin.miter) 96 ..setLineJoin(PdfLineJoin.miter)
91 ..setMiterLimit(4) 97 ..setMiterLimit(4)
@@ -93,12 +99,12 @@ abstract class BoxBorder { @@ -93,12 +99,12 @@ abstract class BoxBorder {
93 ..setLineWidth(side.width); 99 ..setLineWidth(side.width);
94 borderRadius.paint(context, box); 100 borderRadius.paint(context, box);
95 context.canvas.strokePath(); 101 context.canvas.strokePath();
96 - unsetStyle(context, side.style); 102 + side.style.unsetStyle(context);
97 } 103 }
98 104
99 static void _paintUniformBorderWithRectangle( 105 static void _paintUniformBorderWithRectangle(
100 Context context, PdfRect box, BorderSide side) { 106 Context context, PdfRect box, BorderSide side) {
101 - setStyle(context, side.style); 107 + side.style.setStyle(context);
102 context.canvas 108 context.canvas
103 ..setLineJoin(PdfLineJoin.miter) 109 ..setLineJoin(PdfLineJoin.miter)
104 ..setMiterLimit(4) 110 ..setMiterLimit(4)
@@ -106,7 +112,7 @@ abstract class BoxBorder { @@ -106,7 +112,7 @@ abstract class BoxBorder {
106 ..setLineWidth(side.width) 112 ..setLineWidth(side.width)
107 ..drawBox(box) 113 ..drawBox(box)
108 ..strokePath(); 114 ..strokePath();
109 - unsetStyle(context, side.style); 115 + side.style.unsetStyle(context);
110 } 116 }
111 } 117 }
112 118
@@ -251,44 +257,44 @@ class Border extends BoxBorder { @@ -251,44 +257,44 @@ class Border extends BoxBorder {
251 ..setMiterLimit(4) 257 ..setMiterLimit(4)
252 ..setLineJoin(PdfLineJoin.miter); 258 ..setLineJoin(PdfLineJoin.miter);
253 259
254 - if (top.style != BorderStyle.none) {  
255 - BoxBorder.setStyle(context, top.style); 260 + if (top.style.paint) {
  261 + top.style.setStyle(context);
256 context.canvas 262 context.canvas
257 ..setStrokeColor(top.color) 263 ..setStrokeColor(top.color)
258 ..setLineWidth(top.width) 264 ..setLineWidth(top.width)
259 ..drawLine(box.left, box.top, box.right, box.top) 265 ..drawLine(box.left, box.top, box.right, box.top)
260 ..strokePath(); 266 ..strokePath();
261 - BoxBorder.unsetStyle(context, top.style); 267 + top.style.unsetStyle(context);
262 } 268 }
263 269
264 - if (right.style != BorderStyle.none) {  
265 - BoxBorder.setStyle(context, right.style); 270 + if (right.style.paint) {
  271 + right.style.setStyle(context);
266 context.canvas 272 context.canvas
267 ..setStrokeColor(right.color) 273 ..setStrokeColor(right.color)
268 ..setLineWidth(right.width) 274 ..setLineWidth(right.width)
269 ..drawLine(box.right, box.top, box.right, box.bottom) 275 ..drawLine(box.right, box.top, box.right, box.bottom)
270 ..strokePath(); 276 ..strokePath();
271 - BoxBorder.unsetStyle(context, right.style); 277 + right.style.unsetStyle(context);
272 } 278 }
273 279
274 - if (bottom.style != BorderStyle.none) {  
275 - BoxBorder.setStyle(context, bottom.style); 280 + if (bottom.style.paint) {
  281 + bottom.style.setStyle(context);
276 context.canvas 282 context.canvas
277 ..setStrokeColor(bottom.color) 283 ..setStrokeColor(bottom.color)
278 ..setLineWidth(bottom.width) 284 ..setLineWidth(bottom.width)
279 ..drawLine(box.right, box.bottom, box.left, box.bottom) 285 ..drawLine(box.right, box.bottom, box.left, box.bottom)
280 ..strokePath(); 286 ..strokePath();
281 - BoxBorder.unsetStyle(context, bottom.style); 287 + bottom.style.unsetStyle(context);
282 } 288 }
283 289
284 - if (left.style != BorderStyle.none) {  
285 - BoxBorder.setStyle(context, left.style); 290 + if (left.style.paint) {
  291 + left.style.setStyle(context);
286 context.canvas 292 context.canvas
287 ..setStrokeColor(left.color) 293 ..setStrokeColor(left.color)
288 ..setLineWidth(left.width) 294 ..setLineWidth(left.width)
289 ..drawLine(box.left, box.top, box.left, box.bottom) 295 ..drawLine(box.left, box.top, box.left, box.bottom)
290 ..strokePath(); 296 ..strokePath();
291 - BoxBorder.unsetStyle(context, left.style); 297 + left.style.unsetStyle(context);
292 } 298 }
293 } 299 }
294 } 300 }
@@ -270,45 +270,45 @@ class GridPaper extends SingleChildWidget { @@ -270,45 +270,45 @@ class GridPaper extends SingleChildWidget {
270 n++; 270 n++;
271 } 271 }
272 272
273 - if (border.left.style != BorderStyle.none) {  
274 - BoxBorder.setStyle(context, border.left.style); 273 + if (border.left.style.paint) {
  274 + border.left.style.setStyle(context);
275 context.canvas 275 context.canvas
276 ..setStrokeColor(border.left.color) 276 ..setStrokeColor(border.left.color)
277 ..setLineWidth(border.left.width) 277 ..setLineWidth(border.left.width)
278 ..drawLine(box!.left + margin.left, box!.top, box!.left + margin.left, 278 ..drawLine(box!.left + margin.left, box!.top, box!.left + margin.left,
279 box!.bottom) 279 box!.bottom)
280 ..strokePath(); 280 ..strokePath();
281 - BoxBorder.unsetStyle(context, border.left.style); 281 + border.left.style.unsetStyle(context);
282 } 282 }
283 - if (border.right.style != BorderStyle.none) {  
284 - BoxBorder.setStyle(context, border.right.style); 283 + if (border.right.style.paint) {
  284 + border.right.style.setStyle(context);
285 context.canvas 285 context.canvas
286 ..setStrokeColor(border.right.color) 286 ..setStrokeColor(border.right.color)
287 ..setLineWidth(border.right.width) 287 ..setLineWidth(border.right.width)
288 ..drawLine(box!.right - margin.right, box!.top, 288 ..drawLine(box!.right - margin.right, box!.top,
289 box!.right - margin.right, box!.bottom) 289 box!.right - margin.right, box!.bottom)
290 ..strokePath(); 290 ..strokePath();
291 - BoxBorder.unsetStyle(context, border.right.style); 291 + border.right.style.unsetStyle(context);
292 } 292 }
293 - if (border.top.style != BorderStyle.none) {  
294 - BoxBorder.setStyle(context, border.top.style); 293 + if (border.top.style.paint) {
  294 + border.top.style.setStyle(context);
295 context.canvas 295 context.canvas
296 ..setStrokeColor(border.top.color) 296 ..setStrokeColor(border.top.color)
297 ..setLineWidth(border.top.width) 297 ..setLineWidth(border.top.width)
298 ..drawLine( 298 ..drawLine(
299 box!.left, box!.top - margin.top, box!.right, box!.top - margin.top) 299 box!.left, box!.top - margin.top, box!.right, box!.top - margin.top)
300 ..strokePath(); 300 ..strokePath();
301 - BoxBorder.unsetStyle(context, border.top.style); 301 + border.top.style.unsetStyle(context);
302 } 302 }
303 - if (border.bottom.style != BorderStyle.none) {  
304 - BoxBorder.setStyle(context, border.bottom.style); 303 + if (border.bottom.style.paint) {
  304 + border.bottom.style.setStyle(context);
305 context.canvas 305 context.canvas
306 ..setStrokeColor(border.bottom.color) 306 ..setStrokeColor(border.bottom.color)
307 ..setLineWidth(border.bottom.width) 307 ..setLineWidth(border.bottom.width)
308 ..drawLine(box!.left, box!.bottom + margin.bottom, box!.right, 308 ..drawLine(box!.left, box!.bottom + margin.bottom, box!.right,
309 box!.bottom + margin.bottom) 309 box!.bottom + margin.bottom)
310 ..strokePath(); 310 ..strokePath();
311 - BoxBorder.unsetStyle(context, border.bottom.style); 311 + border.bottom.style.unsetStyle(context);
312 } 312 }
313 313
314 context.canvas.restoreContext(); 314 context.canvas.restoreContext();
@@ -105,8 +105,8 @@ class TableBorder extends Border { @@ -105,8 +105,8 @@ class TableBorder extends Border {
105 [List<double?>? widths, List<double>? heights]) { 105 [List<double?>? widths, List<double>? heights]) {
106 super.paint(context, box); 106 super.paint(context, box);
107 107
108 - if (verticalInside.style != BorderStyle.none) {  
109 - BoxBorder.setStyle(context, verticalInside.style); 108 + if (verticalInside.style.paint) {
  109 + verticalInside.style.setStyle(context);
110 var offset = box.x; 110 var offset = box.x;
111 for (var width in widths!.sublist(0, widths.length - 1)) { 111 for (var width in widths!.sublist(0, widths.length - 1)) {
112 offset += width!; 112 offset += width!;
@@ -117,11 +117,11 @@ class TableBorder extends Border { @@ -117,11 +117,11 @@ class TableBorder extends Border {
117 context.canvas.setLineWidth(verticalInside.width); 117 context.canvas.setLineWidth(verticalInside.width);
118 context.canvas.strokePath(); 118 context.canvas.strokePath();
119 119
120 - BoxBorder.unsetStyle(context, verticalInside.style); 120 + verticalInside.style.unsetStyle(context);
121 } 121 }
122 122
123 - if (horizontalInside.style != BorderStyle.none) {  
124 - BoxBorder.setStyle(context, verticalInside.style); 123 + if (horizontalInside.style.paint) {
  124 + horizontalInside.style.setStyle(context);
125 var offset = box.top; 125 var offset = box.top;
126 for (var height in heights!.sublist(0, heights.length - 1)) { 126 for (var height in heights!.sublist(0, heights.length - 1)) {
127 offset -= height; 127 offset -= height;
@@ -131,7 +131,7 @@ class TableBorder extends Border { @@ -131,7 +131,7 @@ class TableBorder extends Border {
131 context.canvas.setStrokeColor(verticalInside.color); 131 context.canvas.setStrokeColor(verticalInside.color);
132 context.canvas.setLineWidth(verticalInside.width); 132 context.canvas.setLineWidth(verticalInside.width);
133 context.canvas.strokePath(); 133 context.canvas.strokePath();
134 - BoxBorder.unsetStyle(context, verticalInside.style); 134 + horizontalInside.style.unsetStyle(context);
135 } 135 }
136 } 136 }
137 } 137 }