David PHAM-VAN

Move BoxDecoration to a new file

@@ -33,6 +33,7 @@ part 'widgets/basic.dart'; @@ -33,6 +33,7 @@ part 'widgets/basic.dart';
33 part 'widgets/clip.dart'; 33 part 'widgets/clip.dart';
34 part 'widgets/container.dart'; 34 part 'widgets/container.dart';
35 part 'widgets/content.dart'; 35 part 'widgets/content.dart';
  36 +part 'widgets/decoration.dart';
36 part 'widgets/document.dart'; 37 part 'widgets/document.dart';
37 part 'widgets/flex.dart'; 38 part 'widgets/flex.dart';
38 part 'widgets/font.dart'; 39 part 'widgets/font.dart';
@@ -18,228 +18,6 @@ @@ -18,228 +18,6 @@
18 18
19 part of widget; 19 part of widget;
20 20
21 -enum DecorationPosition { background, foreground }  
22 -  
23 -@immutable  
24 -class BoxBorder {  
25 - const BoxBorder(  
26 - {this.left = false,  
27 - this.top = false,  
28 - this.right = false,  
29 - this.bottom = false,  
30 - this.color = PdfColors.black,  
31 - this.width = 1.0})  
32 - : assert(color != null),  
33 - assert(width != null),  
34 - assert(width >= 0.0);  
35 -  
36 - final bool top;  
37 - final bool bottom;  
38 - final bool left;  
39 - final bool right;  
40 -  
41 - /// The color of the  
42 - final PdfColor color;  
43 -  
44 - /// The width of the  
45 - final double width;  
46 -  
47 - void paintRect(Context context, PdfRect box) {  
48 - assert(box.x != null);  
49 - assert(box.y != null);  
50 - assert(box.width != null);  
51 - assert(box.height != null);  
52 -  
53 - if (top || bottom || left || right) {  
54 - context.canvas  
55 - ..setStrokeColor(color)  
56 - ..setLineWidth(width);  
57 -  
58 - if (top) {  
59 - context.canvas.drawLine(box.x, box.top, box.right, box.top);  
60 - }  
61 -  
62 - if (right) {  
63 - if (!top) {  
64 - context.canvas.moveTo(box.right, box.top);  
65 - }  
66 - context.canvas.lineTo(box.right, box.y);  
67 - }  
68 -  
69 - if (bottom) {  
70 - if (!right) {  
71 - context.canvas.moveTo(box.right, box.y);  
72 - }  
73 - context.canvas.lineTo(box.x, box.y);  
74 - }  
75 -  
76 - if (left) {  
77 - if (!bottom) {  
78 - context.canvas.moveTo(box.x, box.y);  
79 - context.canvas.lineTo(box.x, box.top);  
80 - } else if (right && top) {  
81 - context.canvas.closePath();  
82 - } else {  
83 - context.canvas.lineTo(box.x, box.top);  
84 - }  
85 - }  
86 -  
87 - context.canvas.strokePath();  
88 - }  
89 - }  
90 -  
91 - void paintEllipse(Context context, PdfRect box) {  
92 - assert(box.x != null);  
93 - assert(box.y != null);  
94 - assert(box.width != null);  
95 - assert(box.height != null);  
96 -  
97 - context.canvas  
98 - ..setStrokeColor(color)  
99 - ..setLineWidth(width)  
100 - ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,  
101 - box.width / 2.0, box.height / 2.0)  
102 - ..strokePath();  
103 - }  
104 -  
105 - void paintRRect(Context context, PdfRect box, double borderRadius) {  
106 - assert(box.x != null);  
107 - assert(box.y != null);  
108 - assert(box.width != null);  
109 - assert(box.height != null);  
110 -  
111 - context.canvas  
112 - ..setStrokeColor(color)  
113 - ..setLineWidth(width)  
114 - ..drawRRect(  
115 - box.x, box.y, box.width, box.height, borderRadius, borderRadius)  
116 - ..strokePath();  
117 - }  
118 -}  
119 -  
120 -@immutable  
121 -class DecorationImage {  
122 - const DecorationImage(  
123 - {@required this.image,  
124 - this.fit = BoxFit.cover,  
125 - this.alignment = Alignment.center})  
126 - : assert(image != null),  
127 - assert(fit != null),  
128 - assert(alignment != null);  
129 -  
130 - final PdfImage image;  
131 - final BoxFit fit;  
132 - final Alignment alignment;  
133 -  
134 - void paint(Context context, PdfRect box) {  
135 - final PdfPoint imageSize =  
136 - PdfPoint(image.width.toDouble(), image.height.toDouble());  
137 - final FittedSizes sizes = applyBoxFit(fit, imageSize, box.size);  
138 - final double scaleX = sizes.destination.x / sizes.source.x;  
139 - final double scaleY = sizes.destination.y / sizes.source.y;  
140 - final PdfRect sourceRect = alignment.inscribe(  
141 - sizes.source, PdfRect.fromPoints(PdfPoint.zero, imageSize));  
142 - final PdfRect destinationRect = alignment.inscribe(sizes.destination, box);  
143 - final Matrix4 mat =  
144 - Matrix4.translationValues(destinationRect.x, destinationRect.y, 0)  
145 - ..scale(scaleX, scaleY, 1)  
146 - ..translate(-sourceRect.x, -sourceRect.y);  
147 -  
148 - context.canvas  
149 - ..saveContext()  
150 - ..drawRect(box.x, box.y, box.width, box.height)  
151 - ..clipPath()  
152 - ..setTransform(mat)  
153 - ..drawImage(image, 0, 0, imageSize.x, imageSize.y)  
154 - ..restoreContext();  
155 - }  
156 -}  
157 -  
158 -enum BoxShape { circle, rectangle }  
159 -  
160 -@immutable  
161 -class BoxDecoration {  
162 - const BoxDecoration(  
163 - {this.color,  
164 - this.border,  
165 - this.borderRadius,  
166 - this.image,  
167 - this.shape = BoxShape.rectangle})  
168 - : assert(shape != null);  
169 -  
170 - /// The color to fill in the background of the box.  
171 - final PdfColor color;  
172 - final BoxBorder border;  
173 - final double borderRadius;  
174 - final BoxShape shape;  
175 - final DecorationImage image;  
176 -  
177 - void paint(Context context, PdfRect box) {  
178 - assert(box.x != null);  
179 - assert(box.y != null);  
180 - assert(box.width != null);  
181 - assert(box.height != null);  
182 -  
183 - if (color != null) {  
184 - switch (shape) {  
185 - case BoxShape.rectangle:  
186 - if (borderRadius == null) {  
187 - context.canvas.drawRect(box.x, box.y, box.width, box.height);  
188 - } else {  
189 - context.canvas.drawRRect(box.x, box.y, box.width, box.height,  
190 - borderRadius, borderRadius);  
191 - }  
192 - break;  
193 - case BoxShape.circle:  
194 - context.canvas.drawEllipse(box.x + box.width / 2.0,  
195 - box.y + box.height / 2.0, box.width / 2.0, box.height / 2.0);  
196 - break;  
197 - }  
198 - context.canvas  
199 - ..setFillColor(color)  
200 - ..fillPath();  
201 - }  
202 -  
203 - if (image != null) {  
204 - context.canvas.saveContext();  
205 - switch (shape) {  
206 - case BoxShape.circle:  
207 - context.canvas  
208 - ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,  
209 - box.width / 2.0, box.height / 2.0)  
210 - ..clipPath();  
211 -  
212 - break;  
213 - case BoxShape.rectangle:  
214 - if (borderRadius != null) {  
215 - context.canvas  
216 - ..drawRRect(box.x, box.y, box.width, box.height, borderRadius,  
217 - borderRadius)  
218 - ..clipPath();  
219 - }  
220 - break;  
221 - }  
222 - image.paint(context, box);  
223 - context.canvas.restoreContext();  
224 - }  
225 -  
226 - if (border != null) {  
227 - switch (shape) {  
228 - case BoxShape.circle:  
229 - border.paintEllipse(context, box);  
230 - break;  
231 - case BoxShape.rectangle:  
232 - if (borderRadius != null) {  
233 - border.paintRRect(context, box, borderRadius);  
234 - } else {  
235 - border.paintRect(context, box);  
236 - }  
237 - break;  
238 - }  
239 - }  
240 - }  
241 -}  
242 -  
243 class DecoratedBox extends SingleChildWidget { 21 class DecoratedBox extends SingleChildWidget {
244 DecoratedBox( 22 DecoratedBox(
245 {@required this.decoration, 23 {@required this.decoration,
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +// ignore_for_file: omit_local_variable_types
  18 +
  19 +part of widget;
  20 +
  21 +enum DecorationPosition { background, foreground }
  22 +
  23 +@immutable
  24 +class BoxBorder {
  25 + const BoxBorder(
  26 + {this.left = false,
  27 + this.top = false,
  28 + this.right = false,
  29 + this.bottom = false,
  30 + this.color = PdfColors.black,
  31 + this.width = 1.0})
  32 + : assert(color != null),
  33 + assert(width != null),
  34 + assert(width >= 0.0);
  35 +
  36 + final bool top;
  37 + final bool bottom;
  38 + final bool left;
  39 + final bool right;
  40 +
  41 + /// The color of the
  42 + final PdfColor color;
  43 +
  44 + /// The width of the
  45 + final double width;
  46 +
  47 + void paintRect(Context context, PdfRect box) {
  48 + assert(box.x != null);
  49 + assert(box.y != null);
  50 + assert(box.width != null);
  51 + assert(box.height != null);
  52 +
  53 + if (top || bottom || left || right) {
  54 + context.canvas
  55 + ..setStrokeColor(color)
  56 + ..setLineWidth(width);
  57 +
  58 + if (top) {
  59 + context.canvas.drawLine(box.x, box.top, box.right, box.top);
  60 + }
  61 +
  62 + if (right) {
  63 + if (!top) {
  64 + context.canvas.moveTo(box.right, box.top);
  65 + }
  66 + context.canvas.lineTo(box.right, box.y);
  67 + }
  68 +
  69 + if (bottom) {
  70 + if (!right) {
  71 + context.canvas.moveTo(box.right, box.y);
  72 + }
  73 + context.canvas.lineTo(box.x, box.y);
  74 + }
  75 +
  76 + if (left) {
  77 + if (!bottom) {
  78 + context.canvas.moveTo(box.x, box.y);
  79 + context.canvas.lineTo(box.x, box.top);
  80 + } else if (right && top) {
  81 + context.canvas.closePath();
  82 + } else {
  83 + context.canvas.lineTo(box.x, box.top);
  84 + }
  85 + }
  86 +
  87 + context.canvas.strokePath();
  88 + }
  89 + }
  90 +
  91 + void paintEllipse(Context context, PdfRect box) {
  92 + assert(box.x != null);
  93 + assert(box.y != null);
  94 + assert(box.width != null);
  95 + assert(box.height != null);
  96 +
  97 + context.canvas
  98 + ..setStrokeColor(color)
  99 + ..setLineWidth(width)
  100 + ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,
  101 + box.width / 2.0, box.height / 2.0)
  102 + ..strokePath();
  103 + }
  104 +
  105 + void paintRRect(Context context, PdfRect box, double borderRadius) {
  106 + assert(box.x != null);
  107 + assert(box.y != null);
  108 + assert(box.width != null);
  109 + assert(box.height != null);
  110 +
  111 + context.canvas
  112 + ..setStrokeColor(color)
  113 + ..setLineWidth(width)
  114 + ..drawRRect(
  115 + box.x, box.y, box.width, box.height, borderRadius, borderRadius)
  116 + ..strokePath();
  117 + }
  118 +}
  119 +
  120 +@immutable
  121 +class DecorationImage {
  122 + const DecorationImage(
  123 + {@required this.image,
  124 + this.fit = BoxFit.cover,
  125 + this.alignment = Alignment.center})
  126 + : assert(image != null),
  127 + assert(fit != null),
  128 + assert(alignment != null);
  129 +
  130 + final PdfImage image;
  131 + final BoxFit fit;
  132 + final Alignment alignment;
  133 +
  134 + void paint(Context context, PdfRect box) {
  135 + final PdfPoint imageSize =
  136 + PdfPoint(image.width.toDouble(), image.height.toDouble());
  137 + final FittedSizes sizes = applyBoxFit(fit, imageSize, box.size);
  138 + final double scaleX = sizes.destination.x / sizes.source.x;
  139 + final double scaleY = sizes.destination.y / sizes.source.y;
  140 + final PdfRect sourceRect = alignment.inscribe(
  141 + sizes.source, PdfRect.fromPoints(PdfPoint.zero, imageSize));
  142 + final PdfRect destinationRect = alignment.inscribe(sizes.destination, box);
  143 + final Matrix4 mat =
  144 + Matrix4.translationValues(destinationRect.x, destinationRect.y, 0)
  145 + ..scale(scaleX, scaleY, 1)
  146 + ..translate(-sourceRect.x, -sourceRect.y);
  147 +
  148 + context.canvas
  149 + ..saveContext()
  150 + ..drawRect(box.x, box.y, box.width, box.height)
  151 + ..clipPath()
  152 + ..setTransform(mat)
  153 + ..drawImage(image, 0, 0, imageSize.x, imageSize.y)
  154 + ..restoreContext();
  155 + }
  156 +}
  157 +
  158 +enum BoxShape { circle, rectangle }
  159 +
  160 +@immutable
  161 +class BoxDecoration {
  162 + const BoxDecoration(
  163 + {this.color,
  164 + this.border,
  165 + this.borderRadius,
  166 + this.image,
  167 + this.shape = BoxShape.rectangle})
  168 + : assert(shape != null);
  169 +
  170 + /// The color to fill in the background of the box.
  171 + final PdfColor color;
  172 + final BoxBorder border;
  173 + final double borderRadius;
  174 + final BoxShape shape;
  175 + final DecorationImage image;
  176 +
  177 + void paint(Context context, PdfRect box) {
  178 + assert(box.x != null);
  179 + assert(box.y != null);
  180 + assert(box.width != null);
  181 + assert(box.height != null);
  182 +
  183 + if (color != null) {
  184 + switch (shape) {
  185 + case BoxShape.rectangle:
  186 + if (borderRadius == null) {
  187 + context.canvas.drawRect(box.x, box.y, box.width, box.height);
  188 + } else {
  189 + context.canvas.drawRRect(box.x, box.y, box.width, box.height,
  190 + borderRadius, borderRadius);
  191 + }
  192 + break;
  193 + case BoxShape.circle:
  194 + context.canvas.drawEllipse(box.x + box.width / 2.0,
  195 + box.y + box.height / 2.0, box.width / 2.0, box.height / 2.0);
  196 + break;
  197 + }
  198 + context.canvas
  199 + ..setFillColor(color)
  200 + ..fillPath();
  201 + }
  202 +
  203 + if (image != null) {
  204 + context.canvas.saveContext();
  205 + switch (shape) {
  206 + case BoxShape.circle:
  207 + context.canvas
  208 + ..drawEllipse(box.x + box.width / 2.0, box.y + box.height / 2.0,
  209 + box.width / 2.0, box.height / 2.0)
  210 + ..clipPath();
  211 +
  212 + break;
  213 + case BoxShape.rectangle:
  214 + if (borderRadius != null) {
  215 + context.canvas
  216 + ..drawRRect(box.x, box.y, box.width, box.height, borderRadius,
  217 + borderRadius)
  218 + ..clipPath();
  219 + }
  220 + break;
  221 + }
  222 + image.paint(context, box);
  223 + context.canvas.restoreContext();
  224 + }
  225 +
  226 + if (border != null) {
  227 + switch (shape) {
  228 + case BoxShape.circle:
  229 + border.paintEllipse(context, box);
  230 + break;
  231 + case BoxShape.rectangle:
  232 + if (borderRadius != null) {
  233 + border.paintRRect(context, box, borderRadius);
  234 + } else {
  235 + border.paintRect(context, box);
  236 + }
  237 + break;
  238 + }
  239 + }
  240 + }
  241 +}