From 5d3f3e2c873c471e501026c573b80dcb49acf17c Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sat, 29 Jan 2022 19:16:13 +0100 Subject: [PATCH] add misc kanji utils --- lib/router.dart | 28 +++---- lib/screens/practise/flashcard.dart | 114 +--------------------------- lib/screens/practise/kanji.dart | 43 +++++++++++ lib/screens/practise/practise.dart | 105 +++++++++++++++++++++++++ 4 files changed, 165 insertions(+), 125 deletions(-) create mode 100644 lib/screens/practise/kanji.dart create mode 100644 lib/screens/practise/practise.dart diff --git a/lib/router.dart b/lib/router.dart index 195cf6a..311be63 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -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, - index: benkyouArgs.index, + builder: (_) => PractiseView( + entries: benkyouArgs.cards as List, + index: benkyouArgs.index ?? 0, + isKanji: false, ), ); case '/benkyou/kanji': - // final benkyouArgs = args as BenkyouArgs; - // return MaterialPageRoute( - // builder: (_) => FlashcardView( - // cards: benkyouArgs.cards as List, - // index: benkyouArgs.index, - // ), - // ); + final benkyouArgs = args as BenkyouArgs; + return MaterialPageRoute( + builder: (_) => PractiseView( + entries: benkyouArgs.cards as List, + 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...')), ), ); } diff --git a/lib/screens/practise/flashcard.dart b/lib/screens/practise/flashcard.dart index 04f133e..a6cf223 100644 --- a/lib/screens/practise/flashcard.dart +++ b/lib/screens/practise/flashcard.dart @@ -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 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 { - String title = ''; - int currentCard = 0; - final List _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 { bool isPressed = false; @override diff --git a/lib/screens/practise/kanji.dart b/lib/screens/practise/kanji.dart new file mode 100644 index 0000000..07b2925 --- /dev/null +++ b/lib/screens/practise/kanji.dart @@ -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 { + 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()), + ], + ), + ), + ); + } +} diff --git a/lib/screens/practise/practise.dart b/lib/screens/practise/practise.dart new file mode 100644 index 0000000..cbd7b4c --- /dev/null +++ b/lib/screens/practise/practise.dart @@ -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 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 createState() => _PractiseViewState(); +} + +const encouragingWords = [ + '頑張れ〜!', + 'できるぞ!', + 'ヨッシャー!', + 'いけいけいけー!', +]; + +class _PractiseViewState extends State { + String title = ''; + late int currentCard; + final List _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)]; + }); + }, + ), + ); +}