import 'dart:math'; import 'package:tangocard_reader/components/flashcard.dart'; import 'package:tangocard_reader/models/yokutango_entry.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.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 { final YokutangoEntry card; final Function() onNextCard; final bool languageFlipped; final int? index; const _FlashcardPage({ required this.card, required this.onNextCard, this.languageFlipped = false, this.index, Key? key, }) : super(key: key); @override _FlashcardPageState createState() => _FlashcardPageState(); } class _FlashcardPageState extends State<_FlashcardPage> { bool isPressed = false; @override Widget build(BuildContext context) { return GestureDetector( onTap: () { if (isPressed) { widget.onNextCard(); } setState(() { isPressed = !isPressed; }); }, child: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Flashcard( card: widget.card, cardIndex: widget.index, isLeftSide: true, languageFlipped: widget.languageFlipped, ), const SizedBox(width: 40), Flashcard( card: isPressed ? widget.card : null, cardIndex: widget.index, languageFlipped: widget.languageFlipped, ), ], ), ), ); } }