@@ -37,6 +37,8 @@ class FormBuilderImagePicker extends StatefulWidget {
37
37
/// supported on the device. Defaults to `CameraDevice.rear` . See [ImagePicker] .
38
38
final CameraDevice preferredCameraDevice;
39
39
40
+ final int maxImages;
41
+
40
42
const FormBuilderImagePicker ({
41
43
Key key,
42
44
@required this .attribute,
@@ -56,6 +58,7 @@ class FormBuilderImagePicker extends StatefulWidget {
56
58
this .maxWidth,
57
59
this .imageQuality,
58
60
this .preferredCameraDevice = CameraDevice .rear,
61
+ this .maxImages,
59
62
}) : super (key: key);
60
63
61
64
@override
@@ -68,6 +71,16 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
68
71
final GlobalKey <FormFieldState > _fieldKey = GlobalKey <FormFieldState >();
69
72
FormBuilderState _formState;
70
73
74
+ bool get _hasMaxImages {
75
+ if (widget.maxImages == null ) {
76
+ return false ;
77
+ } else {
78
+ return /*_fieldKey.currentState.value != null &&*/ _fieldKey
79
+ .currentState.value.length >=
80
+ widget.maxImages;
81
+ }
82
+ }
83
+
71
84
@override
72
85
void initState () {
73
86
_formState = FormBuilder .of (context);
@@ -124,8 +137,9 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
124
137
Container (
125
138
height: widget.imageHeight,
126
139
child: ListView (
127
- scrollDirection: Axis .horizontal,
128
- children: field.value.map <Widget >((item) {
140
+ scrollDirection: Axis .horizontal,
141
+ children: [
142
+ ...(field.value.map <Widget >((item) {
129
143
return Stack (
130
144
alignment: Alignment .topRight,
131
145
children: < Widget > [
@@ -160,45 +174,43 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
160
174
),
161
175
],
162
176
);
163
- }).toList ()
164
- ..add (
165
- GestureDetector (
166
- child: Container (
167
- width: widget.imageWidth,
168
- height: widget.imageHeight,
169
- child: Icon (Icons .camera_enhance,
170
- color: _readOnly
171
- ? Theme .of (context).disabledColor
172
- : widget.iconColor ??
173
- Theme .of (context).primaryColor),
174
- color: (_readOnly
175
- ? Theme .of (context).disabledColor
176
- : widget.iconColor ??
177
- Theme .of (context).primaryColor)
178
- .withAlpha (50 )),
179
- onTap: _readOnly
180
- ? null
181
- : () {
182
- showModalBottomSheet (
183
- context: context,
184
- builder: (_) {
185
- return ImageSourceSheet (
186
- maxHeight: widget.maxHeight,
187
- maxWidth: widget.maxWidth,
188
- imageQuality: widget.imageQuality,
189
- preferredCameraDevice:
190
- widget.preferredCameraDevice,
191
- onImageSelected: (image) {
192
- field.didChange (
193
- [...field.value, image]);
194
- Navigator .of (context).pop ();
195
- },
196
- );
197
- },
198
- );
177
+ }).toList ()),
178
+ if (! _readOnly && ! _hasMaxImages)
179
+ GestureDetector (
180
+ child: Container (
181
+ width: widget.imageWidth,
182
+ height: widget.imageHeight,
183
+ child: Icon (Icons .camera_enhance,
184
+ color: _readOnly
185
+ ? Theme .of (context).disabledColor
186
+ : widget.iconColor ??
187
+ Theme .of (context).primaryColor),
188
+ color: (_readOnly
189
+ ? Theme .of (context).disabledColor
190
+ : widget.iconColor ??
191
+ Theme .of (context).primaryColor)
192
+ .withAlpha (50 )),
193
+ onTap: () {
194
+ showModalBottomSheet (
195
+ context: context,
196
+ builder: (_) {
197
+ return ImageSourceSheet (
198
+ maxHeight: widget.maxHeight,
199
+ maxWidth: widget.maxWidth,
200
+ imageQuality: widget.imageQuality,
201
+ preferredCameraDevice:
202
+ widget.preferredCameraDevice,
203
+ onImageSelected: (image) {
204
+ field.didChange ([...field.value, image]);
205
+ Navigator .of (context).pop ();
199
206
},
200
- ),
201
- )),
207
+ );
208
+ },
209
+ );
210
+ },
211
+ ),
212
+ ],
213
+ ),
202
214
),
203
215
],
204
216
),
0 commit comments