forked from PalisadoesFoundation/talawa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ImageService (PalisadoesFoundation#2265)
* resolved conflicts * fetchmore result typecast * added ImageService and written its tests * added coverage to missing lines. * Minor fix * fixed failing test * added ImageService and written its tests * resolved requested changes. * added changes to locator.dart * add debug print. * resolve requested changes. * resolved requested changes.
- Loading branch information
1 parent
ece173d
commit 6f6201b
Showing
10 changed files
with
308 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:image_cropper/image_cropper.dart'; | ||
import 'package:talawa/locator.dart'; | ||
|
||
/// ImageService class provides different functions as service in the context of Images. | ||
/// | ||
/// Services include: | ||
/// * `cropImage` | ||
/// * `convertToBase64` | ||
class ImageService { | ||
/// Global instance of ImageCropper. | ||
final ImageCropper _imageCropper = locator<ImageCropper>(); | ||
|
||
/// Crops the image selected by the user. | ||
/// | ||
/// **params**: | ||
/// * `imageFile`: the image file to be cropped. | ||
/// | ||
/// **returns**: | ||
/// * `Future<File?>`: the image after been cropped. | ||
/// | ||
/// **throws**: | ||
/// - `Exception`: If an error occurs during the image cropping process. | ||
Future<File?> cropImage({required File imageFile}) async { | ||
// try, to crop the image and returns a File with cropped image path. | ||
try { | ||
final CroppedFile? croppedImage = await _imageCropper.cropImage( | ||
sourcePath: imageFile.path, | ||
aspectRatioPresets: [ | ||
CropAspectRatioPreset.square, | ||
CropAspectRatioPreset.original, | ||
], | ||
uiSettings: [ | ||
AndroidUiSettings( | ||
toolbarTitle: 'Crop Image', | ||
toolbarColor: const Color(0xff18191A), | ||
toolbarWidgetColor: Colors.white, | ||
backgroundColor: Colors.black, | ||
cropGridColor: Colors.white, | ||
initAspectRatio: CropAspectRatioPreset.original, | ||
lockAspectRatio: false, | ||
), | ||
IOSUiSettings( | ||
minimumAspectRatio: 1.0, | ||
), | ||
], | ||
); | ||
|
||
if (croppedImage != null) { | ||
return File(croppedImage.path); | ||
} | ||
} catch (e) { | ||
throw Exception( | ||
"ImageService : $e.", | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// Converts the image into Base64 format. | ||
/// | ||
/// **params**: | ||
/// * `file`: Image as a File object. | ||
/// | ||
/// **returns**: | ||
/// * `Future<String?>`: image in string format | ||
Future<String?> convertToBase64(File file) async { | ||
try { | ||
final List<int> bytes = await file.readAsBytes(); | ||
final String base64String = base64Encode(bytes); | ||
return base64String; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.