Skip to content

Commit 3a21cfa

Browse files
authored
Add limit to image_picker_platform_interface (#6434)
Adds limit parameter to `MediaOptions` and `MultiImagePickerOptions`. The `limit` argument defines how many images or media files can be select. Only platform interface package changes taken from: #6201 Fixes: [flutter/flutter#85772](flutter/flutter#85772)
1 parent 6865b2e commit 3a21cfa

File tree

8 files changed

+296
-12
lines changed

8 files changed

+296
-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: 4 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(
@@ -79,6 +80,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
7980
'maxHeight': maxHeight,
8081
'imageQuality': imageQuality,
8182
'requestFullMetadata': requestFullMetadata,
83+
'limit': limit,
8284
},
8385
);
8486
}
@@ -244,6 +246,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
244246
maxHeight: options.imageOptions.maxHeight,
245247
imageQuality: options.imageOptions.imageQuality,
246248
requestFullMetadata: options.imageOptions.requestFullMetadata,
249+
limit: options.limit,
247250
);
248251
if (paths == null) {
249252
return <XFile>[];
@@ -263,6 +266,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
263266
'maxImageHeight': imageOptions.maxHeight,
264267
'imageQuality': imageOptions.imageQuality,
265268
'allowMultiple': options.allowMultiple,
269+
'limit': options.limit,
266270
};
267271

268272
final List<XFile>? paths = await _channel

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,48 @@ 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+
/// This value may be ignored by platforms that cannot support it.
41+
final int? limit;
42+
43+
/// Validates that all values are within required ranges.
44+
///
45+
/// Throws if limit is lower than 2,
46+
/// or allowMultiple is false and limit is not null.
47+
static void _validate({required bool allowMultiple, int? limit}) {
48+
if (!allowMultiple && limit != null) {
49+
throw ArgumentError.value(
50+
allowMultiple,
51+
'allowMultiple',
52+
'cannot be false, when limit is not null',
53+
);
54+
}
55+
56+
if (limit != null && limit < 2) {
57+
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
58+
}
59+
}
2360
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,37 @@ 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+
/// This value may be ignored by platforms that cannot support it.
32+
final int? limit;
33+
34+
/// Validates that all values are within required ranges.
35+
///
36+
/// Throws if limit is lower than 2.
37+
static void _validate({int? limit}) {
38+
if (limit != null && limit < 2) {
39+
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
40+
}
41+
}
1642
}

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
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:image_picker_platform_interface/src/types/types.dart';
7+
8+
void main() {
9+
group('MediaOptions', () {
10+
test('createAndValidate does not throw when allowMultiple is true', () {
11+
expect(
12+
() => MediaOptions.createAndValidate(allowMultiple: true),
13+
returnsNormally,
14+
);
15+
});
16+
test('createAndValidate does not throw when allowMultiple is false', () {
17+
expect(
18+
() => MediaOptions.createAndValidate(allowMultiple: false),
19+
returnsNormally,
20+
);
21+
});
22+
23+
test('createAndValidate does not throw error for correct limit', () {
24+
expect(
25+
() => MediaOptions.createAndValidate(allowMultiple: true, limit: 2),
26+
returnsNormally,
27+
);
28+
});
29+
30+
test('createAndValidate throw error for to small limit', () {
31+
expect(
32+
() => MediaOptions.createAndValidate(allowMultiple: true, limit: 1),
33+
throwsArgumentError,
34+
);
35+
expect(
36+
() => MediaOptions.createAndValidate(allowMultiple: true, limit: 0),
37+
throwsArgumentError,
38+
);
39+
expect(
40+
() => MediaOptions.createAndValidate(allowMultiple: true, limit: -1),
41+
throwsArgumentError,
42+
);
43+
});
44+
45+
test(
46+
'createAndValidate throw error when allowMultiple is false and has limit',
47+
() {
48+
expect(
49+
() => MediaOptions.createAndValidate(allowMultiple: false, limit: 2),
50+
throwsArgumentError,
51+
);
52+
});
53+
});
54+
}

0 commit comments

Comments
 (0)