stock_row.dart 3.58 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 8 9 10 11 12 13 14 15 16 17 18 19
enum StockRowPartKind { arrow, symbol, price }

class StockRowPartGlobalKey extends GlobalKey {
  const StockRowPartGlobalKey(this.stock, this.part) : super.constructor();
  final Stock stock;
  final StockRowPartKind part;
  String toString() => '[StockRowPartGlobalKey ${stock.symbol}:${part.toString().split(".")[1]})]';
  bool operator==(other) => other is StockRowPartGlobalKey && identical(other.stock, stock) && identical(other.part, part);
  int get hashCode => 37 * (37 * (373) + identityHashCode(stock)) + identityHashCode(part);
}

typedef void StockRowActionCallback(Stock stock, GlobalKey row, GlobalKey arrowKey, GlobalKey symbolKey, GlobalKey priceKey);

20
class StockRow extends StatelessComponent {
Hixie's avatar
Hixie committed
21 22 23 24
  StockRow({
    Stock stock,
    this.onPressed,
    this.onLongPressed
25 26 27 28 29
  }) : this.stock = stock,
       arrowKey = new StockRowPartGlobalKey(stock, StockRowPartKind.arrow),
       symbolKey = new StockRowPartGlobalKey(stock, StockRowPartKind.symbol),
       priceKey = new StockRowPartGlobalKey(stock, StockRowPartKind.price),
       super(key: new GlobalObjectKey(stock));
30 31

  final Stock stock;
32 33 34 35 36
  final StockRowActionCallback onPressed;
  final StockRowActionCallback onLongPressed;
  final GlobalKey arrowKey;
  final GlobalKey symbolKey;
  final GlobalKey priceKey;
37 38 39

  static const double kHeight = 79.0;

40
  GestureTapCallback _getTapHandler(StockRowActionCallback callback) {
41 42 43 44 45
    if (callback == null)
      return null;
    return () => callback(stock, key, arrowKey, symbolKey, priceKey);
  }

46
  GestureLongPressCallback _getLongPressHandler(StockRowActionCallback callback) {
47 48 49 50 51
    if (callback == null)
      return null;
    return () => callback(stock, key, arrowKey, symbolKey, priceKey);
  }

52
  Widget build(BuildContext context) {
53 54 55 56 57
    String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}";

    String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%";
    if (stock.percentChange > 0) changeInPrice = "+" + changeInPrice;

58
    return new InkWell(
59 60
      onTap: _getTapHandler(onPressed),
      onLongPress: _getLongPressHandler(onLongPressed),
61 62 63 64 65 66 67 68 69 70 71 72
      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)
          )
        ),
        child: new Row([
          new Container(
            key: arrowKey,
            child: new StockArrow(percentChange: stock.percentChange),
            margin: const EdgeDims.only(right: 5.0)
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
          new Flexible(
            child: new Row([
                new Flexible(
                  flex: 2,
                  child: new Text(
                    stock.symbol,
                    key: symbolKey
                  )
                ),
                new Flexible(
                  child: new Text(
                    lastSale,
                    style: const TextStyle(textAlign: TextAlign.right),
                    key: priceKey
                  )
                ),
                new Flexible(
                  child: new Text(
                    changeInPrice,
                    style: Theme.of(context).text.caption.copyWith(textAlign: TextAlign.right)
                  )
                ),
              ],
              alignItems: FlexAlignItems.baseline,
              textBaseline: DefaultTextStyle.of(context).textBaseline
99
            )
100 101
          )
        ])
102
      )
103 104 105
    );
  }
}