Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 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'; |
| 6 | import 'package:sky/framework/fn.dart'; |
Hixie | 4c15a96 | 2015-05-15 15:16:25 -0700 | [diff] [blame] | 7 | import 'package:sky/framework/layout.dart'; |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 8 | |
| 9 | class StockArrow extends Component { |
Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 10 | static final Style _style = new Style(''' |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 11 | width: 40px; |
| 12 | height: 40px; |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 13 | align-items: center; |
| 14 | justify-content: center; |
| 15 | border-radius: 40px; |
| 16 | margin-right: 16px; |
| 17 | border: 1px solid transparent;''' |
| 18 | ); |
| 19 | |
Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 20 | static final Style _upStyle = new Style(''' |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 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 | |
Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 29 | static final Style _downStyle = new Style(''' |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 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 | |
Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 38 | double percentChange; |
| 39 | |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 40 | StockArrow({ Object key, this.percentChange }) : super(key: key); |
| 41 | |
Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 42 | // TODO(abarth): These should use sky/framework/theme/colors.dart. |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 43 | final List<String> _kRedColors = [ |
| 44 | '#E57373', |
| 45 | '#EF5350', |
| 46 | '#F44336', |
| 47 | '#E53935', |
| 48 | '#D32F2F', |
| 49 | '#C62828', |
| 50 | '#B71C1C', |
| 51 | ]; |
| 52 | |
Adam Barth | df9d48a | 2015-03-17 15:08:53 -0700 | [diff] [blame] | 53 | // TODO(abarth): These should use sky/framework/theme/colors.dart. |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 54 | final List<String> _kGreenColors = [ |
| 55 | '#81C784', |
| 56 | '#66BB6A', |
| 57 | '#4CAF50', |
| 58 | '#43A047', |
| 59 | '#388E3C', |
| 60 | '#2E7D32', |
| 61 | '#1B5E20', |
| 62 | ]; |
| 63 | |
| 64 | int _colorIndexForPercentChange(double percentChange) { |
| 65 | // Currently the max is 10%. |
| 66 | double maxPercent = 10.0; |
| 67 | return max(0, ((percentChange.abs() / maxPercent) * _kGreenColors.length).floor()); |
| 68 | } |
| 69 | |
| 70 | String _colorForPercentChange(double percentChange) { |
| 71 | if (percentChange > 0) |
| 72 | return _kGreenColors[_colorIndexForPercentChange(percentChange)]; |
| 73 | return _kRedColors[_colorIndexForPercentChange(percentChange)]; |
| 74 | } |
| 75 | |
Hixie | fd14a1d | 2015-04-01 09:46:28 -0700 | [diff] [blame] | 76 | UINode build() { |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 77 | String border = _colorForPercentChange(percentChange).toString(); |
| 78 | bool up = percentChange > 0; |
| 79 | String type = up ? 'bottom' : 'top'; |
| 80 | |
Hixie | 4c15a96 | 2015-05-15 15:16:25 -0700 | [diff] [blame] | 81 | return new FlexContainer( |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 82 | inlineStyle: 'border-color: $border', |
Hixie | 4c15a96 | 2015-05-15 15:16:25 -0700 | [diff] [blame] | 83 | direction: FlexDirection.Row, |
Rafael Weinstein | 4c2f248 | 2015-03-13 14:08:24 -0700 | [diff] [blame] | 84 | style: _style, |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 85 | children: [ |
| 86 | new Container( |
| 87 | inlineStyle: 'border-$type-color: $border', |
Rafael Weinstein | 4c2f248 | 2015-03-13 14:08:24 -0700 | [diff] [blame] | 88 | style: up ? _upStyle : _downStyle |
Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame] | 89 | ) |
| 90 | ] |
| 91 | ); |
| 92 | } |
| 93 | } |