Commit 6fcdb64a authored by Ian Hickson's avatar Ian Hickson

Merge pull request #1455 from Hixie/heroes2

Groundwork for heroes transition in Stocks app
parents 6baf162a 7734c0b0
...@@ -186,13 +186,13 @@ class StockHomeState extends State<StockHome> { ...@@ -186,13 +186,13 @@ class StockHomeState extends State<StockHome> {
Widget buildStockList(BuildContext context, Iterable<Stock> stocks) { Widget buildStockList(BuildContext context, Iterable<Stock> stocks) {
return new StockList( return new StockList(
stocks: stocks.toList(), stocks: stocks.toList(),
onAction: (Stock stock) { onAction: (Stock stock, GlobalKey row, GlobalKey arrowKey, GlobalKey symbolKey, GlobalKey priceKey) {
setState(() { setState(() {
stock.percentChange = 100.0 * (1.0 / stock.lastSale); stock.percentChange = 100.0 * (1.0 / stock.lastSale);
stock.lastSale += 1.0; stock.lastSale += 1.0;
}); });
}, },
onOpen: (Stock stock) { onOpen: (Stock stock, GlobalKey row, GlobalKey arrowKey, GlobalKey symbolKey, GlobalKey priceKey) {
config.navigator.pushNamed('/stock/${stock.symbol}'); config.navigator.pushNamed('/stock/${stock.symbol}');
} }
); );
......
...@@ -4,14 +4,12 @@ ...@@ -4,14 +4,12 @@
part of stocks; part of stocks;
typedef void StockActionListener(Stock stock);
class StockList extends StatelessComponent { class StockList extends StatelessComponent {
StockList({ Key key, this.stocks, this.onAction, this.onOpen }) : super(key: key); StockList({ Key key, this.stocks, this.onOpen, this.onAction }) : super(key: key);
final List<Stock> stocks; final List<Stock> stocks;
final StockActionListener onAction; final StockRowActionCallback onOpen;
final StockActionListener onOpen; final StockRowActionCallback onAction;
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Material( return new Material(
...@@ -22,8 +20,8 @@ class StockList extends StatelessComponent { ...@@ -22,8 +20,8 @@ class StockList extends StatelessComponent {
itemBuilder: (BuildContext context, Stock stock) { itemBuilder: (BuildContext context, Stock stock) {
return new StockRow( return new StockRow(
stock: stock, stock: stock,
onPressed: () { onAction(stock); }, onPressed: onOpen,
onLongPressed: () { onOpen(stock); } onLongPressed: onAction
); );
} }
) )
......
...@@ -4,47 +4,60 @@ ...@@ -4,47 +4,60 @@
part of stocks; part of stocks;
enum StockRowPartKind { arrow, symbol, price }
class StockRowPartGlobalKey extends GlobalKey {
const StockRowPartGlobalKey(this.stock, this.part) : super.constructor();
final Stock stock;
final StockRowPartKind part;
String toString() => '[StockRowPartGlobalKey ${stock.symbol}:${part.toString().split(".")[1]})]';
bool operator==(other) => other is StockRowPartGlobalKey && identical(other.stock, stock) && identical(other.part, part);
int get hashCode => 37 * (37 * (373) + identityHashCode(stock)) + identityHashCode(part);
}
typedef void StockRowActionCallback(Stock stock, GlobalKey row, GlobalKey arrowKey, GlobalKey symbolKey, GlobalKey priceKey);
class StockRow extends StatelessComponent { class StockRow extends StatelessComponent {
StockRow({ StockRow({
Stock stock, Stock stock,
this.onPressed, this.onPressed,
this.onLongPressed this.onLongPressed
}) : this.stock = stock, super(key: new Key(stock.symbol)); }) : this.stock = stock,
arrowKey = new StockRowPartGlobalKey(stock, StockRowPartKind.arrow),
symbolKey = new StockRowPartGlobalKey(stock, StockRowPartKind.symbol),
priceKey = new StockRowPartGlobalKey(stock, StockRowPartKind.price),
super(key: new GlobalObjectKey(stock));
final Stock stock; final Stock stock;
final GestureTapListener onPressed; final StockRowActionCallback onPressed;
final GestureLongPressListener onLongPressed; final StockRowActionCallback onLongPressed;
final GlobalKey arrowKey;
final GlobalKey symbolKey;
final GlobalKey priceKey;
static const double kHeight = 79.0; static const double kHeight = 79.0;
GestureTapListener _getTapHandler(StockRowActionCallback callback) {
if (callback == null)
return null;
return () => callback(stock, key, arrowKey, symbolKey, priceKey);
}
GestureLongPressListener _getLongPressHandler(StockRowActionCallback callback) {
if (callback == null)
return null;
return () => callback(stock, key, arrowKey, symbolKey, priceKey);
}
Widget build(BuildContext context) { Widget build(BuildContext context) {
String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}"; String lastSale = "\$${stock.lastSale.toStringAsFixed(2)}";
String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%"; String changeInPrice = "${stock.percentChange.toStringAsFixed(2)}%";
if (stock.percentChange > 0) changeInPrice = "+" + changeInPrice; if (stock.percentChange > 0) changeInPrice = "+" + changeInPrice;
List<Widget> children = [
new Flexible(
child: new Text(stock.symbol),
flex: 2
),
new Flexible(
child: new Text(
lastSale,
style: const TextStyle(textAlign: TextAlign.right)
)
),
new Flexible(
child: new Text(
changeInPrice,
style: Theme.of(context).text.caption.copyWith(textAlign: TextAlign.right)
)
)
];
return new GestureDetector( return new GestureDetector(
onTap: onPressed, onTap: _getTapHandler(onPressed),
onLongPress: onLongPressed, onLongPress: _getLongPressHandler(onLongPressed),
child: new InkWell( child: new InkWell(
child: new Container( child: new Container(
padding: const EdgeDims.TRBL(16.0, 16.0, 20.0, 16.0), padding: const EdgeDims.TRBL(16.0, 16.0, 20.0, 16.0),
...@@ -55,12 +68,33 @@ class StockRow extends StatelessComponent { ...@@ -55,12 +68,33 @@ class StockRow extends StatelessComponent {
), ),
child: new Row([ child: new Row([
new Container( new Container(
key: arrowKey,
child: new StockArrow(percentChange: stock.percentChange), child: new StockArrow(percentChange: stock.percentChange),
margin: const EdgeDims.only(right: 5.0) margin: const EdgeDims.only(right: 5.0)
), ),
new Flexible( new Flexible(
child: new Row( child: new Row([
children, new Flexible(
flex: 2,
child: new Text(
stock.symbol,
key: symbolKey
)
),
new Flexible(
child: new Text(
lastSale,
style: const TextStyle(textAlign: TextAlign.right),
key: priceKey
)
),
new Flexible(
child: new Text(
changeInPrice,
style: Theme.of(context).text.caption.copyWith(textAlign: TextAlign.right)
)
),
],
alignItems: FlexAlignItems.baseline, alignItems: FlexAlignItems.baseline,
textBaseline: DefaultTextStyle.of(context).textBaseline textBaseline: DefaultTextStyle.of(context).textBaseline
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment