Jisho-Study-Tool/lib/screens/kanji_search.dart

131 lines
3.8 KiB
Dart
Raw Normal View History

2020-06-30 15:15:31 +02:00
import 'package:flutter/material.dart';
2020-07-13 21:21:33 +02:00
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/kanji/kanji_suggestions.dart';
2020-07-13 21:21:33 +02:00
import 'package:jisho_study_tool/components/loading.dart';
2020-06-30 15:15:31 +02:00
2020-07-09 22:17:10 +02:00
class KanjiView extends StatelessWidget {
2020-07-13 21:21:33 +02:00
@override
Widget build(BuildContext context) {
return BlocListener<KanjiBloc, KanjiState>(
listener: (context, state) {
if (state is KanjiSearchInitial) {
FocusScope.of(context).unfocus();
} else if (state is KanjiSearchLoading) {
FocusScope.of(context).unfocus();
}
2020-07-13 21:21:33 +02:00
},
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();
else if (state is KanjiSearchFinished)
return WillPopScope(
child: KanjiResultCard(state.kanji),
onWillPop: () async {
BlocProvider.of<KanjiBloc>(context)
.add(ReturnToInitialState());
return false;
});
throw 'No such event found';
},
),
2020-07-13 21:21:33 +02:00
);
}
}
2020-07-14 12:52:48 +02:00
class KanjiViewBar extends StatelessWidget {
2020-07-13 21:21:33 +02:00
@override
Widget build(BuildContext context) {
2020-07-16 14:08:02 +02:00
return Container(
child: Row(
children: [
IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () =>
BlocProvider.of<KanjiBloc>(context).add(ReturnToInitialState()),
2020-07-16 14:08:02 +02:00
),
Expanded(
child: Container(
2020-07-22 15:22:21 +02:00
child: _KanjiTextField(),
2020-07-14 14:33:21 +02:00
),
2020-07-16 14:08:02 +02:00
),
IconButton(
icon: Icon(Icons.star_border),
onPressed: null,
),
IconButton(
icon: Icon(Icons.add),
onPressed: null,
),
],
2020-07-13 21:21:33 +02:00
),
);
2020-06-30 15:15:31 +02:00
}
2020-07-14 14:33:21 +02:00
}
2020-07-22 15:22:21 +02:00
class _KanjiTextField extends StatefulWidget {
@override
_KanjiTextFieldState createState() => new _KanjiTextFieldState();
}
class _KanjiTextFieldState extends State<_KanjiTextField> {
FocusNode _focus = new FocusNode();
TextEditingController _textController = new TextEditingController();
@override
void initState() {
super.initState();
_focus.addListener(_onFocusChange);
}
void _onFocusChange() {
debugPrint('TextField Focus Changed: ${_focus.hasFocus.toString()}');
if (_focus.hasFocus) _getKanjiSuggestions(_textController.text);
else FocusScope.of(context).unfocus();
}
void _getKanjiSuggestions(String text) =>
BlocProvider.of<KanjiBloc>(context).add(GetKanjiSuggestions(text));
void _clearText() {
_textController.text = '';
_getKanjiSuggestions(_textController.text);
}
@override
Widget build(BuildContext context) {
return TextField(
focusNode: _focus,
controller: _textController,
onChanged: (text) => _getKanjiSuggestions(text),
onSubmitted: (text) =>
BlocProvider.of<KanjiBloc>(context).add(GetKanji(text)),
decoration: new InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'Search for kanji',
fillColor: Colors.white,
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(100.0),
),
contentPadding: EdgeInsets.symmetric(vertical: 10.0),
isDense: false,
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () => _clearText(),
)
),
style: TextStyle(
fontSize: 14.0,
),
);
}
}