Skip to content
Snippets Groups Projects
Select Git revision
  • 3cbbcac909db88dfa80c7d1ab24c8d8ce641a3d0
  • master default protected
  • 32-upgrade-framework-and-dependencies
  • 15-improve-app-metadata
  • Release_0.9.0_28 protected
  • Release_0.8.2_27 protected
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.0_24 protected
  • Release_0.6.0_23 protected
  • Release_0.5.0_22 protected
  • Release_0.4.2_21 protected
  • Release_0.4.1_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.1_18 protected
  • Release_0.3.0_17 protected
  • Release_0.2.1_16 protected
  • Release_0.2.0_15 protected
  • Release_0.1.1_14 protected
  • Release_0.1.0_13 protected
  • Release_0.0.12_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
24 results

GeneratedPluginRegistrant.h

Blame
  • 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);
        } else {
          myProvider.updateWrongAnswers = myProvider.wrongAnswers + 1;
        }
      }
    
      Container _buildScoreItemContainer(String text, Color blockColor) {
        Color backgroundColor = blockColor;
    
        // Darken block color to get border color
        double amount = 0.2;
        final hsl = HSLColor.fromColor(blockColor);
        final hslDark = hsl.withLightness((hsl.lightness - amount).clamp(0.0, 1.0));
        Color borderColor = hslDark.toColor();
    
        return Container(
          margin: EdgeInsets.symmetric(horizontal: 15),
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: backgroundColor,
            borderRadius: BorderRadius.circular(4),
            border: Border.all(
              color: borderColor,
              width: 4,
            ),
          ),
          child: Text(
            text,
            style: TextStyle(
              fontSize: 25,
              fontWeight: FontWeight.w600,
              color: Colors.black,
            ),
          ),
        );
      }
    
      Container _buildScoreContainer(Data myProvider) {
        return Container(
          padding: EdgeInsets.all(5),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildScoreItemContainer(
                '❓ ' + myProvider.questionsCount.toString(),
                Colors.white,
              ),
              SizedBox(width: 20),
              _buildScoreItemContainer(
                '☺️ ' + myProvider.goodAnswers.toString(),
                Colors.green,
              ),
              SizedBox(width: 20),
              _buildScoreItemContainer(
                '😟 ' + myProvider.wrongAnswers.toString(),
                Colors.orange,
              ),
            ],
          ),
        );
      }
    
      Container _buildImageContainer(String image) {
        String imageAsset = 'assets/placeholder.png';
        if (image != null) {
            imageAsset = 'assets/images/'+image;
        }
    
        return Container(
          child: FlatButton(
            color: Colors.teal,
            child: Container(
              margin: EdgeInsets.all(2),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                border: Border.all(
                  color: Colors.teal,
                  width: 8,
                ),
              ),
              child: Image(
                image: AssetImage(imageAsset),
                width: 150,
                height: 150,
                fit: BoxFit.fill
              ),
            ),
          ),
        );
      }
    
      Column _buildImageItemsBlock(List images) {
        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]),
                _buildImageContainer(images[1]),
              ],
            ),
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildImageContainer(images[2]),
                _buildImageContainer(images[3]),
              ],
            )
          ],
        );
      }
    
      Container _buildTextContainer(Data myProvider, Map word) {
        return Container(
          child: RaisedButton(
            color: Colors.green,
            padding: EdgeInsets.all(15),
            child: Text(
              word != null ? word[myProvider.lang] : '',
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.w400,
                color: Colors.white,
              ),
            ),
            onPressed: () { checkWord(myProvider, word); },
          ),
        );
      }
    
      Column _buildTextItemsBlock(Data myProvider) {
        Map word = myProvider.word;
        List otherWords = myProvider.otherWords;
    
        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((a, b) => a['key'].compareTo(b['key']));
    
        return Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTextContainer(myProvider, words[0]),
                SizedBox(width: 10),
                _buildTextContainer(myProvider, words[1]),
              ],
            ),
            SizedBox(height: 5),
            Row(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                _buildTextContainer(myProvider, words[2]),
                SizedBox(width: 10),
                _buildTextContainer(myProvider, words[3]),
              ],
            )
          ],
        );
      }
    
      Column _buildStartGameBlock(Data myProvider) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              child: RaisedButton(
                color: Colors.teal,
                padding: EdgeInsets.all(35),
                child: Text(
                  "🇫🇷",
                  style: TextStyle(
                    fontSize: 60,
                    color: Colors.black,
                  ),
                ),
                onPressed: () => startGame(myProvider, 'fr'),
              ),
            ),
            SizedBox(height: 15),
            Container(
              child: RaisedButton(
                color: Colors.teal,
                padding: EdgeInsets.all(35),
                child: Text(
                  "🇬🇧",
                  style: TextStyle(
                    fontSize: 60,
                    color: Colors.black,
                  ),
                ),
                onPressed: () => startGame(myProvider, 'en'),
              ),
            )
          ],
        );
      }
    
      @override
      Widget build(BuildContext context) {
        Data _myProvider = Provider.of<Data>(context);
    
        Scaffold pageContent = Scaffold(
          appBar: AppBar(
            elevation: 0,
            actions: <Widget>[
              IconButton(
                icon: const Icon(Icons.loop),
                onPressed: () => resetGame(_myProvider),
              ),
            ],
          ),
          backgroundColor: Colors.blue,
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                _buildImageItemsBlock(_myProvider.images),
                SizedBox(height: 2),
                ((_myProvider.word == null) || (_myProvider.word['key'] == '')) ?
                _buildStartGameBlock(_myProvider) :
                _buildScoreContainer(_myProvider),
                SizedBox(height: 2),
                _buildTextItemsBlock(_myProvider),
              ],
            ),
          ),
        );
    
        return SizedBox.expand(
          child: Container(
            child: FittedBox(
              fit: BoxFit.contain,
              alignment: Alignment.center,
              child: SizedBox(
                height: (MediaQuery.of(context).size.height),
                width: (MediaQuery.of(context).size.width),
                child: pageContent,
              )
            )
          )
        );
      }
    }