1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-21 13:37:29 +01:00

Implement search bloc to search bar

This commit is contained in:
Oystein Kristoffer Tveit 2020-08-19 18:25:45 +02:00
parent 7fd21d974b
commit e50a712893
4 changed files with 34 additions and 10 deletions

View File

@ -23,7 +23,7 @@ class SearchBloc extends Bloc<SearchEvent, SearchState> {
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');
}

View File

@ -14,9 +14,9 @@ class SearchLoading extends SearchState {
}
class SearchFinished extends SearchState {
final JishoAPIResult result;
final List<JishoResult> results;
const SearchFinished(this.result);
const SearchFinished(this.results);
}
class SearchError extends SearchState {

View File

@ -20,12 +20,9 @@ class KanjiView extends StatelessWidget {
},
child: BlocBuilder<KanjiBloc, KanjiState>(
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),

View File

@ -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<SearchBloc, SearchState>(
listener: (context, state) {
},
child: BlocBuilder<SearchBloc, SearchState>(
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<SearchBloc>(context).add(GetSearchResults(text)),
controller: TextEditingController(),
decoration: InputDecoration(
labelText: 'Search',