diff --git a/android/gradle.properties b/android/gradle.properties index f0be9fb67d6fe0b36ce90df03ff2f3f1551d738c..d9abd55731010fe508f39321892e8002f10e79ef 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,5 +1,5 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true -app.versionName=0.0.11 -app.versionCode=11 +app.versionName=0.0.12 +app.versionCode=12 diff --git a/lib/provider/data.dart b/lib/provider/data.dart index a1f00f7cefee1388c37c10bff10a5df2ce777b5f..3d71898e68d70db200cb86c619249a57d9b01edc 100644 --- a/lib/provider/data.dart +++ b/lib/provider/data.dart @@ -5,12 +5,7 @@ class Data extends ChangeNotifier { // randomization String _word = ''; List _otherWords = []; - Map<String, String> _image = { - '1': null, - '2': null, - '3': null, - '4': null, - }; + List _images = []; String get word => _word; @@ -26,18 +21,17 @@ class Data extends ChangeNotifier { notifyListeners(); } - Map<String, String> get image => _image; + List get images => _images; - set updateImage(Map<String, String> image) { - for (var key in image.keys) { - _image[key.toString()] = image[key]; - } + set updateImages(List images) { + _images = images; notifyListeners(); } void resetGame() { _word = ''; _otherWords = []; + _images = []; notifyListeners(); } } diff --git a/lib/screens/game_pick_word.dart b/lib/screens/game_pick_word.dart index 179d9674fd0a8c8ad486c6305334b1fca37fae13..fd0f791e6abfb742c49ddacc94b9871f1bcd69d4 100644 --- a/lib/screens/game_pick_word.dart +++ b/lib/screens/game_pick_word.dart @@ -10,10 +10,7 @@ class GamePickWordPage extends StatelessWidget { Future<void> startGame(BuildContext context, Data myProvider) async { await pickWord(context, myProvider); - await pickImage(context, myProvider, myProvider.word, 1); - await pickImage(context, myProvider, myProvider.word, 2); - await pickImage(context, myProvider, myProvider.word, 3); - await pickImage(context, myProvider, myProvider.word, 4); + await pickImage(context, myProvider, myProvider.word); } Future<void> pickWord(BuildContext context, Data myProvider) async { @@ -36,18 +33,24 @@ class GamePickWordPage extends StatelessWidget { } } - Future<void> pickImage(BuildContext context, Data myProvider, String word, int index) async { + Future<void> pickImage(BuildContext context, Data myProvider, String word) async { + List images; RandomPickImage randomPickImage; int attempts = 0; do { randomPickImage = RandomPickImage(); - await randomPickImage.init(word); - if (randomPickImage.image != null) { - myProvider.updateImage = {index.toString(): randomPickImage.image}; + 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) { @@ -57,7 +60,7 @@ class GamePickWordPage extends StatelessWidget { } return Container( - padding: EdgeInsets.all(10), + padding: EdgeInsets.all(5), child: FlatButton( color: Colors.teal, child: Image(image: AssetImage(imageAsset)), @@ -65,9 +68,13 @@ class GamePickWordPage extends StatelessWidget { ); } - Column _buildImageItemsBlock(Map<String, String> image) { + 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, @@ -76,16 +83,16 @@ class GamePickWordPage extends StatelessWidget { mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - _buildImageContainer(image['1'], color), - _buildImageContainer(image['2'], color), + _buildImageContainer(images[0], color), + _buildImageContainer(images[1], color), ], ), Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - _buildImageContainer(image['3'], color), - _buildImageContainer(image['4'], color), + _buildImageContainer(images[2], color), + _buildImageContainer(images[3], color), ], ) ], @@ -159,8 +166,8 @@ class GamePickWordPage extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.max, children: <Widget>[ - _buildImageItemsBlock(_myProvider.image), - SizedBox(height: 20), + _buildImageItemsBlock(_myProvider.images), + SizedBox(height: 10), FlatButton( onPressed: () => startGame(context, _myProvider), color: Colors.orange, @@ -172,7 +179,7 @@ class GamePickWordPage extends StatelessWidget { ], ), ), - SizedBox(height: 20), + SizedBox(height: 10), _buildTextItemsBlock(_myProvider.word, _myProvider.otherWords), ], ), diff --git a/lib/utils/get_image_from_word.dart b/lib/utils/get_image_from_word.dart index a76c8fb3b2d9b538b5ea1427eb5310a1b0d675b6..9465c382af9517c9062a180f4d09107919737a3a 100644 --- a/lib/utils/get_image_from_word.dart +++ b/lib/utils/get_image_from_word.dart @@ -1,30 +1,38 @@ import 'dart:async'; import 'dart:convert'; import 'package:flutter/services.dart'; -import 'dart:math' show Random; class RandomPickImage { RandomPickImage(); - String _image; - final random = Random(); + List _images; - init(String word) async { - await imageFromLocalFile(word); + init(String word, int count) async { + await imageFromLocalFile(word, count); } - Future<void> imageFromLocalFile(String word) async { - String jsonString; + Future<void> imageFromLocalFile(String word, int count) async { + // Get all images for this word + List imageList; try { - jsonString = await rootBundle.loadString('assets/assets_images.json'); + String jsonString = await rootBundle.loadString('assets/assets_images.json'); final jsonResponse = await json.decode(jsonString); - var imageList = jsonResponse['images'][word]; - String image = imageList[random.nextInt(imageList.length)]; - _image = image ?? 'UNEXPECTED ERROR'; + imageList = jsonResponse['images'][word]; } catch (e) { - _image = 'UNEXPECTED ERROR'; + print("$e"); } + + // Check we have enough images + if (imageList.length < count) { + print('Not enough images in list for word "'+word+'".'); + } + + // Randomize images list + imageList.shuffle(); + + // Pick first images from shuffled list + _images = imageList.take(count).toList(); } - String get image => _image; + List get images => _images; }