Skip to content
Snippets Groups Projects
Select Git revision
  • 0c0cfbd00935d3852d05389d78db3fe3424d4753
  • master default protected
  • 94-upgrade-framework-and-dependencies
  • 77-improve-app-metadata
  • 68-add-words
  • 62-fix-get-image-when-word-with-accent
  • 44-implement-game-write-word-from-letters
  • 43-add-script-to-get-images-from-assets
  • 32-add-accents-and-diacritics-in-french-words-2
  • Release_0.10.0_80 protected
  • Release_0.9.2_79 protected
  • Release_0.9.1_78 protected
  • Release_0.9.0_77 protected
  • Release_0.8.0_76 protected
  • Release_0.7.0_75 protected
  • Release_0.6.0_74 protected
  • Release_0.5.2_73 protected
  • Release_0.5.1_72 protected
  • Release_0.5.0_71 protected
  • Release_0.4.1_70 protected
  • Release_0.4.0_69 protected
  • Release_0.3.1_68 protected
  • Release_0.3.0_67 protected
  • Release_0.2.1_66 protected
  • Release_0.2.0_65 protected
  • Release_0.1.40_64 protected
  • Release_0.1.39_63 protected
  • Release_0.1.38_62 protected
  • Release_0.1.37_61 protected
29 results

game_pick_word.dart

Blame
  • game_pick_word.dart 6.87 KiB
    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    
    import '../provider/data.dart';
    import '../utils/random_pick_word.dart';
    import '../utils/get_image_from_word.dart';
    
    class GamePickWordPage extends StatelessWidget {
      int _count = 4;
    
      Future<void> startGame(BuildContext context, Data myProvider) async {
        myProvider.updateQuestionsCount = 0;
        myProvider.updateGoodAnswers = 0;
        myProvider.updateWrongAnswers = 0;
        await nextWord(context, myProvider);
      }
    
      Future<void> nextWord(BuildContext context, Data myProvider) async {
        await pickData(context, myProvider);
        myProvider.updateQuestionsCount = myProvider.questionsCount + 1;
      }
    
      Future<void> pickData(BuildContext context, Data myProvider) async {
        List words;
        List images;
        RandomPickWord randomPickWord;
        RandomPickImage randomPickImage;
        String word;
    
        int attempts = 0;
        do {
          randomPickWord = RandomPickWord();
          await randomPickWord.init(_count);
    
          if (randomPickWord.words != null) {
            words = randomPickWord.words;
            word = words.take(1).toList()[0];
            randomPickImage = RandomPickImage();
            await randomPickImage.init(word, _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(BuildContext context, Data myProvider, word) async {
        if (myProvider.word == word) {
          myProvider.updateGoodAnswers = myProvider.goodAnswers + 1;
          nextWord(context, myProvider);
        } else {
          myProvider.updateWrongAnswers = myProvider.wrongAnswers + 1;
        }
      }
    
      Container _buildScoreContainer(BuildContext context, Data myProvider) {
        String score = ''
          + '❓ ' + myProvider.questionsCount.toString()
          + ' - '
          + '☺️ ' + myProvider.goodAnswers.toString()
          + ' - '
          + '😟 ' + myProvider.wrongAnswers.toString()
          ;
    
        return Container(
          padding: EdgeInsets.all(5),
          child: Text(
            score,
            style: TextStyle(
              fontSize: 20,
              fontWeight: FontWeight.w400,
              color: Colors.black,
            ),
          ),
        );
      }
    
      Container _buildImageContainer(String image, Color color) {
        String imageAsset = 'assets/placeholder.png';
        if (image != null) {
            imageAsset = 'assets/images/'+image;
        }
    
        return Container(
          padding: EdgeInsets.all(5),
          child: FlatButton(
            color: Colors.teal,
            child: Image(image: AssetImage(imageAsset)),
          ),
        );
      }
    
      Column _buildImageItemsBlock(List images) {
        Color color = Colors.black;
    
        if ((images == null) || (images.length != _count)) {
          return Column();
        }
    
        return Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildImageContainer(images[0], color),
                _buildImageContainer(images[1], color),
              ],
            ),
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildImageContainer(images[2], color),
                _buildImageContainer(images[3], color),
              ],
            )
          ],
        );
      }
    
      Container _buildTextContainer(BuildContext context, Data myProvider, String word, Color color) {
        return Container(
          child: RaisedButton(
            color: Colors.green,
            padding: EdgeInsets.all(15),
            child: Text(
              word != null ? word : '',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.w400,
                color: color,
              ),
            ),
            onPressed: () { checkWord(context, myProvider, word); },
          ),
        );
      }
    
      Column _buildTextItemsBlock(BuildContext context, Data myProvider, String word, List otherWords) {
        Color color = Colors.white;
    
        if ((word == null) || (otherWords.length != (_count - 1))) {
          return Column();
        }
    
        List words = [
          word,
          otherWords.length > 0 ? otherWords[0] : null,
          otherWords.length > 1 ? otherWords[1] : null,
          otherWords.length > 2 ? otherWords[2] : null,
        ];
    
        words.sort();
    
        return Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTextContainer(context, myProvider, words[0], color),
                SizedBox(width: 10),
                _buildTextContainer(context, myProvider, words[1], color),
              ],
            ),
            SizedBox(height: 10),
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTextContainer(context, myProvider, words[2], color),
                SizedBox(width: 10),
                _buildTextContainer(context, myProvider, words[3], color),
              ],
            )
          ],
        );
      }
    
      Row _buildStartGameButton(BuildContext context, Data myProvider) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              child: RaisedButton(
                color: Colors.orange,
                padding: EdgeInsets.all(35),
                child: Text(
                  "🔀",
                  style: TextStyle(
                    fontSize: 60,
                    fontWeight: FontWeight.w600,
                    color: Colors.black,
                  ),
                ),
                onPressed: () => startGame(context, myProvider),
              ),
            )
          ],
        );
      }
    
      @override
      Widget build(BuildContext context) {
        Data _myProvider = Provider.of<Data>(context);
    
        return Scaffold(
          appBar: AppBar(
            elevation: 0,
          ),
          backgroundColor: Colors.blue,
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                _buildImageItemsBlock(_myProvider.images),
                SizedBox(height: 5),
                _buildScoreContainer(context, _myProvider),
                ((_myProvider.word == null) || (_myProvider.word == '')) ?
                _buildStartGameButton(context, _myProvider) :
                SizedBox(height: 5),
                _buildTextItemsBlock(context, _myProvider, _myProvider.word, _myProvider.otherWords),
              ],
            ),
          ),
        );
      }
    }