OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:math' as math; | |
6 import 'dart:sky' as sky; | |
7 | |
8 import 'package:sky/rendering/box.dart'; | |
9 import 'package:sky/rendering/object.dart'; | |
10 import 'package:sky/theme/colors.dart' as colors; | |
11 import 'package:sky/widgets/basic.dart'; | |
12 | |
13 class StockArrow extends Component { | |
14 | |
15 StockArrow({ String key, this.percentChange }) : super(key: key); | |
16 | |
17 final double percentChange; | |
18 | |
19 int _colorIndexForPercentChange(double percentChange) { | |
20 double maxPercent = 10.0; | |
21 double normalizedPercentChange = math.min(percentChange.abs(), maxPercent) /
maxPercent; | |
22 return 100 + (normalizedPercentChange * 8.0).floor() * 100; | |
23 } | |
24 | |
25 Color _colorForPercentChange(double percentChange) { | |
26 if (percentChange > 0) | |
27 return colors.Green[_colorIndexForPercentChange(percentChange)]; | |
28 return colors.Red[_colorIndexForPercentChange(percentChange)]; | |
29 } | |
30 | |
31 Widget build() { | |
32 // TODO(jackson): This should change colors with the theme | |
33 Color color = _colorForPercentChange(percentChange); | |
34 const double kSize = 40.0; | |
35 var arrow = new CustomPaint(callback: (sky.Canvas canvas, Size size) { | |
36 Paint paint = new Paint()..color = color; | |
37 paint.strokeWidth = 1.0; | |
38 const double padding = 2.0; | |
39 assert(padding > paint.strokeWidth / 2.0); // make sure the circle remains
inside the box | |
40 double r = (kSize - padding) / 2.0; // radius of the circle | |
41 double centerX = padding + r; | |
42 double centerY = padding + r; | |
43 | |
44 // Draw the arrow. | |
45 double w = 8.0; | |
46 double h = 5.0; | |
47 double arrowY; | |
48 if (percentChange < 0.0) { | |
49 h = -h; | |
50 arrowY = centerX + 1.0; | |
51 } else { | |
52 arrowY = centerX - 1.0; | |
53 } | |
54 Path path = new Path(); | |
55 path.moveTo(centerX, arrowY - h); // top of the arrow | |
56 path.lineTo(centerX + w, arrowY + h); | |
57 path.lineTo(centerX - w, arrowY + h); | |
58 path.close(); | |
59 paint.setStyle(sky.PaintingStyle.fill); | |
60 canvas.drawPath(path, paint); | |
61 | |
62 // Draw a circle that circumscribes the arrow. | |
63 paint.setStyle(sky.PaintingStyle.stroke); | |
64 canvas.drawCircle(centerX, centerY, r, paint); | |
65 }); | |
66 | |
67 return new Container( | |
68 child: arrow, | |
69 width: kSize, | |
70 height: kSize, | |
71 margin: const EdgeDims.symmetric(horizontal: 5.0) | |
72 ); | |
73 } | |
74 | |
75 } | |
OLD | NEW |