Skip to content

Commit 2df184d

Browse files
Merge pull request #40 from fkoch-tgm/main
Allow only camera or gallery
2 parents 895c11c + 663da89 commit 2df184d

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ ___
1414
- [Use](#use)
1515
- [Setup](#setup)
1616
- [Basic use](#basic-use)
17-
- [Especific uses](#especific-uses)
1817
- [Support](#support)
1918
- [Contribute](#contribute)
2019
- [Questions and answers](#questions-and-answers)
@@ -36,7 +35,7 @@ ___
3635

3736
Since this package makes use of [image_picker](https://pub.dev/packages/image_picker) package, for platform specific setup, follow the instructions [here](https://github.com/flutter/plugins/tree/main/packages/image_picker/image_picker#installation)
3837

39-
### Basuc use
38+
### Basic use
4039

4140
```dart
4241
FormBuilder(
@@ -53,7 +52,22 @@ FormBuilder(
5352
),
5453
```
5554

56-
See [pud.dev example tab](https://pub.dev/packages/form_builder_image_picker/example) or [github code](example/lib/main.dart) for more details
55+
### Only specific pickers
56+
```dart
57+
FormBuilder(
58+
child: Column(
59+
mainAxisAlignment: MainAxisAlignment.center,
60+
children: <Widget>[
61+
FormBuilderImagePicker(
62+
name: 'noCamera',
63+
availableImageSources: const [ImageSourceOption.gallery],
64+
),
65+
],
66+
),
67+
),
68+
```
69+
70+
See [pub.dev example tab](https://pub.dev/packages/form_builder_image_picker/example) or [github code](example/lib/main.dart) for more details
5771

5872
## Support
5973

example/lib/main.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MyApp extends StatelessWidget {
2626
class ApiImage {
2727
final String imageUrl;
2828
final String id;
29+
2930
ApiImage({
3031
required this.imageUrl,
3132
required this.id,
@@ -138,6 +139,20 @@ class MyHomePage extends StatelessWidget {
138139
iconColor: Colors.white,
139140
icon: Icons.ac_unit_rounded,
140141
),
142+
const SizedBox(height: 15),
143+
FormBuilderImagePicker(
144+
name: 'onlyCamera',
145+
decoration: const InputDecoration(
146+
labelText: 'Pick Photos (only from camera)'),
147+
availableImageSources: const [ImageSourceOption.camera],
148+
),
149+
const SizedBox(height: 15),
150+
FormBuilderImagePicker(
151+
name: 'onlyGallery',
152+
decoration: const InputDecoration(
153+
labelText: 'Pick Photos (only from gallery)'),
154+
availableImageSources: const [ImageSourceOption.gallery],
155+
),
141156
ElevatedButton(
142157
child: const Text('Submit'),
143158
onPressed: () {

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)