From f8910c8f8b384647e3d38bcad839ab11131fc0cb Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Mar 2021 18:11:28 +0100 Subject: [PATCH] Force square account image --- lib/ui/dialogs/pick_file_dialog.dart | 4 +++- lib/ui/routes/image_editor_route.dart | 5 +++-- lib/ui/routes/settings/account_image_settings.dart | 4 +++- lib/ui/widgets/post_create_form_widget.dart | 2 +- lib/utils/files_utils.dart | 5 ++++- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/ui/dialogs/pick_file_dialog.dart b/lib/ui/dialogs/pick_file_dialog.dart index 9a83154..677d6d5 100644 --- a/lib/ui/dialogs/pick_file_dialog.dart +++ b/lib/ui/dialogs/pick_file_dialog.dart @@ -8,6 +8,7 @@ import 'package:file_picker/file_picker.dart'; import 'package:filesize/filesize.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:image_cropper/image_cropper.dart'; import 'package:image_picker/image_picker.dart'; import 'package:mime/mime.dart'; @@ -91,6 +92,7 @@ Future showPickFileDialog({ List allowedMimeTypes, int imageMaxWidth, int imageMaxHeight, + CropAspectRatio aspectRatio, }) async { assert(allowedMimeTypes != null); @@ -133,7 +135,7 @@ Future showPickFileDialog({ file = BytesFile(image.path.split("/").last, await image.readAsBytes()); - file = await showImageCropper(context, file); + file = await showImageCropper(context, file, aspectRatio: aspectRatio); break; diff --git a/lib/ui/routes/image_editor_route.dart b/lib/ui/routes/image_editor_route.dart index 9d0ffd6..fd15447 100644 --- a/lib/ui/routes/image_editor_route.dart +++ b/lib/ui/routes/image_editor_route.dart @@ -17,8 +17,8 @@ import '../../utils/files_utils.dart'; /// Attempt to crop image /// /// Return original image in case of error / if the user did not crop the image -Future showImageCropper( - BuildContext context, BytesFile source) async { +Future showImageCropper(BuildContext context, BytesFile source, + {CropAspectRatio aspectRatio}) async { assert(context != null); assert(source != null); @@ -32,6 +32,7 @@ Future showImageCropper( File cropped = await ImageCropper.cropImage( sourcePath: file.absolute.path, compressFormat: ImageCompressFormat.png, + aspectRatio: aspectRatio, androidUiSettings: AndroidUiSettings( toolbarColor: Colors.black, toolbarTitle: tr("Crop Photo"), diff --git a/lib/ui/routes/settings/account_image_settings.dart b/lib/ui/routes/settings/account_image_settings.dart index 33c6817..9572c83 100644 --- a/lib/ui/routes/settings/account_image_settings.dart +++ b/lib/ui/routes/settings/account_image_settings.dart @@ -12,6 +12,7 @@ import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; import 'package:identicon/identicon.dart'; +import 'package:image_cropper/image_cropper.dart'; import 'package:random_string/random_string.dart'; import 'package:settings_ui/settings_ui.dart'; @@ -160,7 +161,8 @@ class _AccountImageSettingsScreenState /// Upload a new account image void _uploadAccountImage() async { try { - final image = await pickImage(context); + final image = await pickImage(context, + aspectRatio: CropAspectRatio(ratioX: 5, ratioY: 5)); if (image == null) return; diff --git a/lib/ui/widgets/post_create_form_widget.dart b/lib/ui/widgets/post_create_form_widget.dart index 4025e55..9e472b5 100644 --- a/lib/ui/widgets/post_create_form_widget.dart +++ b/lib/ui/widgets/post_create_form_widget.dart @@ -254,7 +254,7 @@ class _PostCreateFormWidgetState extends State { }); } catch (e, s) { logError(e, s); - snack(context, tr("Failed to pick image for post!")); + snack(context, tr("Failed to pick an image for the post!")); } } diff --git a/lib/utils/files_utils.dart b/lib/utils/files_utils.dart index 3e6bb8c..16b45a7 100644 --- a/lib/utils/files_utils.dart +++ b/lib/utils/files_utils.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:image_cropper/image_cropper.dart'; import 'package:path/path.dart' as path; import 'package:path_provider/path_provider.dart'; import 'package:random_string/random_string.dart'; @@ -15,12 +16,14 @@ import '../ui/dialogs/pick_file_dialog.dart'; /// Ask the user to choose an image, either from the gallery or using the camera /// /// Throws an exception null in case of failure -Future pickImage(BuildContext context) async { +Future pickImage(BuildContext context, + {CropAspectRatio aspectRatio}) async { return await showPickFileDialog( context: context, allowedMimeTypes: ["image/png", "image/jpeg", "image/gif"], imageMaxHeight: 10000, imageMaxWidth: 10000, + aspectRatio: aspectRatio, ); }