OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
6 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
7 | 7 |
8 import 'package:sky/rendering/box.dart'; | 8 import 'package:sky/rendering/box.dart'; |
9 import 'package:sky/rendering/object.dart'; | 9 import 'package:sky/rendering/object.dart'; |
10 import 'package:sky/theme/colors.dart' as colors; | 10 import 'package:sky/theme/colors.dart' as colors; |
(...skipping 17 matching lines...) Expand all Loading... |
28 return colors.Red[_colorIndexForPercentChange(percentChange)]; | 28 return colors.Red[_colorIndexForPercentChange(percentChange)]; |
29 } | 29 } |
30 | 30 |
31 Widget build() { | 31 Widget build() { |
32 // TODO(jackson): This should change colors with the theme | 32 // TODO(jackson): This should change colors with the theme |
33 Color color = _colorForPercentChange(percentChange); | 33 Color color = _colorForPercentChange(percentChange); |
34 const double kSize = 40.0; | 34 const double kSize = 40.0; |
35 var arrow = new CustomPaint(callback: (sky.Canvas canvas, Size size) { | 35 var arrow = new CustomPaint(callback: (sky.Canvas canvas, Size size) { |
36 Paint paint = new Paint()..color = color; | 36 Paint paint = new Paint()..color = color; |
37 paint.strokeWidth = 1.0; | 37 paint.strokeWidth = 1.0; |
38 var padding = paint.strokeWidth * 3.0; | 38 const double padding = 2.0; |
39 var r = kSize / 2.0 - padding; | 39 assert(padding > paint.strokeWidth / 2.0); // make sure the circle remains
inside the box |
40 canvas.save(); | 40 double r = (kSize - padding) / 2.0; // radius of the circle |
41 canvas.translate(padding, padding); | 41 double centerX = padding + r; |
| 42 double centerY = padding + r; |
42 | 43 |
43 // The arrow (below) is drawn upwards by default. | 44 // Draw the arrow. |
| 45 double w = 8.0; |
| 46 double h = 5.0; |
| 47 double arrowY; |
44 if (percentChange < 0.0) { | 48 if (percentChange < 0.0) { |
45 canvas.translate(r, r); | 49 h = -h; |
46 canvas.rotate(math.PI); | 50 arrowY = centerX + 1.0; |
47 canvas.translate(-r, -r); | 51 } else { |
| 52 arrowY = centerX - 1.0; |
48 } | 53 } |
49 | 54 Path path = new Path(); |
50 // Draw the (equliateral triangle) arrow. | 55 path.moveTo(centerX, arrowY - h); // top of the arrow |
51 var dx = math.sqrt(3.0) * r / 2.0; | 56 path.lineTo(centerX + w, arrowY + h); |
52 var path = new Path(); | 57 path.lineTo(centerX - w, arrowY + h); |
53 path.moveTo(r, 0.0); | |
54 path.lineTo(r + dx, r * 1.5); | |
55 path.lineTo(r - dx, r * 1.5); | |
56 path.lineTo(r, 0.0); | |
57 path.close(); | 58 path.close(); |
58 paint.setStyle(sky.PaintingStyle.fill); | 59 paint.setStyle(sky.PaintingStyle.fill); |
59 canvas.drawPath(path, paint); | 60 canvas.drawPath(path, paint); |
60 | 61 |
61 // Draw a circle that circumscribes the arrow. | 62 // Draw a circle that circumscribes the arrow. |
62 paint.setStyle(sky.PaintingStyle.stroke); | 63 paint.setStyle(sky.PaintingStyle.stroke); |
63 canvas.drawCircle(r, r, r + 2.0, paint); | 64 canvas.drawCircle(centerX, centerY, r, paint); |
64 | |
65 canvas.restore(); | |
66 }); | 65 }); |
67 | 66 |
68 return new Container( | 67 return new Container( |
69 child: arrow, | 68 child: arrow, |
70 width: kSize, | 69 width: kSize, |
71 height: kSize, | 70 height: kSize, |
72 margin: const EdgeDims.symmetric(horizontal: 5.0)); | 71 margin: const EdgeDims.symmetric(horizontal: 5.0) |
| 72 ); |
73 } | 73 } |
74 | 74 |
75 } | 75 } |
OLD | NEW |