From be359726372fd2511412dc8b880cd055e5d12d0f Mon Sep 17 00:00:00 2001 From: Andrew Brogdon Date: Mon, 18 Mar 2019 08:41:16 -0700 Subject: [PATCH] Adding no-result display to search screen. (#57) --- veggieseasons/lib/screens/search.dart | 49 ++++++++++++++--------- veggieseasons/lib/widgets/search_bar.dart | 9 +++-- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/veggieseasons/lib/screens/search.dart b/veggieseasons/lib/screens/search.dart index 4a3c03558..059ca3ce3 100644 --- a/veggieseasons/lib/screens/search.dart +++ b/veggieseasons/lib/screens/search.dart @@ -17,47 +17,58 @@ class SearchScreen extends StatefulWidget { } class _SearchScreenState extends State { - final _controller = TextEditingController(); - final _focusNode = FocusNode(); - String _terms = ''; + final controller = TextEditingController(); + final focusNode = FocusNode(); + String terms = ''; @override void initState() { super.initState(); - _controller.addListener(_onTextChanged); + controller.addListener(_onTextChanged); } @override void dispose() { - _focusNode.dispose(); + focusNode.dispose(); super.dispose(); } void _onTextChanged() { - setState(() => _terms = _controller.text); + setState(() => terms = controller.text); } Widget _createSearchBox() { return Padding( padding: const EdgeInsets.all(8.0), child: SearchBar( - controller: _controller, - focusNode: _focusNode, + controller: controller, + focusNode: focusNode, ), ); } - List _generateVeggieRows(List veggies) { - final cards = new List(); - - for (Veggie veggie in veggies) { - cards.add(Padding( - padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 24.0), - child: VeggieHeadline(veggie), - )); + Widget _buildSearchResults(List veggies) { + if (veggies.isEmpty) { + return Center( + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 24.0), + child: Text( + 'No veggies matching your search terms were found.', + style: Styles.headlineDescription, + ), + ), + ); } - return cards; + return ListView.builder( + itemCount: veggies.length, + itemBuilder: (context, i) { + return Padding( + padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 24.0), + child: VeggieHeadline(veggies[i]), + ); + }, + ); } @override @@ -75,9 +86,7 @@ class _SearchScreenState extends State { children: [ _createSearchBox(), Expanded( - child: ListView( - children: _generateVeggieRows(model.searchVeggies(_terms)), - ), + child: _buildSearchResults(model.searchVeggies(terms)), ), ], ), diff --git a/veggieseasons/lib/widgets/search_bar.dart b/veggieseasons/lib/widgets/search_bar.dart index 55def0d8e..717cfa626 100644 --- a/veggieseasons/lib/widgets/search_bar.dart +++ b/veggieseasons/lib/widgets/search_bar.dart @@ -29,9 +29,11 @@ class SearchBar extends StatelessWidget { ), child: Row( children: [ - Icon( - CupertinoIcons.search, - color: Styles.searchIconColor, + ExcludeSemantics( + child: Icon( + CupertinoIcons.search, + color: Styles.searchIconColor, + ), ), Expanded( child: CupertinoTextField( @@ -47,6 +49,7 @@ class SearchBar extends StatelessWidget { }, child: Icon( CupertinoIcons.clear_thick_circled, + semanticLabel: 'Clear search terms', color: Styles.searchIconColor, ), ),