Skip to content

Commit a0a8323

Browse files
committed
add availableImageSources option
1 parent 895c11c commit a0a8323

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

lib/form_builder_image_picker.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
library form_builder_image_picker;
22

33
export 'src/form_builder_image_picker.dart';
4+
export 'src/image_source_option.dart';

lib/src/form_builder_image_picker.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
55
import 'package:flutter_form_builder/flutter_form_builder.dart';
66
import 'package:image_picker/image_picker.dart';
77

8+
import 'image_source_option.dart';
89
import 'image_source_sheet.dart';
910

1011
/// Field for picking image(s) from Gallery or Camera.
@@ -95,6 +96,10 @@ class FormBuilderImagePicker extends FormBuilderField<List<dynamic>> {
9596
/// fit for each image
9697
final BoxFit fit;
9798

99+
/// The sources available to pick from.
100+
/// Either [ImageSourceOption.gallery], [ImageSourceOption.camera] or both.
101+
final List<ImageSourceOption> availableImageSources;
102+
98103
FormBuilderImagePicker({
99104
Key? key,
100105
//From Super
@@ -135,6 +140,10 @@ class FormBuilderImagePicker extends FormBuilderField<List<dynamic>> {
135140
this.galleryLabel = const Text('Gallery'),
136141
this.bottomSheetPadding = EdgeInsets.zero,
137142
this.placeholderImage,
143+
this.availableImageSources = const [
144+
ImageSourceOption.camera,
145+
ImageSourceOption.gallery,
146+
],
138147
}) : assert(maxImages == null || maxImages >= 0),
139148
super(
140149
key: key,
@@ -203,6 +212,7 @@ class FormBuilderImagePicker extends FormBuilderField<List<dynamic>> {
203212
cameraLabel: cameraLabel,
204213
galleryIcon: galleryIcon,
205214
galleryLabel: galleryLabel,
215+
availableImageSources: availableImageSources,
206216
onImageSelected: (image) {
207217
state.requestFocus();
208218
field.didChange([...value, ...image]);

lib/src/image_source_option.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// Options where a user can pick images from
2+
enum ImageSourceOption {
3+
/// From the device camera
4+
/// (via [ImageSource.camera])
5+
camera,
6+
7+
/// From the gallery (or local files on the web)
8+
/// (via [ImageSource.gallery])
9+
gallery
10+
}

lib/src/image_source_sheet.dart

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:image_picker/image_picker.dart';
33

4+
import 'image_source_option.dart';
5+
46
class ImageSourceBottomSheet extends StatefulWidget {
57
/// Optional maximum height of image
68
final double? maxHeight;
@@ -24,6 +26,10 @@ class ImageSourceBottomSheet extends StatefulWidget {
2426
/// Callback when an image is selected.
2527
final void Function(Iterable<XFile> files) onImageSelected;
2628

29+
/// The sources to create ListTiles for.
30+
/// Either [ImageSourceOption.gallery], [ImageSourceOption.camera] or both.
31+
final List<ImageSourceOption> availableImageSources;
32+
2733
final Widget? cameraIcon;
2834
final Widget? galleryIcon;
2935
final Widget? cameraLabel;
@@ -45,6 +51,7 @@ class ImageSourceBottomSheet extends StatefulWidget {
4551
this.cameraLabel,
4652
this.galleryLabel,
4753
this.bottomSheetPadding,
54+
required this.availableImageSources,
4855
}) : super(key: key);
4956

5057
@override
@@ -94,16 +101,18 @@ class ImageSourceBottomSheetState extends State<ImageSourceBottomSheet> {
94101
padding: widget.bottomSheetPadding,
95102
child: Wrap(
96103
children: <Widget>[
97-
ListTile(
98-
leading: widget.cameraIcon,
99-
title: widget.cameraLabel,
100-
onTap: () => _onPickImage(ImageSource.camera),
101-
),
102-
ListTile(
103-
leading: widget.galleryIcon,
104-
title: widget.galleryLabel,
105-
onTap: () => _onPickImage(ImageSource.gallery),
106-
),
104+
if (widget.availableImageSources.contains(ImageSourceOption.camera))
105+
ListTile(
106+
leading: widget.cameraIcon,
107+
title: widget.cameraLabel,
108+
onTap: () => _onPickImage(ImageSource.camera),
109+
),
110+
if (widget.availableImageSources.contains(ImageSourceOption.gallery))
111+
ListTile(
112+
leading: widget.galleryIcon,
113+
title: widget.galleryLabel,
114+
onTap: () => _onPickImage(ImageSource.gallery),
115+
),
107116
],
108117
),
109118
);

0 commit comments

Comments
 (0)