stock_row.dart 3.3 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
part of stocks;
6

7
enum StockRowPartKind { arrow }
8

Hixie's avatar
Hixie committed
9
class StockRowPartKey extends Key {
10
  const StockRowPartKey(this.stock, this.part) : super.constructor();
11 12
  final Stock stock;
  final StockRowPartKind part;
Hixie's avatar
Hixie committed
13
  bool operator ==(dynamic other) {
14
    if (other is! StockRowPartKey)
Hixie's avatar
Hixie committed
15
      return false;
16
    final StockRowPartKey typedOther = other;
Hixie's avatar
Hixie committed
17 18 19
    return stock == typedOther.stock &&
           part == typedOther.part;
  }
20
  int get hashCode => 37 * (37 * (373) + identityHashCode(stock)) + identityHashCode(part);
21
  String toString() => '[StockRowPartKey ${stock.symbol}:${part.toString().split(".")[1]})]';
22 23
}

Hixie's avatar
Hixie committed
24
typedef void StockRowActionCallback(Stock stock, Key arrowKey);
25

26
class StockRow extends StatelessComponent {
Hixie's avatar
Hixie committed
27 28 29
  StockRow({
    Stock stock,
    this.onPressed,
30
    this.onDoubleTap,
Hixie's avatar
Hixie committed
31
    this.onLongPressed
32
  }) : this.stock = stock,
33
       _arrowKey = new StockRowPartKey(stock, StockRowPartKind.arrow),
Hixie's avatar
Hixie committed
34
       super(key: new ObjectKey(stock));
35 36

  final Stock stock;
37
  final StockRowActionCallback onPressed;
38
  final StockRowActionCallback onDoubleTap;
39
  final StockRowActionCallback onLongPressed;
40 41

  final Key _arrowKey;
42 43 44

  static const double kHeight = 79.0;

45 46
  GestureTapCallback _getHandler(StockRowActionCallback callback) {
    return callback == null ? null : () => callback(stock, _arrowKey);
47 48
  }

49
  Widget build(BuildContext context) {
Hixie's avatar
Hixie committed
50
    final String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}";
51
    String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%";
52 53
    if (stock.percentChange > 0)
      changeInPrice = "+" + changeInPrice;
54
    return new InkWell(
55 56 57
      onTap: _getHandler(onPressed),
      onDoubleTap: _getHandler(onDoubleTap),
      onLongPress: _getHandler(onLongPressed),
58 59 60 61 62 63 64
      child: new Container(
        padding: const EdgeDims.TRBL(16.0, 16.0, 20.0, 16.0),
        decoration: new BoxDecoration(
          border: new Border(
            bottom: new BorderSide(color: Theme.of(context).dividerColor)
          )
        ),
Hixie's avatar
Hixie committed
65
        child: new Row(<Widget>[
66
            new Container(
Hixie's avatar
Hixie committed
67 68 69 70 71 72
              margin: const EdgeDims.only(right: 5.0),
              child: new Hero(
                tag: StockRowPartKind.arrow,
                key: _arrowKey,
                child: new StockArrow(percentChange: stock.percentChange)
              )
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            ),
            new Flexible(
              child: new Row(<Widget>[
                  new Flexible(
                    flex: 2,
                    child: new Text(
                      stock.symbol
                    )
                  ),
                  new Flexible(
                    child: new Text(
                      lastSale,
                      style: const TextStyle(textAlign: TextAlign.right)
                    )
                  ),
                  new Flexible(
                    child: new Text(
                      changeInPrice,
                      style: const TextStyle(textAlign: TextAlign.right)
                    )
                  ),
                ],
                alignItems: FlexAlignItems.baseline,
                textBaseline: DefaultTextStyle.of(context).textBaseline
              )
            ),
          ]
        )
101
      )
102 103 104
    );
  }
}