Skip to content
Snippets Groups Projects
Select Git revision
  • ef1a817fc818d94fb2d4a3b8149346fe68899853
  • 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 5.19 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 {
        await pickWord(context, myProvider);
        await pickImage(context, myProvider, myProvider.word);
      }
    
      Future<void> pickWord(BuildContext context, Data myProvider) async {
        List words;
        RandomPickWord randomPickWord;
        int attempts = 0;
        do {
          randomPickWord = RandomPickWord();
          await randomPickWord.init(_count);
          if (randomPickWord.words != null) {
            words = randomPickWord.words;
            break;
          }
          attempts++;
        } while (attempts < 3);
    
        if ((words != null) && (words.length == _count)) {
          myProvider.updateWord = words.take(1).toList()[0];
          myProvider.updateOtherWords = words.skip(1).toList();
        }
      }
    
      Future<void> pickImage(BuildContext context, Data myProvider, String word) async {
        List images;
        RandomPickImage randomPickImage;
        int attempts = 0;
        do {
          randomPickImage = RandomPickImage();
          await randomPickImage.init(word, _count);
          if (randomPickImage.images != null) {
            images = randomPickImage.images;
            break;
          }
          attempts++;
        } while (attempts < 3);
    
    
        if ((images != null) && (images.length == _count)) {
          myProvider.updateImages = images;
        }
      }
    
      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)) {
          images = List(_count);
        }
    
        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(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: () { print("button text: " + word); },
          ),
        );
      }
    
      Column _buildTextItemsBlock(String word, List otherWords) {
        Color color = Colors.white;
    
        List words = [
          word,
          otherWords.length > 0 ? otherWords[0] : null,
          otherWords.length > 1 ? otherWords[1] : null,
          otherWords.length > 2 ? otherWords[2] : null,
        ];
    
        words.shuffle();
    
        return Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTextContainer(words[0], color),
                SizedBox(width: 10),
                _buildTextContainer(words[1], color),
              ],
            ),
            SizedBox(height: 10),
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTextContainer(words[2], color),
                SizedBox(width: 10),
                _buildTextContainer(words[3], color),
              ],
            )
          ],
        );
      }
    
      @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: 10),
                FlatButton(
                  onPressed: () => startGame(context, _myProvider),
                  color: Colors.orange,
                  padding: EdgeInsets.all(10.0),
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.shuffle),
                      Text("Nouveau mot")
                    ],
                  ),
                ),
                SizedBox(height: 10),
                _buildTextItemsBlock(_myProvider.word, _myProvider.otherWords),
              ],
            ),
          ),
        );
      }
    }