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

154 lines
4.4 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';
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) {
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> {
FocusNode _focus = new FocusNode();
TextEditingController _textController = new TextEditingController();
2020-07-22 16:24:00 +02:00
TextFieldButton _button = TextFieldButton.paste;
2020-07-22 15:22:21 +02:00
@override
void initState() {
super.initState();
_focus.addListener(_onFocusChange);
}
2020-07-22 16:24:00 +02:00
void _getKanjiSuggestions(String text) =>
BlocProvider.of<KanjiBloc>(context).add(GetKanjiSuggestions(text));
void updateSuggestions() => _getKanjiSuggestions(_textController.text);
2020-07-22 15:22:21 +02:00
void _onFocusChange() {
debugPrint('TextField Focus Changed: ${_focus.hasFocus.toString()}');
2020-07-22 16:24:00 +02:00
setState(() {
_button = _focus.hasFocus ? TextFieldButton.clear : TextFieldButton.paste;
});
if (_focus.hasFocus)
updateSuggestions();
else
FocusScope.of(context).unfocus();
}
2020-07-22 15:22:21 +02:00
void _clearText() {
_textController.text = '';
2020-07-22 16:24:00 +02:00
updateSuggestions();
}
void _pasteText() async {
ClipboardData clipboardData = await Clipboard.getData('text/plain');
_textController.text = clipboardData.text;
updateSuggestions();
2020-07-22 15:22:21 +02:00
}
@override
Widget build(BuildContext context) {
2020-07-22 16:24:00 +02:00
IconButton _clearButton = IconButton(
icon: Icon(Icons.clear),
onPressed: () => _clearText(),
);
IconButton _pasteButton = IconButton(
icon: Icon(Icons.content_paste),
onPressed: () => _pasteText(),
);
2020-07-22 15:22:21 +02:00
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,
2020-07-22 16:24:00 +02:00
suffixIcon: (_button == TextFieldButton.clear) ? _clearButton : _pasteButton,
2020-07-22 15:22:21 +02:00
),
style: TextStyle(
fontSize: 14.0,
),
);
}
}