stock_symbol_viewer.dart 3.97 KB
Newer Older
Hixie's avatar
Hixie committed
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';
Hixie's avatar
Hixie committed
9

10
class _StockSymbolView extends StatelessWidget {
11
  const _StockSymbolView({ this.stock, this.arrow });
Hixie's avatar
Hixie committed
12 13

  final Stock stock;
14
  final Widget arrow;
Hixie's avatar
Hixie committed
15

16
  @override
Hixie's avatar
Hixie committed
17
  Widget build(BuildContext context) {
18
    assert(stock != null);
19 20
    final String lastSale = '\$${stock.lastSale.toStringAsFixed(2)}';
    String changeInPrice = '${stock.percentChange.toStringAsFixed(2)}%';
21
    if (stock.percentChange > 0)
22
      changeInPrice = '+' + changeInPrice;
23

24
    final TextStyle headings = Theme.of(context).textTheme.body2;
25
    return Container(
26
      padding: const EdgeInsets.all(20.0),
27
      child: Column(
28
        children: <Widget>[
29
          Row(
30
            children: <Widget>[
31
              Text(
32
                '${stock.symbol}',
33
                style: Theme.of(context).textTheme.display2,
34
              ),
35
              arrow,
36
            ],
37
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
38
          ),
39 40 41
          Text('Last Sale', style: headings),
          Text('$lastSale ($changeInPrice)'),
          Container(
42 43
            height: 8.0
          ),
44 45 46
          Text('Market Cap', style: headings),
          Text('${stock.marketCap}'),
          Container(
47 48
            height: 8.0
          ),
49 50
          RichText(
            text: TextSpan(
51
              style: DefaultTextStyle.of(context).style.merge(const TextStyle(fontSize: 8.0)),
52
              text: 'Prices may be delayed by ',
53
              children: const <TextSpan>[
54 55
                TextSpan(text: 'several', style: TextStyle(fontStyle: FontStyle.italic)),
                TextSpan(text: ' years.'),
56 57
              ],
            ),
58
          ),
59
        ],
60 61
        mainAxisSize: MainAxisSize.min,
      ),
62 63 64 65
    );
  }
}

66
class StockSymbolPage extends StatelessWidget {
67
  const StockSymbolPage({ this.symbol, this.stocks });
68

69 70
  final String symbol;
  final StockData stocks;
71

72
  @override
73
  Widget build(BuildContext context) {
74
    return AnimatedBuilder(
75 76 77
      animation: stocks,
      builder: (BuildContext context, Widget child) {
        final Stock stock = stocks[symbol];
78 79
        return Scaffold(
          appBar: AppBar(
80
            title: Text(stock?.name ?? symbol),
81
          ),
82 83
          body: SingleChildScrollView(
            child: Container(
84
              margin: const EdgeInsets.all(20.0),
85 86
              child: Card(
                child: AnimatedCrossFade(
87 88
                  duration: const Duration(milliseconds: 300),
                  firstChild: const Padding(
89 90
                    padding: EdgeInsets.all(20.0),
                    child: Center(child: CircularProgressIndicator()),
91 92
                  ),
                  secondChild: stock != null
93
                    ? _StockSymbolView(
94
                      stock: stock,
95
                      arrow: Hero(
96
                        tag: stock,
97
                        child: StockArrow(percentChange: stock.percentChange),
98
                      ),
99
                    ) : Padding(
100
                        padding: const EdgeInsets.all(20.0),
101
                        child: Center(child: Text('$symbol not found')),
102 103 104
                    ),
                  crossFadeState: stock == null && stocks.loading ? CrossFadeState.showFirst : CrossFadeState.showSecond,
                ),
105 106 107
              ),
            ),
          ),
108 109
        );
      },
Hixie's avatar
Hixie committed
110 111
    );
  }
112 113
}

114
class StockSymbolBottomSheet extends StatelessWidget {
115
  const StockSymbolBottomSheet({ this.stock });
116 117

  final Stock stock;
Hixie's avatar
Hixie committed
118

119
  @override
120
  Widget build(BuildContext context) {
121
    return Container(
122
      padding: const EdgeInsets.all(10.0),
123
      decoration: const BoxDecoration(
124
        border: Border(top: BorderSide(color: Colors.black26))
125
      ),
126
      child: _StockSymbolView(
127
        stock: stock,
128 129
        arrow: StockArrow(percentChange: stock.percentChange),
      ),
130 131
   );
  }
Hixie's avatar
Hixie committed
132
}