mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Can crop image
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import 'package:comunic/models/api_request.dart';
|
||||
import 'package:comunic/ui/dialogs/record_audio_dialog.dart';
|
||||
import 'package:comunic/ui/routes/image_editor_route.dart';
|
||||
import 'package:comunic/utils/files_utils.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
@ -132,6 +133,8 @@ Future<BytesFile> showPickFileDialog({
|
||||
|
||||
file = BytesFile(image.path.split("/").last, await image.readAsBytes());
|
||||
|
||||
file = await showImageCropper(context, file);
|
||||
|
||||
break;
|
||||
|
||||
// Pick an video
|
||||
|
53
lib/ui/routes/image_editor_route.dart
Normal file
53
lib/ui/routes/image_editor_route.dart
Normal file
@ -0,0 +1,53 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:comunic/models/api_request.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/log_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_cropper/image_cropper.dart';
|
||||
|
||||
import '../../models/api_request.dart';
|
||||
import '../../utils/files_utils.dart';
|
||||
|
||||
/// Image cropper route
|
||||
///
|
||||
/// @author Pierre Hubert
|
||||
|
||||
/// Attempt to crop image
|
||||
///
|
||||
/// Return original image in case of error / if the user did not crop the image
|
||||
Future<BytesFile> showImageCropper(
|
||||
BuildContext context, BytesFile source) async {
|
||||
assert(context != null);
|
||||
assert(source != null);
|
||||
|
||||
File file;
|
||||
File cropped;
|
||||
|
||||
try {
|
||||
file = await generateTemporaryFile();
|
||||
await file.writeAsBytes(source.bytes);
|
||||
|
||||
File cropped = await ImageCropper.cropImage(
|
||||
sourcePath: file.absolute.path,
|
||||
compressFormat: ImageCompressFormat.png,
|
||||
androidUiSettings: AndroidUiSettings(
|
||||
toolbarColor: Colors.black,
|
||||
toolbarTitle: tr("Crop Photo"),
|
||||
toolbarWidgetColor: Colors.white,
|
||||
),
|
||||
);
|
||||
|
||||
if (cropped == null) return null;
|
||||
|
||||
return BytesFile("cropped.png", await cropped.readAsBytes());
|
||||
} catch (e, s) {
|
||||
logError(e, s);
|
||||
snack(context, tr("Failed to execute image cropper!"));
|
||||
return source;
|
||||
} finally {
|
||||
if (file != null && await file.exists()) file.delete();
|
||||
if (cropped != null && await cropped.exists()) cropped.delete();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user