yokutango-mobile-reader/lib/screens/practise/practise.dart

169 lines
5.2 KiB
Dart
Raw Normal View History

2022-01-29 19:16:13 +01:00
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tangocard_reader/models/data_entry.dart';
2022-02-02 23:45:37 +01:00
import 'package:tangocard_reader/screens/practise/kanji.dart';
2022-01-29 19:16:13 +01:00
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 = [
'頑張れ〜!',
'できるぞ!',
'ヨッシャー!',
2022-02-02 23:45:37 +01:00
'いけいけー!',
2022-01-29 19:16:13 +01:00
];
class _PractiseViewState extends State<PractiseView> {
late int currentCard;
2022-02-02 23:45:37 +01:00
final List<bool> _flashcardToggles = [false, false];
final List<bool> _kanjiToggles = [false, false];
2022-01-29 19:16:13 +01:00
2022-02-02 23:45:37 +01:00
bool get isShuffleMode => _flashcardToggles[0];
bool get isLanguageSwitchedMode => _flashcardToggles[1];
bool get isKanjiDrawingMode => _kanjiToggles[0];
bool get isKanjiAnimationMode => _kanjiToggles[1];
int get randomCard => Random().nextInt(widget.entries.length);
String get randomEncouragingWord =>
encouragingWords[Random().nextInt(encouragingWords.length)];
bool get isPhone =>
MediaQueryData.fromWindow(WidgetsBinding.instance!.window)
.size
.shortestSide <
600;
2022-01-29 19:16:13 +01:00
@override
void initState() {
currentCard = widget.index;
if (isPhone) {
2022-02-02 23:45:37 +01:00
if (widget.isKanji) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
} else {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
2022-01-29 19:16:13 +01:00
}
super.initState();
}
@override
2022-02-02 23:45:37 +01:00
void dispose() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}
AppBar get flashcardAppBar => AppBar(
title: Row(
children: [
Expanded(child: Container()),
Text(randomEncouragingWord),
Expanded(child: Container()),
IconButton(
onPressed: () => setState(() => currentCard = 0),
icon: const Icon(Icons.repeat),
),
ToggleButtons(
selectedColor: Colors.white,
children: const [
Icon(Icons.shuffle),
Icon(Icons.translate),
],
isSelected: _flashcardToggles,
onPressed: (int index) =>
setState(() => _flashcardToggles[index] = !_flashcardToggles[index])),
],
),
centerTitle: true,
);
AppBar get kanjiAppBar => AppBar(
title: Row(
children: [
Expanded(child: Container()),
Text(randomEncouragingWord),
Expanded(child: Container()),
IconButton(
onPressed: () => setState(() => currentCard = 0),
icon: const Icon(Icons.repeat),
),
ToggleButtons(
selectedColor: Colors.white,
children: const [
Icon(Icons.edit),
Icon(Icons.animation),
],
isSelected: _flashcardToggles,
onPressed: (int index) =>
setState(() => _flashcardToggles[index] = !_flashcardToggles[index])),
],
2022-01-29 19:16:13 +01:00
),
2022-02-02 23:45:37 +01:00
centerTitle: true,
);
@override
Widget build(BuildContext context) => Scaffold(
appBar: widget.isKanji ? kanjiAppBar : flashcardAppBar,
2022-01-29 19:16:13 +01:00
body: widget.isKanji
2022-02-02 23:45:37 +01:00
? KanjiPage(
entry: widget.entries[currentCard] as KanjiEntry,
index: currentCard,
showDrawingPanel: isKanjiDrawingMode,
showStrokeOrder: isKanjiAnimationMode,
onNextCard: () => setState(() {
currentCard = isShuffleMode ? randomCard : currentCard + 1;
if (currentCard == widget.entries.length) currentCard = 0;
}),
onPreviousCard: () => setState(() {
currentCard = isShuffleMode ? randomCard : currentCard - 1;
if (currentCard == -1) {
currentCard = widget.entries.length - 1;
}
}),
)
2022-01-29 19:16:13 +01:00
: FlashcardPage(
card: widget.entries[currentCard] as YokutangoEntry,
index: currentCard,
languageFlipped: isLanguageSwitchedMode,
2022-02-02 23:45:37 +01:00
onNextCard: () => setState(() {
currentCard = isShuffleMode ? randomCard : currentCard + 1;
if (currentCard == widget.entries.length) currentCard = 0;
}),
onPreviousCard: () => setState(() {
currentCard = isShuffleMode ? randomCard : currentCard - 1;
if (currentCard == -1) {
currentCard = widget.entries.length - 1;
}
}),
2022-01-29 19:16:13 +01:00
),
);
}