Skip to content

Commit 82e18d1

Browse files
committed
Added maxImages attribute to ImagePicker - limit number of images user can upload. Fixes #291, #301
1 parent 30de979 commit 82e18d1

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

lib/src/fields/form_builder_image_picker.dart

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class FormBuilderImagePicker extends StatefulWidget {
3737
/// supported on the device. Defaults to `CameraDevice.rear`. See [ImagePicker].
3838
final CameraDevice preferredCameraDevice;
3939

40+
final int maxImages;
41+
4042
const FormBuilderImagePicker({
4143
Key key,
4244
@required this.attribute,
@@ -56,6 +58,7 @@ class FormBuilderImagePicker extends StatefulWidget {
5658
this.maxWidth,
5759
this.imageQuality,
5860
this.preferredCameraDevice = CameraDevice.rear,
61+
this.maxImages,
5962
}) : super(key: key);
6063

6164
@override
@@ -68,6 +71,16 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
6871
final GlobalKey<FormFieldState> _fieldKey = GlobalKey<FormFieldState>();
6972
FormBuilderState _formState;
7073

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+
7184
@override
7285
void initState() {
7386
_formState = FormBuilder.of(context);
@@ -124,8 +137,9 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
124137
Container(
125138
height: widget.imageHeight,
126139
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) {
129143
return Stack(
130144
alignment: Alignment.topRight,
131145
children: <Widget>[
@@ -160,45 +174,43 @@ class _FormBuilderImagePickerState extends State<FormBuilderImagePicker> {
160174
),
161175
],
162176
);
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();
199206
},
200-
),
201-
)),
207+
);
208+
},
209+
);
210+
},
211+
),
212+
],
213+
),
202214
),
203215
],
204216
),

0 commit comments

Comments
 (0)