Skip to content

Commit

Permalink
Add limit to image_picker_platform_interface (#6434)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
pdenert authored Apr 7, 2024
1 parent 6865b2e commit 3a21cfa
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.10.0

* Adds limit parameter to `MediaOptions` and `MultiImagePickerOptions`.

## 2.9.4

* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
double? maxHeight,
int? imageQuality,
bool requestFullMetadata = true,
int? limit,
}) {
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
Expand All @@ -79,6 +80,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
'maxHeight': maxHeight,
'imageQuality': imageQuality,
'requestFullMetadata': requestFullMetadata,
'limit': limit,
},
);
}
Expand Down Expand Up @@ -244,6 +246,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
maxHeight: options.imageOptions.maxHeight,
imageQuality: options.imageOptions.imageQuality,
requestFullMetadata: options.imageOptions.requestFullMetadata,
limit: options.limit,
);
if (paths == null) {
return <XFile>[];
Expand All @@ -263,6 +266,7 @@ class MethodChannelImagePicker extends ImagePickerPlatform {
'maxImageHeight': imageOptions.maxHeight,
'imageQuality': imageOptions.imageQuality,
'allowMultiple': options.allowMultiple,
'limit': options.limit,
};

final List<XFile>? paths = await _channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,48 @@ class MediaOptions {
const MediaOptions({
this.imageOptions = const ImageOptions(),
required this.allowMultiple,
this.limit,
});

/// Construct a new MediaOptions instance.
///
/// Throws if limit is lower than 2,
/// or allowMultiple is false and limit is not null.
MediaOptions.createAndValidate({
this.imageOptions = const ImageOptions(),
required this.allowMultiple,
this.limit,
}) {
_validate(allowMultiple: allowMultiple, limit: limit);
}

/// Options that will apply to images upon selection.
final ImageOptions imageOptions;

/// Whether to allow for selecting multiple media.
final bool allowMultiple;

/// The maximum number of images to select.
///
/// Default null value means no limit.
/// This value may be ignored by platforms that cannot support it.
final int? limit;

/// Validates that all values are within required ranges.
///
/// Throws if limit is lower than 2,
/// or allowMultiple is false and limit is not null.
static void _validate({required bool allowMultiple, int? limit}) {
if (!allowMultiple && limit != null) {
throw ArgumentError.value(
allowMultiple,
'allowMultiple',
'cannot be false, when limit is not null',
);
}

if (limit != null && limit < 2) {
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,37 @@ import 'image_options.dart';

/// Specifies options for picking multiple images from the device's gallery.
class MultiImagePickerOptions {
/// Creates an instance with the given [imageOptions].
/// Creates an instance with the given [imageOptions] and [limit].
const MultiImagePickerOptions({
this.imageOptions = const ImageOptions(),
this.limit,
});

/// Creates an instance with the given [imageOptions] and [limit].
///
/// Throws if limit is lower than 2.
MultiImagePickerOptions.createAndValidate({
this.imageOptions = const ImageOptions(),
this.limit,
}) {
_validate(limit: limit);
}

/// The image-specific options for picking.
final ImageOptions imageOptions;

/// The maximum number of images to select.
///
/// Default null value means no limit.
/// This value may be ignored by platforms that cannot support it.
final int? limit;

/// Validates that all values are within required ranges.
///
/// Throws if limit is lower than 2.
static void _validate({int? limit}) {
if (limit != null && limit < 2) {
throw ArgumentError.value(limit, 'limit', 'cannot be lower then 2');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/image_picker/
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.9.4
version: 2.10.0

environment:
sdk: ^3.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:image_picker_platform_interface/src/types/types.dart';

void main() {
group('MediaOptions', () {
test('createAndValidate does not throw when allowMultiple is true', () {
expect(
() => MediaOptions.createAndValidate(allowMultiple: true),
returnsNormally,
);
});
test('createAndValidate does not throw when allowMultiple is false', () {
expect(
() => MediaOptions.createAndValidate(allowMultiple: false),
returnsNormally,
);
});

test('createAndValidate does not throw error for correct limit', () {
expect(
() => MediaOptions.createAndValidate(allowMultiple: true, limit: 2),
returnsNormally,
);
});

test('createAndValidate throw error for to small limit', () {
expect(
() => MediaOptions.createAndValidate(allowMultiple: true, limit: 1),
throwsArgumentError,
);
expect(
() => MediaOptions.createAndValidate(allowMultiple: true, limit: 0),
throwsArgumentError,
);
expect(
() => MediaOptions.createAndValidate(allowMultiple: true, limit: -1),
throwsArgumentError,
);
});

test(
'createAndValidate throw error when allowMultiple is false and has limit',
() {
expect(
() => MediaOptions.createAndValidate(allowMultiple: false, limit: 2),
throwsArgumentError,
);
});
});
}
Loading

0 comments on commit 3a21cfa

Please sign in to comment.