blob: 5395984f25cb2daaed62dc06fe9d2d151eddabf4 [file] [log] [blame]
Adam Barthdf9d48a2015-03-17 15:08:53 -07001// 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
5import 'dart:math';
6import 'package:sky/framework/fn.dart';
Hixie4c15a962015-05-15 15:16:25 -07007import 'package:sky/framework/layout.dart';
Rafael Weinstein202f99d2015-03-02 20:55:02 -08008
9class StockArrow extends Component {
Adam Barthdf9d48a2015-03-17 15:08:53 -070010 static final Style _style = new Style('''
Rafael Weinstein202f99d2015-03-02 20:55:02 -080011 width: 40px;
12 height: 40px;
Rafael Weinstein202f99d2015-03-02 20:55:02 -080013 align-items: center;
14 justify-content: center;
15 border-radius: 40px;
16 margin-right: 16px;
17 border: 1px solid transparent;'''
18 );
19
Adam Barthdf9d48a2015-03-17 15:08:53 -070020 static final Style _upStyle = new Style('''
Rafael Weinstein202f99d2015-03-02 20:55:02 -080021 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 Barthdf9d48a2015-03-17 15:08:53 -070029 static final Style _downStyle = new Style('''
Rafael Weinstein202f99d2015-03-02 20:55:02 -080030 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 Barthdf9d48a2015-03-17 15:08:53 -070038 double percentChange;
39
Rafael Weinstein202f99d2015-03-02 20:55:02 -080040 StockArrow({ Object key, this.percentChange }) : super(key: key);
41
Adam Barthdf9d48a2015-03-17 15:08:53 -070042 // TODO(abarth): These should use sky/framework/theme/colors.dart.
Rafael Weinstein202f99d2015-03-02 20:55:02 -080043 final List<String> _kRedColors = [
44 '#E57373',
45 '#EF5350',
46 '#F44336',
47 '#E53935',
48 '#D32F2F',
49 '#C62828',
50 '#B71C1C',
51 ];
52
Adam Barthdf9d48a2015-03-17 15:08:53 -070053 // TODO(abarth): These should use sky/framework/theme/colors.dart.
Rafael Weinstein202f99d2015-03-02 20:55:02 -080054 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
Hixiefd14a1d2015-04-01 09:46:28 -070076 UINode build() {
Rafael Weinstein202f99d2015-03-02 20:55:02 -080077 String border = _colorForPercentChange(percentChange).toString();
78 bool up = percentChange > 0;
79 String type = up ? 'bottom' : 'top';
80
Hixie4c15a962015-05-15 15:16:25 -070081 return new FlexContainer(
Rafael Weinstein202f99d2015-03-02 20:55:02 -080082 inlineStyle: 'border-color: $border',
Hixie4c15a962015-05-15 15:16:25 -070083 direction: FlexDirection.Row,
Rafael Weinstein4c2f2482015-03-13 14:08:24 -070084 style: _style,
Rafael Weinstein202f99d2015-03-02 20:55:02 -080085 children: [
86 new Container(
87 inlineStyle: 'border-$type-color: $border',
Rafael Weinstein4c2f2482015-03-13 14:08:24 -070088 style: up ? _upStyle : _downStyle
Rafael Weinstein202f99d2015-03-02 20:55:02 -080089 )
90 ]
91 );
92 }
93}