From b5b7ddac27dd175253ca63ca11d003c626e2c606 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 11 Mar 2015 12:07:24 -0700 Subject: [PATCH] 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 --- examples/stocks-fn/stocklist.dart | 18 ++++++++++-------- examples/stocks-fn/stocksapp.dart | 21 +++++++++++++++++---- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/stocks-fn/stocklist.dart b/examples/stocks-fn/stocklist.dart index 9f0de09196a..50c64f36001 100644 --- a/examples/stocks-fn/stocklist.dart +++ b/examples/stocks-fn/stocklist.dart @@ -1,20 +1,22 @@ part of stocksapp; class Stocklist extends FixedHeightScrollable { - + String query; List stocks; Stocklist({ Object key, - this.stocks + this.stocks, + this.query }) : super(key: key, minOffset: 0.0); List 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); } } diff --git a/examples/stocks-fn/stocksapp.dart b/examples/stocks-fn/stocksapp.dart index 16333e06267..77cb6ef4e6c 100644 --- a/examples/stocks-fn/stocksapp.dart +++ b/examples/stocks-fn/stocksapp.dart @@ -42,6 +42,7 @@ class StocksApp extends App { List _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,