Rafael Weinstein | 202f99d | 2015-03-02 20:55:02 -0800 | [diff] [blame^] | 1 | part of stocksapp; |
| 2 | |
| 3 | class StockRow extends Component { |
| 4 | |
| 5 | Stock stock; |
| 6 | LinkedHashSet<SplashAnimation> _splashes; |
| 7 | |
| 8 | static Style _style = new Style(''' |
| 9 | transform: translateX(0); |
| 10 | max-height: 48px; |
| 11 | display: flex; |
| 12 | align-items: center; |
| 13 | border-bottom: 1px solid #F4F4F4; |
| 14 | padding-top: 16px; |
| 15 | padding-left: 16px; |
| 16 | padding-right: 16px; |
| 17 | padding-bottom: 20px;''' |
| 18 | ); |
| 19 | |
| 20 | static Style _tickerStyle = new Style(''' |
| 21 | flex: 1; |
| 22 | font-family: 'Roboto Medium', 'Helvetica';''' |
| 23 | ); |
| 24 | |
| 25 | static Style _lastSaleStyle = new Style(''' |
| 26 | text-align: right; |
| 27 | padding-right: 16px;''' |
| 28 | ); |
| 29 | |
| 30 | static Style _changeStyle = new Style(''' |
| 31 | color: #8A8A8A; |
| 32 | text-align: right;''' |
| 33 | ); |
| 34 | |
| 35 | StockRow({Stock stock}) : super(key: stock.symbol) { |
| 36 | this.stock = stock; |
| 37 | } |
| 38 | |
| 39 | Node render() { |
| 40 | String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}"; |
| 41 | |
| 42 | String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%"; |
| 43 | if (stock.percentChange > 0) |
| 44 | changeInPrice = "+" + changeInPrice; |
| 45 | |
| 46 | List<Node> children = [ |
| 47 | new StockArrow( |
| 48 | percentChange: stock.percentChange |
| 49 | ), |
| 50 | new Container( |
| 51 | key: 'Ticker', |
| 52 | style: _tickerStyle, |
| 53 | children: [new Text(stock.symbol)] |
| 54 | ), |
| 55 | new Container( |
| 56 | key: 'LastSale', |
| 57 | style: _lastSaleStyle, |
| 58 | children: [new Text(lastSale)] |
| 59 | ), |
| 60 | new Container( |
| 61 | key: 'Change', |
| 62 | style: _changeStyle, |
| 63 | children: [new Text(changeInPrice)] |
| 64 | ) |
| 65 | ]; |
| 66 | |
| 67 | if (_splashes != null) { |
| 68 | children.addAll(_splashes.map((s) => new InkSplash(s.onStyleChanged))); |
| 69 | } |
| 70 | |
| 71 | return new Container( |
| 72 | style: _style, |
| 73 | onScrollStart: _cancelSplashes, |
| 74 | onWheel: _cancelSplashes, |
| 75 | onPointerDown: _handlePointerDown, |
| 76 | children: children |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | sky.ClientRect _getBoundingRect() => getRoot().getBoundingClientRect(); |
| 81 | |
| 82 | void _handlePointerDown(sky.Event event) { |
| 83 | setState(() { |
| 84 | if (_splashes == null) { |
| 85 | _splashes = new LinkedHashSet<SplashAnimation>(); |
| 86 | } |
| 87 | |
| 88 | var splash; |
| 89 | splash = new SplashAnimation(_getBoundingRect(), event.x, event.y, |
| 90 | onDone: () { _splashDone(splash); }); |
| 91 | |
| 92 | _splashes.add(splash); |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | void _cancelSplashes(sky.Event event) { |
| 97 | if (_splashes == null) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | setState(() { |
| 102 | var splashes = _splashes; |
| 103 | _splashes = null; |
| 104 | splashes.forEach((s) { s.cancel(); }); |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | void willUnmount() { |
| 109 | _cancelSplashes(null); |
| 110 | } |
| 111 | |
| 112 | void _splashDone(SplashAnimation splash) { |
| 113 | if (_splashes == null) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | setState(() { |
| 118 | _splashes.remove(splash); |
| 119 | if (_splashes.length == 0) { |
| 120 | _splashes = null; |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | } |