submitForm method
Submits the currentData if not already submitting
Implementation
Future<void> submitForm() async {
final link = _formData?.links[ApptiveLinkType.submit];
if (link != null) {
setState(() {
_submitting = true;
});
const doneAttachmentPercentage = 0.6;
const uploadFormPercentage = 0.8;
const startPercentage = 0.1;
_submitProgressSubscription?.cancel();
final attachmentsToUpload = _formData!.attachmentActions.length;
int attachmentCount = 0;
setState(() {
_progress = UploadingAttachmentsProgress(
processedAttachments: attachmentCount,
totalAttachments: attachmentsToUpload,
progress: startPercentage,
);
});
_submitProgressSubscription =
_client.submitFormWithProgress(link, _formData!).listen(
(event) async {
if (event is ProcessedAttachmentProgressEvent) {
setState(() {
_progress = UploadingAttachmentsProgress(
processedAttachments: ++attachmentCount,
totalAttachments: attachmentsToUpload,
progress: startPercentage +
(attachmentCount *
(doneAttachmentPercentage - startPercentage) /
attachmentsToUpload),
);
});
} else if (event is AttachmentCompleteProgressEvent) {
final response = event.response;
if (response != null && response.statusCode >= 400) {
_onSavedOffline(link);
}
} else if (event is UploadFormProgressEvent) {
setState(() {
_progress = SubmittingFormProgress(
progress: uploadFormPercentage,
);
});
} else if (event is SubmitCompleteProgressEvent) {
final response = event.response;
if (response != null && response.statusCode < 400) {
final createdUri = _parseCreatedBody(response: response);
if (createdUri != null) {
widget.onCreated?.call(createdUri);
}
if (await widget.onActionSuccess?.call(link, _formData!) !=
false) {
if (_formData?.properties?.reloadAfterSubmit == true) {
widget.triggerReload?.call();
_updateView();
} else {
setState(() {
_success = true;
});
}
}
} else {
// FormData was saved to [ApptiveGridCache]
_onSavedOffline(link);
}
} else if (event is ErrorProgressEvent) {
_onError(event.error);
}
},
onError: (error) {
_onError(error);
},
onDone: () {
setState(() {
_submitting = false;
});
},
);
}
}