1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'stock_arrow.dart';
import 'stock_data.dart';
class _StockSymbolView extends StatelessWidget {
const _StockSymbolView({
required this.stock,
required this.arrow,
});
final Stock stock;
final Widget arrow;
@override
Widget build(BuildContext context) {
assert(stock != null);
final String lastSale = '\$${stock.lastSale.toStringAsFixed(2)}';
String changeInPrice = '${stock.percentChange.toStringAsFixed(2)}%';
if (stock.percentChange > 0)
changeInPrice = '+$changeInPrice';
final TextStyle headings = Theme.of(context).textTheme.bodyText1!;
return Container(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
stock.symbol,
key: ValueKey<String>('${stock.symbol}_symbol_name'),
style: Theme.of(context).textTheme.headline3,
),
arrow,
],
),
Text('Last Sale', style: headings),
Text('$lastSale ($changeInPrice)'),
Container(
height: 8.0
),
Text('Market Cap', style: headings),
Text(stock.marketCap),
Container(
height: 8.0
),
RichText(
text: TextSpan(
style: DefaultTextStyle.of(context).style.merge(const TextStyle(fontSize: 8.0)),
text: 'Prices may be delayed by ',
children: const <TextSpan>[
TextSpan(text: 'several', style: TextStyle(fontStyle: FontStyle.italic)),
TextSpan(text: ' years.'),
],
),
),
],
),
);
}
}
class StockSymbolPage extends StatelessWidget {
const StockSymbolPage({
Key? key,
required this.symbol,
required this.stocks,
}) : super(key: key);
final String symbol;
final StockData stocks;
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: stocks,
builder: (BuildContext context, Widget? child) {
final Stock? stock = stocks[symbol];
return Scaffold(
appBar: AppBar(
title: Text(stock?.name ?? symbol),
),
body: SingleChildScrollView(
child: Container(
margin: const EdgeInsets.all(20.0),
child: Card(
child: AnimatedCrossFade(
duration: const Duration(milliseconds: 300),
firstChild: const Padding(
padding: EdgeInsets.all(20.0),
child: Center(child: CircularProgressIndicator()),
),
secondChild: stock != null
? _StockSymbolView(
stock: stock,
arrow: Hero(
tag: stock,
child: StockArrow(percentChange: stock.percentChange),
),
) : Padding(
padding: const EdgeInsets.all(20.0),
child: Center(child: Text('$symbol not found')),
),
crossFadeState: stock == null && stocks.loading ? CrossFadeState.showFirst : CrossFadeState.showSecond,
),
),
),
),
);
},
);
}
}
class StockSymbolBottomSheet extends StatelessWidget {
const StockSymbolBottomSheet({
Key? key,
required this.stock,
}) : super(key: key);
final Stock stock;
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(10.0),
decoration: const BoxDecoration(
border: Border(top: BorderSide(color: Colors.black26))
),
child: _StockSymbolView(
stock: stock,
arrow: StockArrow(percentChange: stock.percentChange),
),
);
}
}