Skip to content
Snippets Groups Projects
Commit 0e386021 authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Pick random images from word (without duplicates)

parent 4dcfdb6a
No related branches found
No related tags found
1 merge request!13Resolve "Display images from randomly picked word"
Pipeline #963 passed
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
......@@ -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();
}
}
......@@ -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),
],
),
......
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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment