import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:tangocard_reader/models/data_entry.dart'; import 'package:tangocard_reader/screens/practise/kanji.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 { late int currentCard; final List _flashcardToggles = [false, false]; final List _kanjiToggles = [false, false]; 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; @override void initState() { currentCard = widget.index; if (isPhone) { if (widget.isKanji) { SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); } else { SystemChrome.setPreferredOrientations([ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ]); } } super.initState(); } @override 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: _kanjiToggles, onPressed: (int index) => setState(() => _kanjiToggles[index] = !_kanjiToggles[index])), ], ), centerTitle: true, ); @override Widget build(BuildContext context) => Scaffold( appBar: widget.isKanji ? kanjiAppBar : flashcardAppBar, body: widget.isKanji ? 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; } }), ) : 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; }), onPreviousCard: () => setState(() { currentCard = isShuffleMode ? randomCard : currentCard - 1; if (currentCard == -1) { currentCard = widget.entries.length - 1; } }), ), ); }