From 289592cda5ca1218da261ead7ab2009271cf2be3 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 23 Jan 2022 21:29:18 +0100 Subject: [PATCH] Connect multiple kanji parts with search --- lib/components/kanji/kanji_result_body.dart | 2 +- .../kanji/kanji_result_body/examples.dart | 10 +++- .../kanji/kanji_result_body/header.dart | 9 ++-- .../kanji/kanji_result_body/radical.dart | 26 ++++++---- .../kanji/kanji_result_body/yomi_chips.dart | 51 ++++++++++++------- lib/routing/router.dart | 3 +- .../search_mechanisms/radical_list.dart | 13 ++++- 7 files changed, 77 insertions(+), 37 deletions(-) diff --git a/lib/components/kanji/kanji_result_body.dart b/lib/components/kanji/kanji_result_body.dart index ad29039..4f8fd9e 100644 --- a/lib/components/kanji/kanji_result_body.dart +++ b/lib/components/kanji/kanji_result_body.dart @@ -31,7 +31,7 @@ class KanjiResultBody extends StatelessWidget { children: [ const Flexible( fit: FlexFit.tight, - child: Center(child: SizedBox()), + child: SizedBox(), ), Flexible( fit: FlexFit.tight, diff --git a/lib/components/kanji/kanji_result_body/examples.dart b/lib/components/kanji/kanji_result_body/examples.dart index 0d694b5..9b3d923 100644 --- a/lib/components/kanji/kanji_result_body/examples.dart +++ b/lib/components/kanji/kanji_result_body/examples.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:unofficial_jisho_api/api.dart'; import '../../../bloc/theme/theme_bloc.dart'; +import '../../../routing/routes.dart'; import '../../../services/romaji_transliteration.dart'; import '../../../settings.dart'; @@ -73,7 +74,14 @@ class _Example extends StatelessWidget { child: IntrinsicHeight( child: Row( children: [ - _Kana(colors: kanaColors, example: yomiExample), + InkWell( + onTap: () => Navigator.pushNamed( + context, + Routes.search, + arguments: yomiExample.example, + ), + child: _Kana(colors: kanaColors, example: yomiExample), + ), _ExampleText(colors: menuColors, example: yomiExample) ], ), diff --git a/lib/components/kanji/kanji_result_body/header.dart b/lib/components/kanji/kanji_result_body/header.dart index 7df084d..8d32c7c 100644 --- a/lib/components/kanji/kanji_result_body/header.dart +++ b/lib/components/kanji/kanji_result_body/header.dart @@ -18,15 +18,14 @@ class Header extends StatelessWidget { final colors = state.theme.kanjiResultColor; return Container( + alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10.0), color: colors.background, ), - child: Center( - child: Text( - kanji, - style: TextStyle(fontSize: 70.0, color: colors.foreground), - ), + child: Text( + kanji, + style: TextStyle(fontSize: 70.0, color: colors.foreground), ), ); }, diff --git a/lib/components/kanji/kanji_result_body/radical.dart b/lib/components/kanji/kanji_result_body/radical.dart index 75aac51..8cfcb81 100644 --- a/lib/components/kanji/kanji_result_body/radical.dart +++ b/lib/components/kanji/kanji_result_body/radical.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:unofficial_jisho_api/api.dart' as jisho; import '../../../bloc/theme/theme_bloc.dart'; +import '../../../routing/routes.dart'; class Radical extends StatelessWidget { final jisho.Radical radical; @@ -16,17 +17,20 @@ class Radical extends StatelessWidget { builder: (context, state) { final colors = state.theme.kanjiResultColor; - return Container( - padding: const EdgeInsets.all(15.0), - decoration: BoxDecoration( - shape: BoxShape.circle, - color: colors.background, - ), - child: Text( - radical.symbol, - style: TextStyle( - color: colors.foreground, - fontSize: 40.0, + return InkWell( + onTap: () => Navigator.pushNamed(context, Routes.kanjiSearchRadicals, arguments: radical.symbol), + child: Container( + padding: const EdgeInsets.all(15.0), + decoration: BoxDecoration( + shape: BoxShape.circle, + color: colors.background, + ), + child: Text( + radical.symbol, + style: TextStyle( + color: colors.foreground, + fontSize: 40.0, + ), ), ), ); diff --git a/lib/components/kanji/kanji_result_body/yomi_chips.dart b/lib/components/kanji/kanji_result_body/yomi_chips.dart index 8918f40..ab77cbd 100644 --- a/lib/components/kanji/kanji_result_body/yomi_chips.dart +++ b/lib/components/kanji/kanji_result_body/yomi_chips.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import '../../../bloc/theme/theme_bloc.dart'; +import '../../../routing/routes.dart'; import '../../../services/romaji_transliteration.dart'; import '../../../settings.dart'; @@ -50,24 +51,29 @@ class YomiChips extends StatelessWidget { bool get isExpandable => yomi.length > 6; Widget yomiCard({ + required BuildContext context, required String yomi, required ColorSet colors, }) => - Container( - margin: const EdgeInsets.symmetric(horizontal: 5), - padding: const EdgeInsets.symmetric( - vertical: 10.0, - horizontal: 10.0, - ), - decoration: BoxDecoration( - color: colors.background, - borderRadius: BorderRadius.circular(10.0), - ), - child: Text( - yomi, - style: TextStyle( - fontSize: 20.0, - color: colors.foreground, + InkWell( + onTap: () => + Navigator.pushNamed(context, Routes.search, arguments: yomi), + child: Container( + margin: const EdgeInsets.symmetric(horizontal: 5), + padding: const EdgeInsets.symmetric( + vertical: 10.0, + horizontal: 10.0, + ), + decoration: BoxDecoration( + color: colors.background, + borderRadius: BorderRadius.circular(10.0), + ), + child: Text( + yomi, + style: TextStyle( + fontSize: 20.0, + color: colors.foreground, + ), ), ), ); @@ -75,12 +81,19 @@ class YomiChips extends StatelessWidget { Widget yomiWrapper(BuildContext context) { final yomiCards = yomi .map((y) => romajiEnabled ? transliterateKanaToLatin(y) : y) - .map((y) => yomiCard(yomi: y, colors: type.getColors(context))) + .map( + (y) => yomiCard( + context: context, + yomi: y, + colors: type.getColors(context), + ), + ) .toList(); final yomiCardsWithTitle = [ if (type != YomiType.meaning) yomiCard( + context: context, yomi: type == YomiType.kunyomi ? 'Kun:' : 'On:', colors: ColorSet( foreground: type.getColors(context).background, @@ -101,7 +114,11 @@ class YomiChips extends StatelessWidget { else return ExpansionTile( title: Center( - child: yomiCard(yomi: type.title, colors: type.getColors(context)), + child: yomiCard( + context: context, + yomi: type.title, + colors: type.getColors(context), + ), ), children: [ const SizedBox(height: 20.0), diff --git a/lib/routing/router.dart b/lib/routing/router.dart index 4a1e50b..7e45508 100644 --- a/lib/routing/router.dart +++ b/lib/routing/router.dart @@ -34,7 +34,8 @@ Route generateRoute(RouteSettings settings) { return MaterialPageRoute(builder: (_) => const KanjiGradeSearch()); case Routes.kanjiSearchRadicals: - return MaterialPageRoute(builder: (_) => const KanjiRadicalSearch()); + final prechosenRadical = args as String?; + return MaterialPageRoute(builder: (_) => KanjiRadicalSearch(prechosenRadical: prechosenRadical)); // TODO: Add more specific error screens. case Routes.errorNotFound: diff --git a/lib/screens/search/search_mechanisms/radical_list.dart b/lib/screens/search/search_mechanisms/radical_list.dart index a1a48c6..bac84f6 100644 --- a/lib/screens/search/search_mechanisms/radical_list.dart +++ b/lib/screens/search/search_mechanisms/radical_list.dart @@ -6,7 +6,9 @@ import '../../../../routing/routes.dart'; import '../../../../services/jisho_api/radicals_search.dart'; class KanjiRadicalSearch extends StatefulWidget { - const KanjiRadicalSearch({Key? key}) : super(key: key); + final String? prechosenRadical; + + const KanjiRadicalSearch({Key? key, this.prechosenRadical}) : super(key: key); @override _KanjiRadicalSearchState createState() => _KanjiRadicalSearchState(); @@ -25,6 +27,15 @@ class _KanjiRadicalSearchState extends State { for (final String r in radicals.values.expand((l) => l)) r: true }; + @override + void initState() { + if (widget.prechosenRadical != null && + radicalToggles.containsKey(widget.prechosenRadical)) + radicalToggles[widget.prechosenRadical!] = true; + updateSuggestions(); + super.initState(); + } + void resetRadicalToggles() => radicalToggles.forEach((k, _) { radicalToggles[k] = false; });