-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/gh 25/images form #62
Merged
+387
−3
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
aa40c2d
feat: created image section of crowdaction form
andre-tm-hui 35f5897
feat: created image section of crowdaction form
andre-tm-hui 59f1f90
Merge branch 'feat/gh-25/images-form' of https://github.com/collactio…
andre-tm-hui aed1456
feat: implemented cropper dialog
andre-tm-hui 485665c
fix: centered text
andre-tm-hui 041f609
fix: updated card and banner dimensions
andre-tm-hui d0b350b
fix: simplified caption
andre-tm-hui 3ea9b3d
feat: horizontal padding in dialog
andre-tm-hui 68b06f0
feat: added horizontal padding as param
andre-tm-hui ec85fa2
Merge branch 'development' of https://github.com/collactionteam/colla…
andre-tm-hui 2dae967
clean: removed unused
andre-tm-hui 9f97a94
fix: changed all croppers to 16:9
andre-tm-hui d1b2a9c
fix: cleaned code, fixed late initialization error
andre-tm-hui 1fa7719
fix: replaced Size with double aspectRatio
andre-tm-hui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...on/crowdactions/crowdaction_form/sections/crowdaction_images/crowdaction_images_form.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:collaction_cms/domain/core/value_validators.dart'; | ||
import 'package:collaction_cms/presentation/shared/form/form_header.dart'; | ||
import 'package:collaction_cms/presentation/shared/form/image_field.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
class CrowdActionImagesForm extends StatefulWidget { | ||
final double width; | ||
final bool buttonTriggered; | ||
|
||
const CrowdActionImagesForm({ | ||
super.key, | ||
this.width = double.infinity, | ||
this.buttonTriggered = false, | ||
}); | ||
|
||
@override | ||
State<CrowdActionImagesForm> createState() => _CrowdActionImagesFormState(); | ||
} | ||
|
||
class _CrowdActionImagesFormState extends State<CrowdActionImagesForm> { | ||
Uint8List? cardImage; | ||
Uint8List? bannerImage; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
width: widget.width, | ||
padding: const EdgeInsets.all(10), | ||
child: Column( | ||
children: [ | ||
const FormHeader(title: "Images"), | ||
const SizedBox(height: 18), | ||
Container( | ||
padding: const EdgeInsets.symmetric(horizontal: 23), | ||
child: Column( | ||
children: [ | ||
ImageField( | ||
label: "Card", | ||
buttonTriggered: widget.buttonTriggered, | ||
validationCallback: validateEmptyField, | ||
callback: (Uint8List image) => cardImage = image, | ||
imageSize: const Size(16, 9), | ||
), | ||
ImageField( | ||
label: "Banner", | ||
buttonTriggered: widget.buttonTriggered, | ||
validationCallback: validateEmptyField, | ||
callback: (Uint8List image) => bannerImage = image, | ||
imageSize: const Size(16, 9), | ||
), | ||
], | ||
), | ||
) | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:collaction_cms/domain/core/value_validators.dart'; | ||
import 'package:collaction_cms/presentation/shared/form/form_field.dart'; | ||
import 'package:collaction_cms/presentation/shared/form/util/image_cropper_dialog.dart'; | ||
import 'package:collaction_cms/presentation/theme/constants.dart'; | ||
import 'package:dotted_border/dotted_border.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_dropzone/flutter_dropzone.dart'; | ||
|
||
class ImageField extends StatefulWidget { | ||
final String label; | ||
final String caption; | ||
final double width; | ||
final Size? imageSize; | ||
final Function? callback; | ||
final Function? validationCallback; | ||
final bool readOnly; | ||
final bool buttonTriggered; | ||
|
||
const ImageField({ | ||
super.key, | ||
required this.label, | ||
this.caption = "Image should be a PNG or JPEG file.", | ||
this.width = double.infinity, | ||
this.imageSize, | ||
this.callback, | ||
this.validationCallback, | ||
this.readOnly = false, | ||
this.buttonTriggered = false, | ||
}); | ||
|
||
@override | ||
State<ImageField> createState() => _ImageFieldState(); | ||
} | ||
|
||
class _ImageFieldState extends State<ImageField> { | ||
final List<String> mime = [ | ||
'image/jpeg', | ||
'image/png', | ||
]; | ||
late DropzoneViewController _controller; | ||
late ValidationOutput _validationOutput; | ||
bool _highlighted = false; | ||
bool _loading = false; | ||
Uint8List? _image; | ||
String message = "Drag and drop your files here or choose file"; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return CollActionFormField( | ||
readOnly: widget.readOnly, | ||
error: widget.buttonTriggered && _validationOutput.error | ||
? _validationOutput.output | ||
: null, | ||
label: widget.label, | ||
caption: widget.caption, | ||
child: DottedBorder( | ||
borderType: BorderType.RRect, | ||
radius: const Radius.circular(10), | ||
color: const Color(0x80707070), | ||
dashPattern: const [4, 4], | ||
strokeWidth: 1, | ||
child: ClipRRect( | ||
borderRadius: const BorderRadius.all(Radius.circular(10)), | ||
child: Container( | ||
width: widget.width, | ||
height: 87, | ||
color: _highlighted ? const Color(0x20A0A0A0) : Colors.transparent, | ||
child: Stack( | ||
children: [ | ||
DropzoneView( | ||
operation: DragOperation.copy, | ||
cursor: _loading ? null : CursorType.grab, | ||
onCreated: (ctrl) => _controller = ctrl, | ||
onHover: () => setState( | ||
() => _highlighted = true, | ||
), | ||
onLeave: () => setState( | ||
() => _highlighted = false, | ||
), | ||
onDrop: (event) async { | ||
await _processUploadedImage(event); | ||
setState(() { | ||
_loading = false; | ||
}); | ||
}, | ||
), | ||
GestureDetector( | ||
onTap: () async { | ||
var files = await _controller.pickFiles(mime: mime); | ||
if (files.isNotEmpty) { | ||
await _processUploadedImage(files[0]); | ||
} | ||
setState(() { | ||
_loading = false; | ||
}); | ||
}, | ||
behavior: HitTestBehavior.translucent, | ||
child: Center( | ||
child: _loading | ||
? const CircularProgressIndicator( | ||
color: kBlackPrimary200, | ||
) | ||
: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
const Icon( | ||
Icons.file_upload_outlined, | ||
color: kBlackPrimary300, | ||
), | ||
const SizedBox(height: 12), | ||
Text( | ||
message, | ||
style: CollactionTextStyles.body14, | ||
textAlign: TextAlign.center, | ||
), | ||
], | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
bool _validateExtension(String s) { | ||
return s.toLowerCase().endsWith("png") || | ||
s.toLowerCase().endsWith("jpg") || | ||
s.toLowerCase().endsWith("jpeg"); | ||
} | ||
|
||
void _validate() { | ||
widget.validationCallback == null | ||
? _validationOutput = ValidationOutput(error: false) | ||
: _validationOutput = widget.validationCallback!(_image); | ||
widget.callback == null | ||
? null | ||
: widget.callback!(_validationOutput.error ? null : _image); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, will make the change |
||
} | ||
|
||
Future<void> _processUploadedImage(dynamic file) async { | ||
setState(() { | ||
_highlighted = false; | ||
_loading = true; | ||
}); | ||
|
||
if (!_validateExtension(file.name)) { | ||
message = "File type invalid."; | ||
return; | ||
} | ||
|
||
_image = await _controller.getFileData(file); | ||
if (!mounted) { | ||
message = "Image could not be loaded."; | ||
return; | ||
} | ||
|
||
if (widget.imageSize != null) { | ||
_image = await showImageCropperModal( | ||
context, | ||
widget.imageSize!, | ||
_image!, | ||
); | ||
} | ||
|
||
if (_image != null) { | ||
_validate(); | ||
message = "Uploaded image: ${file.name}"; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you trigger the button "Save CrowdAction" and the fields are empty a "late initialization error" is produced. This is because the ValidationOutput has not been initialized. One way to fix this is by adding a validation in
initState
like this: