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

154 lines
4.5 KiB
Dart
Raw Normal View History

2020-06-30 15:15:31 +02:00
import 'package:flutter/material.dart';
2020-07-22 16:24:00 +02:00
import 'package:flutter/services.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';
2021-03-02 22:26:59 +01:00
import 'package:jisho_study_tool/view/components/kanji/kanji_search_result_page/kanji_search_result_page.dart';
import 'package:jisho_study_tool/view/components/kanji/kanji_search_suggestion_list/kanji_search_suggestion_list.dart';
import 'package:jisho_study_tool/view/screens/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) {
2020-08-19 18:25:45 +02:00
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();
}
2020-07-22 16:24:00 +02:00
enum TextFieldButton {clear, paste}
2020-07-22 15:22:21 +02:00
class _KanjiTextFieldState extends State<_KanjiTextField> {
2021-03-03 00:24:25 +01:00
FocusNode focus = new FocusNode();
TextEditingController textController = new TextEditingController();
TextFieldButton button = TextFieldButton.paste;
2020-07-22 15:22:21 +02:00
@override
void initState() {
super.initState();
2021-03-03 00:24:25 +01:00
focus.addListener(_onFocusChange);
2020-07-22 15:22:21 +02:00
}
2020-07-22 16:24:00 +02:00
void _getKanjiSuggestions(String text) =>
BlocProvider.of<KanjiBloc>(context).add(GetKanjiSuggestions(text));
2021-03-03 00:24:25 +01:00
void updateSuggestions() => _getKanjiSuggestions(textController.text);
2020-07-22 16:24:00 +02:00
2020-07-22 15:22:21 +02:00
void _onFocusChange() {
2021-03-03 00:24:25 +01:00
debugPrint('TextField Focus Changed: ${focus.hasFocus.toString()}');
2020-07-22 15:22:21 +02:00
2020-07-22 16:24:00 +02:00
setState(() {
2021-03-03 00:24:25 +01:00
button = focus.hasFocus ? TextFieldButton.clear : TextFieldButton.paste;
2020-07-22 16:24:00 +02:00
});
2021-03-03 00:24:25 +01:00
if (focus.hasFocus)
2020-07-22 16:24:00 +02:00
updateSuggestions();
else
FocusScope.of(context).unfocus();
}
2020-07-22 15:22:21 +02:00
void _clearText() {
2021-03-03 00:24:25 +01:00
textController.text = '';
2020-07-22 16:24:00 +02:00
updateSuggestions();
}
void _pasteText() async {
ClipboardData clipboardData = await Clipboard.getData('text/plain');
2021-03-03 00:24:25 +01:00
textController.text = clipboardData.text;
2020-07-22 16:24:00 +02:00
updateSuggestions();
2020-07-22 15:22:21 +02:00
}
@override
Widget build(BuildContext context) {
2021-03-03 00:24:25 +01:00
IconButton clearButton = IconButton(
2020-07-22 16:24:00 +02:00
icon: Icon(Icons.clear),
onPressed: () => _clearText(),
);
2021-03-03 00:24:25 +01:00
IconButton pasteButton = IconButton(
2020-07-22 16:24:00 +02:00
icon: Icon(Icons.content_paste),
onPressed: () => _pasteText(),
);
2020-07-22 15:22:21 +02:00
return TextField(
2021-03-03 00:24:25 +01:00
focusNode: focus,
controller: textController,
2020-07-22 15:22:21 +02:00
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,
2021-03-03 00:24:25 +01:00
suffixIcon: (button == TextFieldButton.clear) ? clearButton : pasteButton,
2020-07-22 15:22:21 +02:00
),
style: TextStyle(
fontSize: 14.0,
),
);
}
}