From e0df9893590a4c23c9dce26bfc19cebec879ba07 Mon Sep 17 00:00:00 2001 From: h7x4abk3g Date: Fri, 10 Jul 2020 15:53:51 +0200 Subject: [PATCH] Redirect kanji function --- lib/components/kanji/kanji_search_page.dart | 58 ++++++++++----------- lib/screens/kanji_search.dart | 2 +- lib/services/jisho_search.dart | 27 +++++----- lib/services/kanji_search.dart | 23 ++++++++ 4 files changed, 67 insertions(+), 43 deletions(-) diff --git a/lib/components/kanji/kanji_search_page.dart b/lib/components/kanji/kanji_search_page.dart index 638a4d0..a5820ae 100644 --- a/lib/components/kanji/kanji_search_page.dart +++ b/lib/components/kanji/kanji_search_page.dart @@ -23,30 +23,6 @@ class _Header extends StatelessWidget { _Header(this._kanji); } -class _JlptLevel extends StatelessWidget { - final String _jlptLevel; - - @override - Widget build(BuildContext context) { - return Container( - padding: EdgeInsets.all(10.0), - child: Text( - _jlptLevel, - style: TextStyle( - color: Colors.white, - fontSize: 20.0, - ), - ), - decoration: BoxDecoration( - shape: BoxShape.circle, - color: Colors.blue, - ), - ); - } - - _JlptLevel(this._jlptLevel); -} - class _Rank extends StatelessWidget { final int _rank; @@ -71,6 +47,30 @@ class _Rank extends StatelessWidget { _Rank(this._rank); } +class _JlptLevel extends StatelessWidget { + final String _jlptLevel; + + @override + Widget build(BuildContext context) { + return Container( + padding: EdgeInsets.all(10.0), + child: Text( + _jlptLevel, + style: TextStyle( + color: Colors.white, + fontSize: 20.0, + ), + ), + decoration: BoxDecoration( + shape: BoxShape.circle, + color: Colors.blue, + ), + ); + } + + _JlptLevel(this._jlptLevel); +} + class _Grade extends StatelessWidget { final String _grade; @@ -87,7 +87,7 @@ class _Grade extends StatelessWidget { ), decoration: BoxDecoration( color: Colors.blue, - borderRadius: BorderRadius.circular(10.0), + shape: BoxShape.circle, ), ); } @@ -106,7 +106,7 @@ class _Radical extends StatelessWidget { _radical.symbol, style: TextStyle( color: Colors.white, - fontSize: 20.0, + fontSize: 40.0, ), ), decoration: BoxDecoration( @@ -186,19 +186,19 @@ class KanjiResultCard extends StatelessWidget { Row( children: [ Text("JLPT: ", style: TextStyle(fontSize: 20.0)), - _JlptLevel(_result.jlptLevel), + _JlptLevel(_result.jlptLevel ?? "⨉"), ], ), Row( children: [ Text("Grade: ", style: TextStyle(fontSize: 20.0)), - _Grade(_result.taughtIn), + _Grade(_result.taughtIn ?? "⨉"), ], ), Row( children: [ Text("Rank: ", style: TextStyle(fontSize: 20.0)), - _Rank(_result.newspaperFrequencyRank), + _Rank(_result.newspaperFrequencyRank ?? -1), ], ), ], diff --git a/lib/screens/kanji_search.dart b/lib/screens/kanji_search.dart index eaeb72a..b532237 100644 --- a/lib/screens/kanji_search.dart +++ b/lib/screens/kanji_search.dart @@ -4,6 +4,6 @@ import 'package:jisho_study_tool/services/jisho_search.dart'; class KanjiView extends StatelessWidget { @override Widget build(BuildContext context) { - return searchForKanji('拒'); + return searchForKanji('黽'); } } diff --git a/lib/services/jisho_search.dart b/lib/services/jisho_search.dart index 055c546..e10dcab 100644 --- a/lib/services/jisho_search.dart +++ b/lib/services/jisho_search.dart @@ -1,21 +1,22 @@ import 'package:flutter/material.dart'; +import './kanji_search.dart'; import 'package:unofficial_jisho_api/api.dart' as jisho; import 'package:jisho_study_tool/components/kanji/kanji_search_page.dart'; Widget searchForKanji(String kanji) { return FutureBuilder( - future: jisho.searchForKanji(kanji), - builder: - (BuildContext context, AsyncSnapshot snapshot) { - if (snapshot.hasData) { - return KanjiResultCard(snapshot.data); - } else if (snapshot.hasError) { - throw 'ASYNC ERROR'; - } else { - return Center( - child: CircularProgressIndicator(), - ); - } - }); + future: fetchKanji(kanji), + builder: (BuildContext context, AsyncSnapshot snapshot) { + if (snapshot.hasData) { + return KanjiResultCard(snapshot.data); + } else if (snapshot.hasError) { + throw 'ASYNC ERROR'; + } else { + return Center( + child: CircularProgressIndicator(), + ); + } + }, + ); } diff --git a/lib/services/kanji_search.dart b/lib/services/kanji_search.dart index e69de29..ff4b460 100644 --- a/lib/services/kanji_search.dart +++ b/lib/services/kanji_search.dart @@ -0,0 +1,23 @@ +import 'package:unofficial_jisho_api/api.dart' as jisho; + +String _convertGrade(String grade) { + const _conversionTable = { + "grade 1": "小1", + "grade 2": "小2", + "grade 3": "小3", + "grade 4": "小4", + "grade 5": "小5", + "grade 6": "小6", + "junior high": "中" + }; + + print('conversion run: $grade -> ${_conversionTable[grade]}'); + + return _conversionTable[grade]; +} + +Future fetchKanji(String kanji) async { + final result = await jisho.searchForKanji(kanji); + result.taughtIn = _convertGrade(result.taughtIn); + return result; +} \ No newline at end of file