stock_row.dart 2.65 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// 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
    required 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 22 23
  final StockRowActionCallback? onPressed;
  final StockRowActionCallback? onDoubleTap;
  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
    }
38
    return InkWell(
39
      key: ValueKey<String>(stock.symbol),
40 41 42
      onTap: _getHandler(onPressed),
      onDoubleTap: _getHandler(onDoubleTap),
      onLongPress: _getHandler(onLongPressed),
43
      child: Container(
44
        padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 20.0),
45 46 47
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(color: Theme.of(context).dividerColor)
48 49
          )
        ),
50
        child: Row(
51
          children: <Widget>[
52
            Container(
53
              margin: const EdgeInsets.only(right: 5.0),
54
              child: Hero(
55
                tag: stock,
56 57
                child: StockArrow(percentChange: stock.percentChange),
              ),
58
            ),
59 60
            Expanded(
              child: Row(
61 62
                crossAxisAlignment: CrossAxisAlignment.baseline,
                textBaseline: DefaultTextStyle.of(context).style.textBaseline,
63
                children: <Widget>[
64
                  Expanded(
65
                    flex: 2,
66
                    child: Text(
67
                      stock.symbol
68
                    ),
69
                  ),
70 71
                  Expanded(
                    child: Text(
72
                      lastSale,
73 74
                      textAlign: TextAlign.right,
                    ),
75
                  ),
76 77
                  Expanded(
                    child: Text(
78
                      changeInPrice,
79 80
                      textAlign: TextAlign.right,
                    ),
81 82
                  ),
                ],
83
              ),
84
            ),
85 86 87
          ],
        ),
      ),
88 89 90
    );
  }
}