add misc kanji utils

This commit is contained in:
Oystein Kristoffer Tveit 2022-01-29 19:16:13 +01:00
parent c1ccbc00b3
commit 5d3f3e2c87
4 changed files with 165 additions and 125 deletions

View File

@ -2,10 +2,10 @@ import 'dart:io';
import 'package:tangocard_reader/models/data_entry.dart';
import 'package:tangocard_reader/models/router_args.dart';
import 'package:tangocard_reader/screens/practise/flashcard.dart';
import 'package:tangocard_reader/screens/home.dart';
import 'package:tangocard_reader/screens/pages/tango_list.dart';
import 'package:flutter/material.dart';
import 'package:tangocard_reader/screens/practise/practise.dart';
import 'screens/pages/kanji_list.dart';
@ -28,28 +28,28 @@ class PageRouter {
case '/benkyou/tango':
final benkyouArgs = args as BenkyouArgs;
return MaterialPageRoute(
builder: (_) => FlashcardView(
cards: benkyouArgs.cards as List<YokutangoEntry>,
index: benkyouArgs.index,
builder: (_) => PractiseView(
entries: benkyouArgs.cards as List<YokutangoEntry>,
index: benkyouArgs.index ?? 0,
isKanji: false,
),
);
case '/benkyou/kanji':
// final benkyouArgs = args as BenkyouArgs;
// return MaterialPageRoute(
// builder: (_) => FlashcardView(
// cards: benkyouArgs.cards as List<YokutangoEntry>,
// index: benkyouArgs.index,
// ),
// );
final benkyouArgs = args as BenkyouArgs;
return MaterialPageRoute(
builder: (_) => PractiseView(
entries: benkyouArgs.cards as List<KanjiEntry>,
index: benkyouArgs.index ?? 0,
isKanji: true,
),
);
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
appBar: AppBar(title: const Text('Error')),
body: Center(
child: ErrorWidget('No such route...')
),
body: Center(child: ErrorWidget('No such route...')),
),
);
}

View File

@ -1,123 +1,15 @@
import 'dart:math';
import 'package:tangocard_reader/components/flashcard.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tangocard_reader/models/data_entry.dart';
class FlashcardView extends StatefulWidget {
final List<YokutangoEntry> cards;
final int? index;
const FlashcardView({
required this.cards,
this.index,
Key? key,
}) : super(key: key);
@override
_FlashcardViewState createState() => _FlashcardViewState();
}
const encouragingWords = [
'頑張れ〜!',
'できるぞ!',
'ヨッシャー!',
'いけいけいけー!',
];
class _FlashcardViewState extends State<FlashcardView> {
String title = '';
int currentCard = 0;
final List<bool> _isSelected = [false, false];
get isShuffleMode => _isSelected[0];
get isLanguageSwitchedMode => _isSelected[1];
get randomCard => Random().nextInt(widget.cards.length);
@override
void initState() {
title = encouragingWords[Random().nextInt(encouragingWords.length)];
currentCard = widget.index ?? 0;
final isPhone = MediaQueryData.fromWindow(WidgetsBinding.instance!.window)
.size
.shortestSide <
600;
if (isPhone) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
super.initState();
}
@override
void dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
children: [
Expanded(child: Container()),
Text(title),
Expanded(child: Container()),
IconButton(
onPressed: () => setState(() {
currentCard = 0;
}),
icon: const Icon(Icons.repeat),
),
ToggleButtons(
children: const [
Icon(Icons.shuffle),
Icon(Icons.translate),
],
isSelected: _isSelected,
onPressed: (int index) {
setState(() {
_isSelected[index] = !_isSelected[index];
});
})
],
),
centerTitle: true,
),
body: _FlashcardPage(
card: widget.cards[currentCard],
index: currentCard,
languageFlipped: isLanguageSwitchedMode,
onNextCard: () {
setState(() {
currentCard = isShuffleMode ? randomCard : currentCard + 1;
if (currentCard == widget.cards.length) currentCard = 0;
title = encouragingWords[Random().nextInt(encouragingWords.length)];
});
},
),
);
}
}
class _FlashcardPage extends StatefulWidget {
class FlashcardPage extends StatefulWidget {
final YokutangoEntry card;
final Function() onNextCard;
final bool languageFlipped;
final int? index;
const _FlashcardPage({
const FlashcardPage({
required this.card,
required this.onNextCard,
this.languageFlipped = false,
@ -129,7 +21,7 @@ class _FlashcardPage extends StatefulWidget {
_FlashcardPageState createState() => _FlashcardPageState();
}
class _FlashcardPageState extends State<_FlashcardPage> {
class _FlashcardPageState extends State<FlashcardPage> {
bool isPressed = false;
@override

View File

@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import '../../models/data_entry.dart';
class KanjiPage extends StatefulWidget {
final KanjiEntry entry;
final Function() onNextCard;
final int? index;
const KanjiPage({
required this.entry,
required this.onNextCard,
this.index,
Key? key,
}) : super(key: key);
@override
_KanjiPageState createState() => _KanjiPageState();
}
class _KanjiPageState extends State<KanjiPage> {
bool isPressed = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (isPressed) widget.onNextCard();
setState(() => isPressed = !isPressed);
},
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(widget.entry.kanji),
const SizedBox(width: 40),
Text(widget.entry.kana.toString()),
],
),
),
);
}
}

View File

@ -0,0 +1,105 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tangocard_reader/models/data_entry.dart';
import 'flashcard.dart';
class PractiseView extends StatefulWidget {
final List<DataEntry> entries;
final bool isKanji;
final int index;
const PractiseView({
Key? key,
required this.entries,
required this.isKanji,
this.index = 0,
}) : super(key: key);
@override
State<PractiseView> createState() => _PractiseViewState();
}
const encouragingWords = [
'頑張れ〜!',
'できるぞ!',
'ヨッシャー!',
'いけいけいけー!',
];
class _PractiseViewState extends State<PractiseView> {
String title = '';
late int currentCard;
final List<bool> _isSelected = [false, false];
get isShuffleMode => _isSelected[0];
get isLanguageSwitchedMode => _isSelected[1];
get randomCard => Random().nextInt(widget.entries.length);
@override
void initState() {
title = encouragingWords[Random().nextInt(encouragingWords.length)];
currentCard = widget.index;
final isPhone = MediaQueryData.fromWindow(WidgetsBinding.instance!.window)
.size
.shortestSide <
600;
if (isPhone) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
super.initState();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Row(
children: [
Expanded(child: Container()),
Text(title),
Expanded(child: Container()),
IconButton(
onPressed: () => setState(() {
currentCard = 0;
}),
icon: const Icon(Icons.repeat),
),
ToggleButtons(
children: const [
Icon(Icons.shuffle),
Icon(Icons.translate),
],
isSelected: _isSelected,
onPressed: (int index) {
setState(() {
_isSelected[index] = !_isSelected[index];
});
})
],
),
centerTitle: true,
),
body: widget.isKanji
? Container()
: FlashcardPage(
card: widget.entries[currentCard] as YokutangoEntry,
index: currentCard,
languageFlipped: isLanguageSwitchedMode,
onNextCard: () {
setState(() {
currentCard = isShuffleMode ? randomCard : currentCard + 1;
if (currentCard == widget.entries.length) currentCard = 0;
title = encouragingWords[
Random().nextInt(encouragingWords.length)];
});
},
),
);
}