mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Can post PDFs
This commit is contained in:
@ -9,6 +9,7 @@ import 'package:comunic/utils/files_utils.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/post_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:file_picker_cross/file_picker_cross.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Widget that allows to create posts
|
||||
@ -47,15 +48,20 @@ class _PostCreateFormWidgetState extends State<PostCreateFormWidget> {
|
||||
final TextEditingController _postTextController = TextEditingController();
|
||||
PostVisibilityLevel _postVisibilityLevel;
|
||||
File _postImage;
|
||||
List<int> _postPDF;
|
||||
|
||||
bool get hasImage => _postImage != null;
|
||||
|
||||
bool get hasPDF => _postPDF != null;
|
||||
|
||||
bool get canSubmitForm =>
|
||||
!_isCreating && _postTextController.text.length > 5 || hasImage;
|
||||
!_isCreating && _postTextController.text.length > 5 || hasImage || hasPDF;
|
||||
|
||||
PostKind get postKind {
|
||||
if (hasImage)
|
||||
return PostKind.IMAGE;
|
||||
else if (hasPDF)
|
||||
return PostKind.PDF;
|
||||
else
|
||||
return PostKind.TEXT;
|
||||
}
|
||||
@ -102,6 +108,13 @@ class _PostCreateFormWidgetState extends State<PostCreateFormWidget> {
|
||||
onTap: _pickImageForPost,
|
||||
),
|
||||
|
||||
// Include PDF button
|
||||
_PostOptionWidget(
|
||||
icon: Icons.picture_as_pdf,
|
||||
selected: postKind == PostKind.PDF,
|
||||
onTap: _pickPDFForPost,
|
||||
),
|
||||
|
||||
Expanded(
|
||||
child: Container(),
|
||||
),
|
||||
@ -159,6 +172,7 @@ class _PostCreateFormWidgetState extends State<PostCreateFormWidget> {
|
||||
void _resetPostSelection() {
|
||||
setState(() {
|
||||
_postImage = null;
|
||||
_postPDF = null;
|
||||
});
|
||||
}
|
||||
|
||||
@ -175,6 +189,27 @@ class _PostCreateFormWidgetState extends State<PostCreateFormWidget> {
|
||||
});
|
||||
}
|
||||
|
||||
/// Pick a PDF for the new post
|
||||
Future<void> _pickPDFForPost() async {
|
||||
try {
|
||||
final picker = FilePickerCross(
|
||||
type: FileTypeCross.custom,
|
||||
fileExtension: "pdf",
|
||||
);
|
||||
|
||||
if (!await picker.pick()) return;
|
||||
|
||||
_resetPostSelection();
|
||||
|
||||
setState(() {
|
||||
this._postPDF = picker.toUint8List();
|
||||
});
|
||||
} catch (e, stack) {
|
||||
print("Pick PDF error: $e\n$stack");
|
||||
showSimpleSnack(context, tr("Could not pick a PDF!"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Submit new post
|
||||
Future<void> _submitForm() async {
|
||||
if (!canSubmitForm)
|
||||
@ -190,6 +225,7 @@ class _PostCreateFormWidgetState extends State<PostCreateFormWidget> {
|
||||
content: _postTextController.text,
|
||||
kind: postKind,
|
||||
image: _postImage,
|
||||
pdf: _postPDF,
|
||||
));
|
||||
setState(() => _isCreating = false);
|
||||
|
||||
|
Reference in New Issue
Block a user