From e50a7128937571b173208767b2cac59256f13feb Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 19 Aug 2020 18:25:45 +0200 Subject: [PATCH] Implement search bloc to search bar --- lib/bloc/search/search_bloc.dart | 2 +- lib/bloc/search/search_state.dart | 4 ++-- lib/screens/kanji_search.dart | 9 +++------ lib/screens/search.dart | 29 ++++++++++++++++++++++++++++- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/bloc/search/search_bloc.dart b/lib/bloc/search/search_bloc.dart index 12fd1f2..889fdbf 100644 --- a/lib/bloc/search/search_bloc.dart +++ b/lib/bloc/search/search_bloc.dart @@ -23,7 +23,7 @@ class SearchBloc extends Bloc { try { final _searchResults = await fetchJishoResults(event.searchString); - yield SearchFinished(_searchResults); + if (_searchResults.meta.status == 200) yield SearchFinished(_searchResults.data); } on Exception { yield SearchError('Something went wrong'); } diff --git a/lib/bloc/search/search_state.dart b/lib/bloc/search/search_state.dart index cf0ed2c..4f934ee 100644 --- a/lib/bloc/search/search_state.dart +++ b/lib/bloc/search/search_state.dart @@ -14,9 +14,9 @@ class SearchLoading extends SearchState { } class SearchFinished extends SearchState { - final JishoAPIResult result; + final List results; - const SearchFinished(this.result); + const SearchFinished(this.results); } class SearchError extends SearchState { diff --git a/lib/screens/kanji_search.dart b/lib/screens/kanji_search.dart index 9ed202f..617423b 100644 --- a/lib/screens/kanji_search.dart +++ b/lib/screens/kanji_search.dart @@ -20,12 +20,9 @@ class KanjiView extends StatelessWidget { }, child: BlocBuilder( builder: (context, state) { - if (state is KanjiSearchInitial) { - return Container(); - } else if (state is KanjiSearchInput) - return KanjiSuggestions(state.kanjiSuggestions); - else if (state is KanjiSearchLoading) - return LoadingScreen(); + if (state is KanjiSearchInitial) return Container(); + else if (state is KanjiSearchInput) return KanjiSuggestions(state.kanjiSuggestions); + else if (state is KanjiSearchLoading) return LoadingScreen(); else if (state is KanjiSearchFinished) return WillPopScope( child: KanjiResultCard(state.kanji), diff --git a/lib/screens/search.dart b/lib/screens/search.dart index a28cc49..a38e5c9 100644 --- a/lib/screens/search.dart +++ b/lib/screens/search.dart @@ -1,13 +1,39 @@ import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:jisho_study_tool/bloc/search/search_bloc.dart'; +import 'package:jisho_study_tool/components/loading.dart'; +import 'package:jisho_study_tool/components/search/search_card.dart'; class SearchView extends StatelessWidget { + @override + Widget build(BuildContext context) { + return BlocListener( + listener: (context, state) { + }, + child: BlocBuilder( + builder: (context, state) { + if (state is SearchInitial) return _InitialView(); + else if (state is SearchLoading) return LoadingScreen(); + else if (state is SearchFinished) { + return ListView( + children: state.results.map((result) => SearchResultCard(result)).toList(), + ); + } + }, + ) + ); + } +} + +class _InitialView extends StatelessWidget { + @override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ SearchBar(), - ], + ] ); } } @@ -44,6 +70,7 @@ class SearchBar extends StatelessWidget { child: Column( children: [ TextField( + onSubmitted: (text) => BlocProvider.of(context).add(GetSearchResults(text)), controller: TextEditingController(), decoration: InputDecoration( labelText: 'Search',