Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update image.dart #36

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/src/image.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import 'dart:math';

class Image {
const Image();

/// Generates a url to a image random image. The size of image is determined
/// by `width` and `height` parameters. You can choose the image categories related
/// by the `keywords` parameter separeted by comma .
/// by the `keywords` parameter separeted by comma . By supplying the `random` parameter
/// you can ensure a different image is returned with the same parameters.
///
/// Example:
/// ```dart
/// faker.image.image(width: 1200, height: 900, keywords: ['people', 'nature']);
/// faker.image.image(width: 1200, height: 900, keywords: ['people', 'nature'], random: true);
/// ```
String image(
{int width = 640, int height = 480, List<String> keywords = const []}) {
return _imageUrl(width, height, keywords);
{int width = 640, int height = 480, List<String> keywords = const [], bool random = false}) {
return _imageUrl(width, height, keywords, random);
}

/// Build a url to a image random image from unsplash
Expand All @@ -20,7 +23,7 @@ class Image {
/// ```dart
/// this._imageUrl(1600, 1200, ['people']);
/// ```
String _imageUrl(int width, int height, List<String> keywords) {
String _imageUrl(int width, int height, List<String> keywords, [bool random = false]) {
var url = 'https://source.unsplash.com';

url += '/${width}x${height}';
Expand All @@ -30,9 +33,15 @@ class Image {
RegExp(r'^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$');
if (keywords.any(keywordFormat.hasMatch)) {
url += '?' + keywords.join(',');
if(random){
url = url += '&random=${Random().nextInt(100) + 1}';
}
}
}else{
if(random){
url = url += '?random=${Random().nextInt(100) + 1}';
}
}

return url;
}
}