diff --git a/lib/view/components/kanji/kanji_search_bar.dart b/lib/view/components/kanji/kanji_search_bar.dart new file mode 100644 index 0000000..1e94bfe --- /dev/null +++ b/lib/view/components/kanji/kanji_search_bar.dart @@ -0,0 +1,88 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart'; + +class KanjiSearchBar extends StatefulWidget { + @override + _KanjiSearchBarState createState() => new _KanjiSearchBarState(); +} + +enum TextFieldButton {clear, paste} + +class _KanjiSearchBarState extends State { + FocusNode focus = new FocusNode(); + TextEditingController textController = new TextEditingController(); + TextFieldButton button = TextFieldButton.paste; + + @override + void initState() { + super.initState(); + focus.addListener(_onFocusChange); + } + + void _getKanjiSuggestions(String text) => + BlocProvider.of(context).add(GetKanjiSuggestions(text)); + + void updateSuggestions() => _getKanjiSuggestions(textController.text); + + void _onFocusChange() { + debugPrint('TextField Focus Changed: ${focus.hasFocus.toString()}'); + + setState(() { + button = focus.hasFocus ? TextFieldButton.clear : TextFieldButton.paste; + }); + + if (focus.hasFocus) + updateSuggestions(); + else + FocusScope.of(context).unfocus(); + } + + void _clearText() { + textController.text = ''; + updateSuggestions(); + } + + void _pasteText() async { + ClipboardData clipboardData = await Clipboard.getData('text/plain'); + textController.text = clipboardData.text; + updateSuggestions(); + } + + @override + Widget build(BuildContext context) { + IconButton clearButton = IconButton( + icon: Icon(Icons.clear), + onPressed: () => _clearText(), + ); + + IconButton pasteButton = IconButton( + icon: Icon(Icons.content_paste), + onPressed: () => _pasteText(), + ); + + return TextField( + focusNode: focus, + controller: textController, + onChanged: (text) => _getKanjiSuggestions(text), + onSubmitted: (text) => + BlocProvider.of(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: (button == TextFieldButton.clear) ? clearButton : pasteButton, + ), + style: TextStyle( + fontSize: 14.0, + ), + ); + } +} \ No newline at end of file diff --git a/lib/view/screens/kanji_search.dart b/lib/view/screens/kanji_search.dart index 1950175..91cacb4 100644 --- a/lib/view/screens/kanji_search.dart +++ b/lib/view/screens/kanji_search.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:jisho_study_tool/bloc/kanji/kanji_bloc.dart'; +import 'package:jisho_study_tool/view/components/kanji/kanji_search_bar.dart'; 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'; @@ -51,7 +51,7 @@ class KanjiViewBar extends StatelessWidget { ), Expanded( child: Container( - child: _KanjiTextField(), + child: KanjiSearchBar(), ), ), IconButton( @@ -66,88 +66,4 @@ class KanjiViewBar extends StatelessWidget { ), ); } -} - -class _KanjiTextField extends StatefulWidget { - @override - _KanjiTextFieldState createState() => new _KanjiTextFieldState(); -} - -enum TextFieldButton {clear, paste} - -class _KanjiTextFieldState extends State<_KanjiTextField> { - FocusNode focus = new FocusNode(); - TextEditingController textController = new TextEditingController(); - TextFieldButton button = TextFieldButton.paste; - - @override - void initState() { - super.initState(); - focus.addListener(_onFocusChange); - } - - void _getKanjiSuggestions(String text) => - BlocProvider.of(context).add(GetKanjiSuggestions(text)); - - void updateSuggestions() => _getKanjiSuggestions(textController.text); - - void _onFocusChange() { - debugPrint('TextField Focus Changed: ${focus.hasFocus.toString()}'); - - setState(() { - button = focus.hasFocus ? TextFieldButton.clear : TextFieldButton.paste; - }); - - if (focus.hasFocus) - updateSuggestions(); - else - FocusScope.of(context).unfocus(); - } - - void _clearText() { - textController.text = ''; - updateSuggestions(); - } - - void _pasteText() async { - ClipboardData clipboardData = await Clipboard.getData('text/plain'); - textController.text = clipboardData.text; - updateSuggestions(); - } - - @override - Widget build(BuildContext context) { - IconButton clearButton = IconButton( - icon: Icon(Icons.clear), - onPressed: () => _clearText(), - ); - - IconButton pasteButton = IconButton( - icon: Icon(Icons.content_paste), - onPressed: () => _pasteText(), - ); - - return TextField( - focusNode: focus, - controller: textController, - onChanged: (text) => _getKanjiSuggestions(text), - onSubmitted: (text) => - BlocProvider.of(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: (button == TextFieldButton.clear) ? clearButton : pasteButton, - ), - style: TextStyle( - fontSize: 14.0, - ), - ); - } -} +} \ No newline at end of file