Skip to content

Commit c00d8de

Browse files
committed
Add limit to image_picker_platform_interface
1 parent ad0274a commit c00d8de

File tree

6 files changed

+212
-12
lines changed

6 files changed

+212
-12
lines changed

packages/image_picker/image_picker_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.10.0
2+
3+
* Adds limit parameter to `MediaOptions` and `MultiImagePickerOptions`.
4+
15
## 2.9.4
26

37
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.

packages/image_picker/image_picker_platform_interface/lib/src/method_channel/method_channel_image_picker.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
5858
double? maxHeight,
5959
int? imageQuality,
6060
bool requestFullMetadata = true,
61+
int? limit,
6162
}) {
6263
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
6364
throw ArgumentError.value(
@@ -72,13 +73,18 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
7273
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
7374
}
7475

76+
if (limit != null && limit < 2) {
77+
throw ArgumentError.value(limit, 'limit', 'cannot be lower than 2');
78+
}
79+
7580
return _channel.invokeMethod<List<dynamic>?>(
7681
'pickMultiImage',
7782
<String, dynamic>{
7883
'maxWidth': maxWidth,
7984
'maxHeight': maxHeight,
8085
'imageQuality': imageQuality,
8186
'requestFullMetadata': requestFullMetadata,
87+
'limit': limit,
8288
},
8389
);
8490
}
@@ -244,6 +250,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
244250
maxHeight: options.imageOptions.maxHeight,
245251
imageQuality: options.imageOptions.imageQuality,
246252
requestFullMetadata: options.imageOptions.requestFullMetadata,
253+
limit: options.limit,
247254
);
248255
if (paths == null) {
249256
return <XFile>[];
@@ -263,6 +270,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
263270
'maxImageHeight': imageOptions.maxHeight,
264271
'imageQuality': imageOptions.imageQuality,
265272
'allowMultiple': options.allowMultiple,
273+
'limit': options.limit,
266274
};
267275

268276
final List<XFile>? paths = await _channel

packages/image_picker/image_picker_platform_interface/lib/src/types/media_options.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,47 @@ class MediaOptions {
1313
const MediaOptions({
1414
this.imageOptions = const ImageOptions(),
1515
required this.allowMultiple,
16+
this.limit,
1617
});
1718

19+
/// Construct a new MediaOptions instance.
20+
///
21+
/// Throws if limit is lower than 2,
22+
/// or allowMultiple is false and limit is not null
23+
MediaOptions.createAndValidate({
24+
this.imageOptions = const ImageOptions(),
25+
required this.allowMultiple,
26+
this.limit,
27+
}) {
28+
_validate(allowMultiple: allowMultiple, limit: limit);
29+
}
30+
1831
/// Options that will apply to images upon selection.
1932
final ImageOptions imageOptions;
2033

2134
/// Whether to allow for selecting multiple media.
2235
final bool allowMultiple;
36+
37+
/// The maximum number of images to select.
38+
///
39+
/// Default null value means no limit.
40+
final int? limit;
41+
42+
/// Validates that all values are within required ranges.
43+
///
44+
/// Throws if limit is lower than 2,
45+
/// or allowMultiple is false and limit is not null.
46+
static void _validate({required bool allowMultiple, int? limit}) {
47+
if (!allowMultiple && limit != null) {
48+
throw ArgumentError.value(
49+
allowMultiple,
50+
'allowMultiple',
51+
'cannot be false, when limit is not null',
52+
);
53+
}
54+
55+
if (limit != null && limit < 2) {
56+
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
57+
}
58+
}
2359
}

packages/image_picker/image_picker_platform_interface/lib/src/types/multi_image_picker_options.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,36 @@ import 'image_options.dart';
66

77
/// Specifies options for picking multiple images from the device's gallery.
88
class MultiImagePickerOptions {
9-
/// Creates an instance with the given [imageOptions].
9+
/// Creates an instance with the given [imageOptions] and [limit].
1010
const MultiImagePickerOptions({
1111
this.imageOptions = const ImageOptions(),
12+
this.limit,
1213
});
1314

15+
/// Creates an instance with the given [imageOptions] and [limit].
16+
///
17+
/// Throws if limit is lower than 2.
18+
MultiImagePickerOptions.createAndValidate({
19+
this.imageOptions = const ImageOptions(),
20+
this.limit,
21+
}) {
22+
_validate(limit: limit);
23+
}
24+
1425
/// The image-specific options for picking.
1526
final ImageOptions imageOptions;
27+
28+
/// The maximum number of images to select.
29+
///
30+
/// Default null value means no limit.
31+
final int? limit;
32+
33+
/// Validates that all values are within required ranges.
34+
///
35+
/// Throws if limit is lower than 2.
36+
static void _validate({int? limit}) {
37+
if (limit != null && limit < 2) {
38+
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
39+
}
40+
}
1641
}

packages/image_picker/image_picker_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/image_picker/
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.9.4
7+
version: 2.10.0
88

99
environment:
1010
sdk: ^3.3.0

0 commit comments

Comments
 (0)