Skip to content

Commit 33b3053

Browse files
Merge pull request #41 from itsmeabhi12/main
Added Options For Custom Picker View
2 parents 2df184d + 65828bc commit 33b3053

File tree

3 files changed

+85
-22
lines changed

3 files changed

+85
-22
lines changed

example/lib/main.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/cupertino.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_form_builder/flutter_form_builder.dart';
34

@@ -153,6 +154,36 @@ class MyHomePage extends StatelessWidget {
153154
labelText: 'Pick Photos (only from gallery)'),
154155
availableImageSources: const [ImageSourceOption.gallery],
155156
),
157+
FormBuilderImagePicker(
158+
decoration: const InputDecoration(
159+
labelText: 'Pick Photos (with custom view)'),
160+
name: "CupertinoActionSheet",
161+
optionsBuilder: (cameraPicker, galleryPicker) =>
162+
CupertinoActionSheet(
163+
title: const Text('Image'),
164+
message: const Text('Pick an image from given options'),
165+
actions: [
166+
CupertinoActionSheetAction(
167+
isDefaultAction: true,
168+
onPressed: () {
169+
cameraPicker();
170+
},
171+
child: const Text('Camera'),
172+
),
173+
CupertinoActionSheetAction(
174+
isDefaultAction: true,
175+
onPressed: () {
176+
galleryPicker();
177+
},
178+
child: const Text('Gallery'),
179+
)
180+
],
181+
),
182+
onTap: (child) => showCupertinoModalPopup(
183+
context: context,
184+
builder: (context) => child,
185+
),
186+
),
156187
ElevatedButton(
157188
child: const Text('Submit'),
158189
onPressed: () {

lib/src/form_builder_image_picker.dart

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ class FormBuilderImagePicker extends FormBuilderField<List<dynamic>> {
100100
/// Either [ImageSourceOption.gallery], [ImageSourceOption.camera] or both.
101101
final List<ImageSourceOption> availableImageSources;
102102

103+
///A callback that returns a pickup options
104+
///ListTile(inside Wrap) by Default
105+
///use optionsBuilder to return a widget of your choice
106+
final ValueChanged<ImageSourceBottomSheet>? onTap;
107+
108+
/// use this callback if you want custom view for options
109+
/// call cameraPicker() to picks image from camera
110+
/// call galleryPicker() to picks image from gallery
111+
final Widget Function(
112+
FutureVoidCallBack cameraPicker, FutureVoidCallBack galleryPicker)?
113+
optionsBuilder;
114+
103115
FormBuilderImagePicker({
104116
Key? key,
105117
//From Super
@@ -140,6 +152,8 @@ class FormBuilderImagePicker extends FormBuilderField<List<dynamic>> {
140152
this.galleryLabel = const Text('Gallery'),
141153
this.bottomSheetPadding = EdgeInsets.zero,
142154
this.placeholderImage,
155+
this.onTap,
156+
this.optionsBuilder,
143157
this.availableImageSources = const [
144158
ImageSourceOption.camera,
145159
ImageSourceOption.gallery,
@@ -197,30 +211,35 @@ class FormBuilderImagePicker extends FormBuilderField<List<dynamic>> {
197211
onTap: () async {
198212
final remainingImages =
199213
maxImages == null ? null : maxImages - value.length;
200-
await showModalBottomSheet<void>(
201-
context: state.context,
202-
builder: (_) {
203-
return ImageSourceBottomSheet(
204-
maxHeight: maxHeight,
205-
maxWidth: maxWidth,
206-
preventPop: preventPop,
207-
remainingImages: remainingImages,
208-
imageQuality: imageQuality,
209-
preferredCameraDevice: preferredCameraDevice,
210-
bottomSheetPadding: bottomSheetPadding,
211-
cameraIcon: cameraIcon,
212-
cameraLabel: cameraLabel,
213-
galleryIcon: galleryIcon,
214-
galleryLabel: galleryLabel,
215-
availableImageSources: availableImageSources,
216-
onImageSelected: (image) {
217-
state.requestFocus();
218-
field.didChange([...value, ...image]);
219-
Navigator.pop(state.context);
220-
},
221-
);
214+
215+
final imageSourceSheet = ImageSourceBottomSheet(
216+
maxHeight: maxHeight,
217+
maxWidth: maxWidth,
218+
preventPop: preventPop,
219+
remainingImages: remainingImages,
220+
imageQuality: imageQuality,
221+
preferredCameraDevice: preferredCameraDevice,
222+
bottomSheetPadding: bottomSheetPadding,
223+
cameraIcon: cameraIcon,
224+
cameraLabel: cameraLabel,
225+
galleryIcon: galleryIcon,
226+
galleryLabel: galleryLabel,
227+
optionsBuilder: optionsBuilder,
228+
availableImageSources: availableImageSources,
229+
onImageSelected: (image) {
230+
state.requestFocus();
231+
field.didChange([...value, ...image]);
232+
Navigator.pop(state.context);
222233
},
223234
);
235+
onTap != null
236+
? onTap(imageSourceSheet)
237+
: await showModalBottomSheet<void>(
238+
context: state.context,
239+
builder: (_) {
240+
return imageSourceSheet;
241+
},
242+
);
224243
},
225244
);
226245

lib/src/image_source_sheet.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'package:image_picker/image_picker.dart';
33

44
import 'image_source_option.dart';
55

6+
typedef FutureVoidCallBack = Future<void> Function();
7+
68
class ImageSourceBottomSheet extends StatefulWidget {
79
/// Optional maximum height of image
810
final double? maxHeight;
@@ -37,6 +39,10 @@ class ImageSourceBottomSheet extends StatefulWidget {
3739
final EdgeInsets? bottomSheetPadding;
3840
final bool preventPop;
3941

42+
final Widget Function(
43+
FutureVoidCallBack cameraPicker, FutureVoidCallBack galleryPicker)?
44+
optionsBuilder;
45+
4046
const ImageSourceBottomSheet({
4147
Key? key,
4248
this.remainingImages,
@@ -51,6 +57,7 @@ class ImageSourceBottomSheet extends StatefulWidget {
5157
this.cameraLabel,
5258
this.galleryLabel,
5359
this.bottomSheetPadding,
60+
this.optionsBuilder,
5461
required this.availableImageSources,
5562
}) : super(key: key);
5663

@@ -97,6 +104,12 @@ class ImageSourceBottomSheetState extends State<ImageSourceBottomSheet> {
97104

98105
@override
99106
Widget build(BuildContext context) {
107+
if (widget.optionsBuilder != null) {
108+
return widget.optionsBuilder!(
109+
() => _onPickImage(ImageSource.camera),
110+
() => _onPickImage(ImageSource.gallery),
111+
);
112+
}
100113
Widget res = Container(
101114
padding: widget.bottomSheetPadding,
102115
child: Wrap(

0 commit comments

Comments
 (0)