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

Implement Bloc

This commit is contained in:
Oystein Kristoffer Tveit 2020-07-13 21:21:33 +02:00
parent 2b11f5f8c2
commit d5782f5e56
4 changed files with 64 additions and 2 deletions

View File

@ -6,6 +6,9 @@ import './kanji_state.dart';
import 'package:bloc/bloc.dart';
import 'package:jisho_study_tool/services/kanji_search.dart';
export './kanji_event.dart';
export './kanji_state.dart';
class KanjiBloc extends Bloc<KanjiEvent, KanjiState> {
KanjiBloc() : super(KanjiSearchInitial());

View File

@ -0,0 +1,12 @@
import 'package:flutter/material.dart';
class LoadingScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
}
}

View File

@ -1,9 +1,46 @@
import 'package:flutter/material.dart';
import 'package:jisho_study_tool/bloc/temporary_provider.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart';
import 'package:jisho_study_tool/components/kanji/kanji__search_page/kanji_search_page.dart';
import 'package:jisho_study_tool/components/loading.dart';
class KanjiView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return searchForKanji('');
return BlocProvider(
create: (context) => KanjiBloc(),
child: _KanjiSearchPage(),
);
}
}
class _KanjiSearchPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<KanjiBloc, KanjiState>(
builder: (context, state) {
if (state is KanjiSearchInitial) return KanjiSearchBar();
else if (state is KanjiSearchLoading) return LoadingScreen();
else if (state is KanjiSearchFinished) return KanjiResultCard(state.kanji);
throw 'No such event found';
},
);
}
}
class KanjiSearchBar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: TextField(
onSubmitted: (text) => BlocProvider.of<KanjiBloc>(context).add(GetKanji(text)),
decoration: new InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'Search for kanji'
),
),
);
}
}

View File

@ -0,0 +1,10 @@
import 'package:flutter/material.dart';
class InitialState extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: TextField(),
);
}
}