From 689983b84826a27d89d56b850c839aade469a060 Mon Sep 17 00:00:00 2001 From: h7x4abk3g Date: Wed, 22 Jul 2020 14:13:53 +0200 Subject: [PATCH] Placed keyboard logic inside bloc listener --- lib/components/kanji/kanji_suggestions.dart | 1 - lib/screens/kanji_search.dart | 67 +++++++++++++-------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/components/kanji/kanji_suggestions.dart b/lib/components/kanji/kanji_suggestions.dart index 3c0a044..ade96cb 100644 --- a/lib/components/kanji/kanji_suggestions.dart +++ b/lib/components/kanji/kanji_suggestions.dart @@ -32,7 +32,6 @@ class _Suggestion extends StatelessWidget { Widget build(BuildContext context) { return InkWell( onTap: () { - FocusScope.of(context).unfocus(); //Puts away the keyboard BlocProvider.of(context).add(GetKanji(_kanji)); }, child: Container( diff --git a/lib/screens/kanji_search.dart b/lib/screens/kanji_search.dart index 2a0fc7d..557d071 100644 --- a/lib/screens/kanji_search.dart +++ b/lib/screens/kanji_search.dart @@ -9,23 +9,33 @@ import 'package:jisho_study_tool/components/loading.dart'; class KanjiView extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocBuilder( - 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(context).add(ReturnToInitialState()); - return false; - }); - throw 'No such event found'; + return BlocListener( + listener: (context, state) { + if (state is KanjiSearchInitial) { + FocusScope.of(context).unfocus(); + } else if (state is KanjiSearchLoading) { + FocusScope.of(context).unfocus(); + } }, + child: BlocBuilder( + 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(context) + .add(ReturnToInitialState()); + return false; + }); + throw 'No such event found'; + }, + ), ); } } @@ -38,23 +48,28 @@ class KanjiViewBar extends StatelessWidget { children: [ IconButton( icon: Icon(Icons.arrow_back), - onPressed: () => BlocProvider.of(context) - .add(ReturnToInitialState()), + onPressed: () => + BlocProvider.of(context).add(ReturnToInitialState()), ), Expanded( child: Container( - padding: EdgeInsets.symmetric(vertical: 10.0), child: TextField( - onChanged: (text) => BlocProvider.of(context).add(GetKanjiSuggestions(text)), + onChanged: (text) => BlocProvider.of(context) + .add(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)), + 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), + style: TextStyle( + fontSize: 14.0, ), ), ),