Select Git revision
game_pick_word.dart
-
Benoît Harrault authoredBenoît Harrault authored
game_pick_word.dart 9.08 KiB
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../provider/data.dart';
import '../utils/random_pick_word.dart';
import '../utils/random_pick_image.dart';
class GamePickWordPage extends StatelessWidget {
int _count = 4;
Future<void> startGame(Data myProvider, String lang) async {
await resetGame(myProvider);
myProvider.updateLang = lang;
await nextWord(myProvider);
}
Future<void> resetGame(Data myProvider) async {
myProvider.updateLang = '';
myProvider.updateQuestionsCount = 0;
myProvider.updateGoodAnswers = 0;
myProvider.updateWrongAnswers = 0;
myProvider.updateWord = null;
myProvider.updateImages = null;
}
Future<void> nextWord(Data myProvider) async {
await pickData(myProvider);
myProvider.updateQuestionsCount = myProvider.questionsCount + 1;
}
Future<void> pickData(Data myProvider) async {
List words;
List images;
RandomPickWord randomPickWord;
RandomPickImage randomPickImage;
Map word;
int attempts = 0;
do {
randomPickWord = RandomPickWord();
await randomPickWord.init(myProvider.lang, _count);
if (randomPickWord.words != null) {
words = randomPickWord.words;
word = words.take(1).toList()[0];
randomPickImage = RandomPickImage();
await randomPickImage.init(word['key'], _count);
if (randomPickImage.images != null) {
images = randomPickImage.images;
break;
}
}
attempts++;
} while (attempts < 3);
if (
((words != null) && (words.length == _count))
&&
((images != null) && (images.length == _count))
) {
myProvider.updateWord = word;
myProvider.updateOtherWords = words.skip(1).toList();
myProvider.updateImages = images;
}
}
Future<void> checkWord(Data myProvider, word) async {
if (myProvider.word['key'] == word['key']) {
myProvider.updateGoodAnswers = myProvider.goodAnswers + 1;
nextWord(myProvider);