| 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'; | 5 import 'dart:math' as math; |
| 6 import 'package:sky/framework/fn.dart'; | 6 import 'dart:sky' as sky; |
| 7 import 'package:sky/framework/layout.dart'; | 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'; |
| 8 | 12 |
| 9 class StockArrow extends Component { | 13 class StockArrow extends Component { |
| 10 static final Style _style = new Style(''' | |
| 11 width: 40px; | |
| 12 height: 40px; | |
| 13 align-items: center; | |
| 14 justify-content: center; | |
| 15 border-radius: 40px; | |
| 16 margin-right: 16px; | |
| 17 border: 1px solid transparent;''' | |
| 18 ); | |
| 19 | 14 |
| 20 static final Style _upStyle = new Style(''' | 15 StockArrow({ String key, this.percentChange }) : super(key: key); |
| 21 width: 0; | |
| 22 height: 0; | |
| 23 border-left: 9px solid transparent; | |
| 24 border-right: 9px solid transparent; | |
| 25 margin-bottom: 3px; | |
| 26 border-bottom: 9px solid white;''' | |
| 27 ); | |
| 28 | 16 |
| 29 static final Style _downStyle = new Style(''' | 17 final double percentChange; |
| 30 width: 0; | |
| 31 height: 0; | |
| 32 border-left: 9px solid transparent; | |
| 33 border-right: 9px solid transparent; | |
| 34 margin-top: 3px; | |
| 35 border-top: 9px solid white''' | |
| 36 ); | |
| 37 | |
| 38 double percentChange; | |
| 39 | |
| 40 StockArrow({ Object key, this.percentChange }) : super(key: key); | |
| 41 | |
| 42 // TODO(abarth): These should use sky/framework/theme/colors.dart. | |
| 43 final List<String> _kRedColors = [ | |
| 44 '#E57373', | |
| 45 '#EF5350', | |
| 46 '#F44336', | |
| 47 '#E53935', | |
| 48 '#D32F2F', | |
| 49 '#C62828', | |
| 50 '#B71C1C', | |
| 51 ]; | |
| 52 | |
| 53 // TODO(abarth): These should use sky/framework/theme/colors.dart. | |
| 54 final List<String> _kGreenColors = [ | |
| 55 '#81C784', | |
| 56 '#66BB6A', | |
| 57 '#4CAF50', | |
| 58 '#43A047', | |
| 59 '#388E3C', | |
| 60 '#2E7D32', | |
| 61 '#1B5E20', | |
| 62 ]; | |
| 63 | 18 |
| 64 int _colorIndexForPercentChange(double percentChange) { | 19 int _colorIndexForPercentChange(double percentChange) { |
| 65 // Currently the max is 10%. | |
| 66 double maxPercent = 10.0; | 20 double maxPercent = 10.0; |
| 67 return max(0, ((percentChange.abs() / maxPercent) * _kGreenColors.length).fl
oor()); | 21 double normalizedPercentChange = math.min(percentChange.abs(), maxPercent) /
maxPercent; |
| 22 return 100 + (normalizedPercentChange * 8.0).floor() * 100; |
| 68 } | 23 } |
| 69 | 24 |
| 70 String _colorForPercentChange(double percentChange) { | 25 Color _colorForPercentChange(double percentChange) { |
| 71 if (percentChange > 0) | 26 if (percentChange > 0) |
| 72 return _kGreenColors[_colorIndexForPercentChange(percentChange)]; | 27 return colors.Green[_colorIndexForPercentChange(percentChange)]; |
| 73 return _kRedColors[_colorIndexForPercentChange(percentChange)]; | 28 return colors.Red[_colorIndexForPercentChange(percentChange)]; |
| 74 } | 29 } |
| 75 | 30 |
| 76 UINode build() { | 31 Widget build() { |
| 77 String border = _colorForPercentChange(percentChange).toString(); | 32 // TODO(jackson): This should change colors with the theme |
| 78 bool up = percentChange > 0; | 33 Color color = _colorForPercentChange(percentChange); |
| 79 String type = up ? 'bottom' : 'top'; | 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; |
| 80 | 43 |
| 81 return new FlexContainer( | 44 // Draw the arrow. |
| 82 inlineStyle: 'border-color: $border', | 45 double w = 8.0; |
| 83 direction: FlexDirection.Row, | 46 double h = 5.0; |
| 84 style: _style, | 47 double arrowY; |
| 85 children: [ | 48 if (percentChange < 0.0) { |
| 86 new Container( | 49 h = -h; |
| 87 inlineStyle: 'border-$type-color: $border', | 50 arrowY = centerX + 1.0; |
| 88 style: up ? _upStyle : _downStyle | 51 } else { |
| 89 ) | 52 arrowY = centerX - 1.0; |
| 90 ] | 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(new Point(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) |
| 91 ); | 72 ); |
| 92 } | 73 } |
| 74 |
| 93 } | 75 } |
| OLD | NEW |