stock_row.dart 3.6 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 6 7 8
import 'package:flutter/material.dart';

import 'stock_data.dart';
import 'stock_arrow.dart';
9

10
enum StockRowPartKind { arrow }
11

Hixie's avatar
Hixie committed
12
class StockRowPartKey extends Key {
13
  const StockRowPartKey(this.keySalt, this.stock, this.part) : super.constructor();
14

15
  final Object keySalt;
16 17
  final Stock stock;
  final StockRowPartKind part;
18 19

  @override
Hixie's avatar
Hixie committed
20
  bool operator ==(dynamic other) {
21 22 23
    if (identical(this, other))
      return true;
    if (other.runtimeType != runtimeType)
Hixie's avatar
Hixie committed
24
      return false;
25
    final StockRowPartKey typedOther = other;
26 27 28
    return keySalt == typedOther.keySalt
        && stock == typedOther.stock
        && part == typedOther.part;
Hixie's avatar
Hixie committed
29
  }
30 31

  @override
32
  int get hashCode => hashValues(keySalt, stock, part);
33 34

  @override
35
  String toString() => '[$runtimeType ${keySalt.toString().split(".")[1]}:${stock.symbol}:${part.toString().split(".")[1]}]';
36 37
}

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

40
class StockRow extends StatelessWidget {
Hixie's avatar
Hixie committed
41 42
  StockRow({
    Stock stock,
43
    Object keySalt,
Hixie's avatar
Hixie committed
44
    this.onPressed,
45
    this.onDoubleTap,
Hixie's avatar
Hixie committed
46
    this.onLongPressed
47
  }) : this.stock = stock,
48
       _arrowKey = new StockRowPartKey(keySalt, stock, StockRowPartKind.arrow),
Hixie's avatar
Hixie committed
49
       super(key: new ObjectKey(stock));
50 51

  final Stock stock;
52
  final StockRowActionCallback onPressed;
53
  final StockRowActionCallback onDoubleTap;
54
  final StockRowActionCallback onLongPressed;
55 56

  final Key _arrowKey;
57 58 59

  static const double kHeight = 79.0;

60 61
  GestureTapCallback _getHandler(StockRowActionCallback callback) {
    return callback == null ? null : () => callback(stock, _arrowKey);
62 63
  }

64
  @override
65
  Widget build(BuildContext context) {
Hixie's avatar
Hixie committed
66
    final String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}";
67
    String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%";
68 69
    if (stock.percentChange > 0)
      changeInPrice = "+" + changeInPrice;
70
    return new InkWell(
71 72 73
      onTap: _getHandler(onPressed),
      onDoubleTap: _getHandler(onDoubleTap),
      onLongPress: _getHandler(onLongPressed),
74
      child: new Container(
75
        padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 20.0),
76 77 78 79 80
        decoration: new BoxDecoration(
          border: new Border(
            bottom: new BorderSide(color: Theme.of(context).dividerColor)
          )
        ),
81 82
        child: new Row(
          children: <Widget>[
83
            new Container(
84
              margin: const EdgeInsets.only(right: 5.0),
Hixie's avatar
Hixie committed
85 86 87 88 89
              child: new Hero(
                tag: StockRowPartKind.arrow,
                key: _arrowKey,
                child: new StockArrow(percentChange: stock.percentChange)
              )
90 91
            ),
            new Flexible(
92 93
              child: new Row(
                children: <Widget>[
94 95 96 97 98 99 100 101 102
                  new Flexible(
                    flex: 2,
                    child: new Text(
                      stock.symbol
                    )
                  ),
                  new Flexible(
                    child: new Text(
                      lastSale,
103
                      textAlign: TextAlign.right
104 105 106 107 108
                    )
                  ),
                  new Flexible(
                    child: new Text(
                      changeInPrice,
109
                      textAlign: TextAlign.right
110 111 112
                    )
                  ),
                ],
113
                crossAxisAlignment: CrossAxisAlignment.baseline,
114
                textBaseline: DefaultTextStyle.of(context).style.textBaseline
115 116 117 118
              )
            ),
          ]
        )
119
      )
120 121 122
    );
  }
}