@@ -14,23 +14,28 @@ class ImageSourceBottomSheet extends StatefulWidget {
14
14
/// image with the original quality will be returned.
15
15
final int ? imageQuality;
16
16
17
+ final int ? remainingImages;
18
+
17
19
/// Use preferredCameraDevice to specify the camera to use when the source is
18
20
/// `ImageSource.camera` . The preferredCameraDevice is ignored when source is
19
21
/// `ImageSource.gallery` . It is also ignored if the chosen camera is not
20
22
/// supported on the device. Defaults to `CameraDevice.rear` .
21
23
final CameraDevice preferredCameraDevice;
22
24
23
25
/// Callback when an image is selected.
24
- final void Function (XFile ) onImageSelected;
26
+ final void Function (Iterable < XFile > files ) onImageSelected;
25
27
26
28
final Widget ? cameraIcon;
27
29
final Widget ? galleryIcon;
28
30
final Widget ? cameraLabel;
29
31
final Widget ? galleryLabel;
30
32
final EdgeInsets ? bottomSheetPadding;
33
+ final bool preventPop;
31
34
32
35
ImageSourceBottomSheet ({
33
36
Key ? key,
37
+ this .remainingImages,
38
+ this .preventPop = false ,
34
39
this .maxHeight,
35
40
this .maxWidth,
36
41
this .imageQuality,
@@ -54,40 +59,61 @@ class _ImageSourceBottomSheetState extends State<ImageSourceBottomSheet> {
54
59
if (_isPickingImage) return ;
55
60
_isPickingImage = true ;
56
61
final imagePicker = ImagePicker ();
57
- final pickedFile = await imagePicker.pickImage (
58
- source: source,
59
- maxHeight: widget.maxHeight,
60
- maxWidth: widget.maxWidth,
61
- imageQuality: widget.imageQuality,
62
- preferredCameraDevice: widget.preferredCameraDevice,
63
- );
64
- _isPickingImage = false ;
65
- if (pickedFile != null ) {
66
- widget.onImageSelected (pickedFile);
62
+ try {
63
+ if (source == ImageSource .camera || widget.remainingImages == 1 ) {
64
+ final pickedFile = await imagePicker.pickImage (
65
+ source: source,
66
+ preferredCameraDevice: widget.preferredCameraDevice,
67
+ maxHeight: widget.maxHeight,
68
+ maxWidth: widget.maxWidth,
69
+ imageQuality: widget.imageQuality,
70
+ );
71
+ _isPickingImage = false ;
72
+ if (pickedFile != null ) {
73
+ widget.onImageSelected ([pickedFile]);
74
+ }
75
+ } else {
76
+ final pickedFiles = await imagePicker.pickMultiImage (
77
+ maxHeight: widget.maxHeight,
78
+ maxWidth: widget.maxWidth,
79
+ imageQuality: widget.imageQuality,
80
+ );
81
+ _isPickingImage = false ;
82
+ if (pickedFiles != null && pickedFiles.length > 0 ) {
83
+ widget.onImageSelected (pickedFiles);
84
+ }
85
+ }
86
+ } catch (e) {
87
+ _isPickingImage = false ;
88
+ rethrow ;
67
89
}
68
90
}
69
91
70
92
@override
71
93
Widget build (BuildContext context) {
72
- return WillPopScope (
73
- onWillPop: () async => ! _isPickingImage,
74
- child: Container (
75
- padding: widget.bottomSheetPadding,
76
- child: Wrap (
77
- children: < Widget > [
78
- ListTile (
79
- leading: widget.cameraIcon,
80
- title: widget.cameraLabel,
81
- onTap: () => _onPickImage (ImageSource .camera),
82
- ),
83
- ListTile (
84
- leading: widget.galleryIcon,
85
- title: widget.galleryLabel,
86
- onTap: () => _onPickImage (ImageSource .gallery),
87
- ),
88
- ],
89
- ),
94
+ Widget res = Container (
95
+ padding: widget.bottomSheetPadding,
96
+ child: Wrap (
97
+ children: < Widget > [
98
+ ListTile (
99
+ leading: widget.cameraIcon,
100
+ title: widget.cameraLabel,
101
+ onTap: () => _onPickImage (ImageSource .camera),
102
+ ),
103
+ ListTile (
104
+ leading: widget.galleryIcon,
105
+ title: widget.galleryLabel,
106
+ onTap: () => _onPickImage (ImageSource .gallery),
107
+ ),
108
+ ],
90
109
),
91
110
);
111
+ if (widget.preventPop) {
112
+ res = WillPopScope (
113
+ onWillPop: () async => ! _isPickingImage,
114
+ child: res,
115
+ );
116
+ }
117
+ return res;
92
118
}
93
119
}
0 commit comments