Redirect kanji function

This commit is contained in:
Oystein Kristoffer Tveit 2020-07-10 15:53:51 +02:00
parent 709b43d95d
commit e0df989359
4 changed files with 67 additions and 43 deletions

View File

@ -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),
],
),
],

View File

@ -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('');
}
}

View File

@ -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<jisho.KanjiResult> 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<jisho.KanjiResult> snapshot) {
if (snapshot.hasData) {
return KanjiResultCard(snapshot.data);
} else if (snapshot.hasError) {
throw 'ASYNC ERROR';
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
);
}

View File

@ -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<jisho.KanjiResult> fetchKanji(String kanji) async {
final result = await jisho.searchForKanji(kanji);
result.taughtIn = _convertGrade(result.taughtIn);
return result;
}