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 { try {
final _searchResults = await fetchJishoResults(event.searchString); final _searchResults = await fetchJishoResults(event.searchString);
yield SearchFinished(_searchResults); if (_searchResults.meta.status == 200) yield SearchFinished(_searchResults.data);
} on Exception { } on Exception {
yield SearchError('Something went wrong'); yield SearchError('Something went wrong');
} }

View File

@ -14,9 +14,9 @@ class SearchLoading extends SearchState {
} }
class SearchFinished 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 { class SearchError extends SearchState {

View File

@ -20,12 +20,9 @@ class KanjiView extends StatelessWidget {
}, },
child: BlocBuilder<KanjiBloc, KanjiState>( child: BlocBuilder<KanjiBloc, KanjiState>(
builder: (context, state) { builder: (context, state) {
if (state is KanjiSearchInitial) { if (state is KanjiSearchInitial) return Container();
return Container(); else if (state is KanjiSearchInput) return KanjiSuggestions(state.kanjiSuggestions);
} else if (state is KanjiSearchInput) else if (state is KanjiSearchLoading) return LoadingScreen();
return KanjiSuggestions(state.kanjiSuggestions);
else if (state is KanjiSearchLoading)
return LoadingScreen();
else if (state is KanjiSearchFinished) else if (state is KanjiSearchFinished)
return WillPopScope( return WillPopScope(
child: KanjiResultCard(state.kanji), child: KanjiResultCard(state.kanji),

View File

@ -1,13 +1,39 @@
import 'package:flutter/material.dart'; 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 { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
SearchBar(), SearchBar(),
], ]
); );
} }
} }
@ -44,6 +70,7 @@ class SearchBar extends StatelessWidget {
child: Column( child: Column(
children: [ children: [
TextField( TextField(
onSubmitted: (text) => BlocProvider.of<SearchBloc>(context).add(GetSearchResults(text)),
controller: TextEditingController(), controller: TextEditingController(),
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Search', labelText: 'Search',