Skip to content

Commit

Permalink
Added additional ImagePicker configuration to the FormBuilderImagePic…
Browse files Browse the repository at this point in the history
…ker. (#314)
  • Loading branch information
awhitford committed Jun 13, 2020
1 parent f27af78 commit 68981fd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/src/fields/form_builder_image_picker.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_form_builder/src/widgets/image_source_sheet.dart';
import 'package:image_picker/image_picker.dart';

class FormBuilderImagePicker extends StatefulWidget {
final String attribute;
Expand All @@ -19,6 +20,23 @@ class FormBuilderImagePicker extends StatefulWidget {
final EdgeInsets imageMargin;
final Color iconColor;

/// Optional maximum height of image; see [ImagePicker].
final double maxHeight;

/// Optional maximum width of image; see [ImagePicker].
final double maxWidth;

/// The imageQuality argument modifies the quality of the image, ranging from
/// 0-100 where 100 is the original/max quality. If imageQuality is null, the
/// image with the original quality will be returned. See [ImagePicker].
final int imageQuality;

/// Use preferredCameraDevice to specify the camera to use when the source is
/// `ImageSource.camera`. The preferredCameraDevice is ignored when source is
/// `ImageSource.gallery`. It is also ignored if the chosen camera is not
/// supported on the device. Defaults to `CameraDevice.rear`. See [ImagePicker].
final CameraDevice preferredCameraDevice;

const FormBuilderImagePicker({
Key key,
@required this.attribute,
Expand All @@ -34,6 +52,10 @@ class FormBuilderImagePicker extends StatefulWidget {
this.onSaved,
this.decoration = const InputDecoration(),
this.iconColor,
this.maxHeight,
this.maxWidth,
this.imageQuality,
this.preferredCameraDevice = CameraDevice.rear,
}) : super(key: key);

@override
Expand Down Expand Up @@ -161,6 +183,11 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
context: context,
builder: (_) {
return ImageSourceSheet(
maxHeight: widget.maxHeight,
maxWidth: widget.maxWidth,
imageQuality: widget.imageQuality,
preferredCameraDevice:
widget.preferredCameraDevice,
onImageSelected: (image) {
field.didChange(
[...field.value, image]);
Expand Down
29 changes: 28 additions & 1 deletion lib/src/widgets/image_source_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ import 'package:flutter/foundation.dart';
import 'package:image_picker/image_picker.dart';

class ImageSourceSheet extends StatelessWidget {
/// Optional maximum height of image
final double maxHeight;

/// Optional maximum width of image
final double maxWidth;

/// The imageQuality argument modifies the quality of the image, ranging from
/// 0-100 where 100 is the original/max quality. If imageQuality is null, the
/// image with the original quality will be returned.
final int imageQuality;

/// Use preferredCameraDevice to specify the camera to use when the source is
/// `ImageSource.camera`. The preferredCameraDevice is ignored when source is
/// `ImageSource.gallery`. It is also ignored if the chosen camera is not
/// supported on the device. Defaults to `CameraDevice.rear`.
final CameraDevice preferredCameraDevice;

/// Callback when an image is selected.
///
/// **Note**: This will work on web platform whereas [onImageSelected] will not.
Expand All @@ -18,14 +35,24 @@ class ImageSourceSheet extends StatelessWidget {

ImageSourceSheet({
Key key,
this.maxHeight,
this.maxWidth,
this.imageQuality,
this.preferredCameraDevice = CameraDevice.rear,
this.onImage,
this.onImageSelected,
}) : assert(null != onImage || null != onImageSelected),
super(key: key);

Future<void> _onPickImage(ImageSource source) async {
final imagePicker = ImagePicker();
final pickedFile = await imagePicker.getImage(source: source);
final pickedFile = await imagePicker.getImage(
source: source,
maxHeight: maxHeight,
maxWidth: maxWidth,
imageQuality: imageQuality,
preferredCameraDevice: preferredCameraDevice,
);
if (null != pickedFile) {
if (null != onImage) {
final image = Image.memory(await pickedFile.readAsBytes());
Expand Down

0 comments on commit 68981fd

Please sign in to comment.