@@ -11,7 +11,7 @@ class _FormInputImagesWidget extends StatelessWidget {
11
11
final bool visuallyMarkRequired;
12
12
final dynamic currentSavedValue;
13
13
final String ? externalError;
14
- final void Function (List <img. Image > images) onSave;
14
+ final void Function (List <Uint8List > images) onSave;
15
15
16
16
const _FormInputImagesWidget ({
17
17
super .key,
@@ -67,7 +67,7 @@ class _FormInputImagesWidgetMulti extends StatefulWidget {
67
67
final bool visuallyMarkRequired;
68
68
final dynamic currentSavedValue;
69
69
final String ? externalError;
70
- final void Function (List <img. Image > images) onSave;
70
+ final void Function (List <Uint8List > images) onSave;
71
71
72
72
const _FormInputImagesWidgetMulti ({
73
73
super .key,
@@ -407,7 +407,7 @@ class _FormInputImagesWidgetSingle extends StatefulWidget {
407
407
final bool visuallyMarkRequired;
408
408
final dynamic currentSavedValue;
409
409
final String ? externalError;
410
- final void Function (img. Image ? image) onSave;
410
+ final void Function (Uint8List ? image) onSave;
411
411
412
412
const _FormInputImagesWidgetSingle ({
413
413
// ignore: unused_element
@@ -450,7 +450,7 @@ class _FormInputImagesWidgetSingleState extends State<_FormInputImagesWidgetSing
450
450
451
451
return _FormInputContainerWidget (
452
452
description: widget.definition.description,
453
- child: FormField <img. Image ?>(
453
+ child: FormField <Uint8List ?>(
454
454
initialValue: widget.currentSavedValue ?? widget.definition.initialValue,
455
455
onSaved: widget.onSave,
456
456
validator: (image)
@@ -530,7 +530,7 @@ class _FormInputImagesWidgetSingleState extends State<_FormInputImagesWidgetSing
530
530
}
531
531
532
532
533
- Widget _buildPreview (BuildContext context, FormFieldState <img. Image ?> state)
533
+ Widget _buildPreview (BuildContext context, FormFieldState <Uint8List ?> state)
534
534
{
535
535
if (_imageProvider == null )
536
536
{
@@ -612,7 +612,7 @@ class _FormInputImagesWidgetSingleState extends State<_FormInputImagesWidgetSing
612
612
);
613
613
}
614
614
615
- Future <void > _addImage (ImageSource source, FormFieldState <img. Image ?> state) async
615
+ Future <void > _addImage (ImageSource source, FormFieldState <Uint8List ?> state) async
616
616
{
617
617
618
618
@@ -627,11 +627,11 @@ class _FormInputImagesWidgetSingleState extends State<_FormInputImagesWidgetSing
627
627
_internalError = null ;
628
628
});
629
629
630
- img. Image ? image;
630
+ Uint8List ? image;
631
631
632
632
if (imageFile != null )
633
633
{
634
- image = await imageFile.getImage ();
634
+ image = await imageFile.readAsBytes ();
635
635
}
636
636
637
637
if (image == null ) return ;
@@ -644,7 +644,7 @@ class _FormInputImagesWidgetSingleState extends State<_FormInputImagesWidgetSing
644
644
{
645
645
state.didChange (image);
646
646
widget.definition.onChange? .call ([image! ]);
647
- _imageProvider = image! . toImageProvider ( );
647
+ _imageProvider = MemoryImage ( image! );
648
648
});
649
649
}
650
650
finally
@@ -657,38 +657,62 @@ class _FormInputImagesWidgetSingleState extends State<_FormInputImagesWidgetSing
657
657
658
658
}
659
659
660
- Future <img. Image > _imageProcessing (img. Image image, FormFieldState <img. Image ?> state) async
660
+ Future <Uint8List > _imageProcessing (Uint8List image, FormFieldState <Uint8List ?> state) async
661
661
{
662
- if (widget.definition.fileSettings.minWidth != null && image.width < widget.definition.fileSettings.minWidth! )
662
+ final codec = await ui.instantiateImageCodec (image);
663
+ final frame = await codec.getNextFrame ();
664
+
665
+
666
+ if (widget.definition.fileSettings.minWidth != null && frame.image.width < widget.definition.fileSettings.minWidth! )
663
667
{
664
668
_internalError = 'MIN_WIDTH' ;
665
669
state.validate ();
666
670
667
671
throw Exception ('Image width is smaller than min width' );
668
672
}
669
673
670
- if (widget.definition.fileSettings.minHeight != null && image.height < widget.definition.fileSettings.minHeight! )
674
+ if (widget.definition.fileSettings.minHeight != null && frame. image.height < widget.definition.fileSettings.minHeight! )
671
675
{
672
676
_internalError = 'MIN_HEIGHT' ;
673
677
state.validate ();
674
678
675
679
throw Exception ('Image height is smaller than min height' );
676
680
}
677
681
682
+ int minHeight = frame.image.height;
683
+ int minWidth = frame.image.width;
678
684
679
- // TODO: use the following for image processing (other is a way too slow): https://pub.dev/packages/flutter_image_compress
685
+ if (widget.definition.fileSettings.maxWidth != null )
686
+ {
687
+ if (widget.definition.fileSettings.maxHeight != null )
688
+ {
689
+ throw UnimplementedError ('Max width and max height are not yet supported' );
690
+ }
691
+ else
692
+ {
693
+ minWidth = widget.definition.fileSettings.maxWidth! ;
694
+ }
695
+ }
696
+ else if (widget.definition.fileSettings.maxHeight != null )
697
+ {
698
+ minHeight = widget.definition.fileSettings.maxHeight! ;
699
+ }
680
700
681
- return image.convertImage (
682
- type: widget.definition.fileSettings.conversion,
683
- quality: widget.definition.fileSettings.conversionQuality,
684
- maxWidth: widget.definition.fileSettings.maxWidth,
685
- maxHeight: widget.definition.fileSettings.maxHeight,
701
+ return await FlutterImageCompress .compressWithList (image,
702
+ minHeight: minHeight,
703
+ minWidth: minWidth,
704
+ quality: (widget.definition.fileSettings.conversionQuality * 100 ).round (),
705
+ format: switch (widget.definition.fileSettings.conversion)
706
+ {
707
+ kImageConversionType.jpeg => CompressFormat .jpeg,
708
+ kImageConversionType.png => CompressFormat .png,
709
+ },
686
710
);
687
-
711
+
688
712
}
689
713
690
714
691
- void _deleteCurrentImage (FormFieldState <img. Image ?> state)
715
+ void _deleteCurrentImage (FormFieldState <Uint8List ?> state)
692
716
{
693
717
if (state.value == null )
694
718
{
0 commit comments