Commit b5b7ddac authored by Adam Barth's avatar Adam Barth

Make Stocks app search actually search

We don't yet reset the scroll offset, so sometimes you can't see your search
results properly.

R=rafaelw@chromium.org

Review URL: https://codereview.chromium.org/1002453003
parent bc6fb30f
part of stocksapp; part of stocksapp;
class Stocklist extends FixedHeightScrollable { class Stocklist extends FixedHeightScrollable {
String query;
List<Stock> stocks; List<Stock> stocks;
Stocklist({ Stocklist({
Object key, Object key,
this.stocks this.stocks,
this.query
}) : super(key: key, minOffset: 0.0); }) : super(key: key, minOffset: 0.0);
List<Node> buildItems(int start, int count) { List<Node> buildItems(int start, int count) {
var items = []; return stocks
for (var i = 0; i < count; i++) { .skip(start)
items.add(new StockRow(stock: stocks[start + i])); .where((stock) => query == null || stock.symbol.contains(
} new RegExp(query, caseSensitive: false)))
.take(count)
return items; .map((stock) => new StockRow(stock: stock))
.toList(growable: false);
} }
} }
...@@ -42,6 +42,7 @@ class StocksApp extends App { ...@@ -42,6 +42,7 @@ class StocksApp extends App {
List<Stock> _sortedStocks; List<Stock> _sortedStocks;
bool _isSearching = false; bool _isSearching = false;
String _searchQuery;
StocksApp() : super() { StocksApp() : super() {
_sortedStocks = oracle.stocks; _sortedStocks = oracle.stocks;
...@@ -54,6 +55,12 @@ class StocksApp extends App { ...@@ -54,6 +55,12 @@ class StocksApp extends App {
}); });
} }
void _handleSearchQueryChanged(query) {
setState(() {
_searchQuery = query;
});
}
Node build() { Node build() {
var drawer = new Drawer( var drawer = new Drawer(
animation: _drawerAnimation, animation: _drawerAnimation,
...@@ -86,9 +93,13 @@ class StocksApp extends App { ...@@ -86,9 +93,13 @@ class StocksApp extends App {
] ]
); );
Node title = _isSearching Node title;
? new Input(focused: true, placeholder: 'Search stocks') if (_isSearching) {
: new Text('I am a stocks app'); title = new Input(focused: true, placeholder: 'Search stocks',
onChanged: _handleSearchQueryChanged);
} else {
title = new Text('I am a stocks app');
}
var toolbar = new Toolbar( var toolbar = new Toolbar(
children: [ children: [
...@@ -110,6 +121,8 @@ class StocksApp extends App { ...@@ -110,6 +121,8 @@ class StocksApp extends App {
] ]
); );
var list = new Stocklist(stocks: _sortedStocks, query: _searchQuery);
var fab = new FloatingActionButton(content: new Icon( var fab = new FloatingActionButton(content: new Icon(
type: 'content/add_white', size: 24)); type: 'content/add_white', size: 24));
...@@ -119,7 +132,7 @@ class StocksApp extends App { ...@@ -119,7 +132,7 @@ class StocksApp extends App {
new Container( new Container(
key: 'Content', key: 'Content',
style: _style, style: _style,
children: [toolbar, new Stocklist(stocks: _sortedStocks)] children: [toolbar, list]
), ),
fab, fab,
drawer, drawer,
......
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