2021-03-13 16:50:59 +00:00
|
|
|
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
|
2021-03-13 17:11:28 +00:00
|
|
|
Future<BytesFile> showImageCropper(BuildContext context, BytesFile source,
|
2022-03-10 18:39:57 +00:00
|
|
|
{CropAspectRatio? aspectRatio}) async {
|
2021-03-13 16:50:59 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
File? file;
|
|
|
|
File? cropped;
|
2021-03-13 16:50:59 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
file = await generateTemporaryFile();
|
2022-03-10 18:39:57 +00:00
|
|
|
await file.writeAsBytes(source.bytes!);
|
2021-03-13 16:50:59 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
File? cropped = await ImageCropper().cropImage(
|
2021-03-13 16:50:59 +00:00
|
|
|
sourcePath: file.absolute.path,
|
|
|
|
compressFormat: ImageCompressFormat.png,
|
2021-03-13 17:11:28 +00:00
|
|
|
aspectRatio: aspectRatio,
|
2021-03-13 16:50:59 +00:00
|
|
|
androidUiSettings: AndroidUiSettings(
|
|
|
|
toolbarColor: Colors.black,
|
|
|
|
toolbarTitle: tr("Crop Photo"),
|
|
|
|
toolbarWidgetColor: Colors.white,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
if (cropped == null) return source;
|
2021-03-13 16:50:59 +00:00
|
|
|
|
|
|
|
return BytesFile("cropped.png", await cropped.readAsBytes());
|
|
|
|
} catch (e, s) {
|
|
|
|
logError(e, s);
|
2022-03-10 18:39:57 +00:00
|
|
|
snack(context, tr("Failed to execute image cropper!")!);
|
2021-03-13 16:50:59 +00:00
|
|
|
return source;
|
|
|
|
} finally {
|
|
|
|
if (file != null && await file.exists()) file.delete();
|
|
|
|
if (cropped != null && await cropped.exists()) cropped.delete();
|
|
|
|
}
|
|
|
|
}
|