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
This commit is contained in:
Adam Barth 2015-03-11 12:07:24 -07:00
parent bc6fb30f01
commit b5b7ddac27
2 changed files with 27 additions and 12 deletions

View file

@ -1,20 +1,22 @@
part of stocksapp;
class Stocklist extends FixedHeightScrollable {
String query;
List<Stock> stocks;
Stocklist({
Object key,
this.stocks
this.stocks,
this.query
}) : super(key: key, minOffset: 0.0);
List<Node> buildItems(int start, int count) {
var items = [];
for (var i = 0; i < count; i++) {
items.add(new StockRow(stock: stocks[start + i]));
}
return items;
return stocks
.skip(start)
.where((stock) => query == null || stock.symbol.contains(
new RegExp(query, caseSensitive: false)))
.take(count)
.map((stock) => new StockRow(stock: stock))
.toList(growable: false);
}
}

View file

@ -42,6 +42,7 @@ class StocksApp extends App {
List<Stock> _sortedStocks;
bool _isSearching = false;
String _searchQuery;
StocksApp() : super() {
_sortedStocks = oracle.stocks;
@ -54,6 +55,12 @@ class StocksApp extends App {
});
}
void _handleSearchQueryChanged(query) {
setState(() {
_searchQuery = query;
});
}
Node build() {
var drawer = new Drawer(
animation: _drawerAnimation,
@ -86,9 +93,13 @@ class StocksApp extends App {
]
);
Node title = _isSearching
? new Input(focused: true, placeholder: 'Search stocks')
: new Text('I am a stocks app');
Node title;
if (_isSearching) {
title = new Input(focused: true, placeholder: 'Search stocks',
onChanged: _handleSearchQueryChanged);
} else {
title = new Text('I am a stocks app');
}
var toolbar = new Toolbar(
children: [
@ -110,6 +121,8 @@ class StocksApp extends App {
]
);
var list = new Stocklist(stocks: _sortedStocks, query: _searchQuery);
var fab = new FloatingActionButton(content: new Icon(
type: 'content/add_white', size: 24));
@ -119,7 +132,7 @@ class StocksApp extends App {
new Container(
key: 'Content',
style: _style,
children: [toolbar, new Stocklist(stocks: _sortedStocks)]
children: [toolbar, list]
),
fab,
drawer,