Implement Bloc
This commit is contained in:
parent
2b11f5f8c2
commit
d5782f5e56
|
@ -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());
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
class LoadingScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -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'
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
class InitialState extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: TextField(),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue