Skip to content
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
merged 14 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:collaction_cms/domain/crowdaction/crowdaction.dart';
import 'package:collaction_cms/presentation/crowdactions/crowdaction_form/sections/commitments_section/commitments_form.dart';
import 'package:collaction_cms/presentation/crowdactions/crowdaction_form/sections/crowdaction_info/crowdaction_info_form.dart';
import 'package:collaction_cms/presentation/crowdactions/crowdaction_form/sections/crowdaction_info/crowdaction_info_controller.dart';
import 'package:collaction_cms/presentation/crowdactions/crowdaction_form/sections/crowdaction_images/crowdaction_images_form.dart';
import 'package:collaction_cms/presentation/shared/buttons/buttons.dart';
import 'package:collaction_cms/presentation/theme/constants.dart';
import 'package:defer_pointer/defer_pointer.dart';
Expand Down Expand Up @@ -89,10 +90,9 @@ class _CrowdActionFormModalState extends State<CrowdActionFormModal> {
controller: _crowdActionInfoFormController,
),
// replace with CrowdActionImagesForm
CrowdActionInfoForm(
CrowdActionImagesForm(
width: halfWidth,
buttonTriggered: _buttonTriggered,
controller: _crowdActionInfoFormController,
),
// replace with CrowdActionCommitmentsForm
const CrowdActionCommitmentsForm()
Expand Down
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),
),
],
),
)
],
),
);
}
}
10 changes: 9 additions & 1 deletion lib/presentation/shared/form/form_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
class CollActionFormField extends StatelessWidget {
final Widget child;
final String? label;
final String? caption;
final String? error;
final double width;
final bool readOnly;
Expand All @@ -12,6 +13,7 @@ class CollActionFormField extends StatelessWidget {
super.key,
required this.child,
this.label,
this.caption,
this.error,
this.width = double.infinity,
this.readOnly = false,
Expand All @@ -28,7 +30,13 @@ class CollActionFormField extends StatelessWidget {
"$label:",
style: CollactionTextStyles.bodyMedium14,
),
const SizedBox(height: 4),
if (caption != null) ...[
const SizedBox(height: 10),
Text(caption!, style: CollactionTextStyles.body14),
const SizedBox(height: 10),
] else ...[
const SizedBox(height: 4),
],
IgnorePointer(
ignoring: readOnly,
child: child,
Expand Down
175 changes: 175 additions & 0 deletions lib/presentation/shared/form/image_field.dart
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;
Copy link
Member

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:

          void initState() {
            // TODO: implement initState
            super.initState();
            widget.validationCallback == null
                ? _validationOutput = ValidationOutput(error: false)
                : _validationOutput = widget.validationCallback!(_image);
      }
    

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that widget.callback?.call(_validationOutput.error ? null : _image) is cleaner in these cases

Copy link
Author

Choose a reason for hiding this comment

The 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}";
}
}
}
Loading