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

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

10
typedef void StockRowActionCallback(Stock stock);
11

12
class StockRow extends StatelessWidget {
Hixie's avatar
Hixie committed
13
  StockRow({
14
    this.stock,
Hixie's avatar
Hixie committed
15
    this.onPressed,
16
    this.onDoubleTap,
Hixie's avatar
Hixie committed
17
    this.onLongPressed
18
  }) : super(key: new ObjectKey(stock));
19 20

  final Stock stock;
21
  final StockRowActionCallback onPressed;
22
  final StockRowActionCallback onDoubleTap;
23
  final StockRowActionCallback onLongPressed;
24

25 26
  static const double kHeight = 79.0;

27
  GestureTapCallback _getHandler(StockRowActionCallback callback) {
28
    return callback == null ? null : () => callback(stock);
29 30
  }

31
  @override
32
  Widget build(BuildContext context) {
Hixie's avatar
Hixie committed
33
    final String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}";
34
    String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%";
35 36
    if (stock.percentChange > 0)
      changeInPrice = "+" + changeInPrice;
37
    return new InkWell(
38 39 40
      onTap: _getHandler(onPressed),
      onDoubleTap: _getHandler(onDoubleTap),
      onLongPress: _getHandler(onLongPressed),
41
      child: new Container(
42
        padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 20.0),
43 44 45 46 47
        decoration: new BoxDecoration(
          border: new Border(
            bottom: new BorderSide(color: Theme.of(context).dividerColor)
          )
        ),
48 49
        child: new Row(
          children: <Widget>[
50
            new Container(
51
              margin: const EdgeInsets.only(right: 5.0),
Hixie's avatar
Hixie committed
52
              child: new Hero(
53
                tag: stock,
Hixie's avatar
Hixie committed
54 55
                child: new StockArrow(percentChange: stock.percentChange)
              )
56
            ),
57
            new Expanded(
58 59
              child: new Row(
                children: <Widget>[
60
                  new Expanded(
61 62 63 64 65
                    flex: 2,
                    child: new Text(
                      stock.symbol
                    )
                  ),
66
                  new Expanded(
67 68
                    child: new Text(
                      lastSale,
69
                      textAlign: TextAlign.right
70 71
                    )
                  ),
72
                  new Expanded(
73 74
                    child: new Text(
                      changeInPrice,
75
                      textAlign: TextAlign.right
76 77 78
                    )
                  ),
                ],
79
                crossAxisAlignment: CrossAxisAlignment.baseline,
80
                textBaseline: DefaultTextStyle.of(context).style.textBaseline
81 82 83 84
              )
            ),
          ]
        )
85
      )
86 87 88
    );
  }
}