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