Created
January 27, 2026 22:06
-
-
Save Meghatronics/7180327da85300d0e49b1e3aa5929bd0 to your computer and use it in GitHub Desktop.
PickDeviceImageService - with ImagePicker implementation
This file contains hidden or 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
| import 'dart:io'; | |
| import 'package:flutter/services.dart'; | |
| import 'package:image_picker/image_picker.dart'; | |
| import '../../core_/base_classes/api_response.dart'; | |
| import '../../core_/base_classes/failures.dart'; | |
| import 'pick_device_image_service.dart'; | |
| import 'package:path_provider/path_provider.dart'; | |
| import 'package:path/path.dart' as p; | |
| class ImagePickerService implements PickDeviceImageService { | |
| final ImagePicker imagePicker; | |
| ImagePickerService({required this.imagePicker}); | |
| @override | |
| Future<ApiResponse<File>> chooseImageFromGallery({ | |
| double? sizeLimit, | |
| double percentQuality = 1, | |
| }) async { | |
| try { | |
| final imagePicked = await imagePicker.pickImage( | |
| source: ImageSource.gallery, | |
| imageQuality: (100 * percentQuality).toInt(), | |
| ); | |
| if (imagePicked != null) { | |
| // final image = File(imagePicked.path); | |
| final image = await _persistXFileToTempFile(imagePicked); | |
| if (!await image.exists()) { | |
| return ApiResponse(error: InputFailure('Image not found')); | |
| } else if (sizeLimit != null) { | |
| final imageSize = (await image.stat()).size / 1024; | |
| final sizeLimitInBytes = sizeLimit * 1024; | |
| if (imageSize > sizeLimitInBytes) { | |
| return ApiResponse( | |
| error: InputFailure( | |
| 'Picture must be less than $sizeLimit MB', | |
| ), | |
| ); | |
| } | |
| } | |
| return ApiResponse( | |
| data: image, | |
| ); | |
| } else { | |
| return ApiResponse( | |
| error: InputFailure('No image selected'), | |
| ); | |
| } | |
| } on PlatformException catch (e) { | |
| return ApiResponse( | |
| error: InputFailure(e.message), | |
| ); | |
| } catch (e) { | |
| return ApiResponse( | |
| error: InputFailure('No image selected'), | |
| ); | |
| } | |
| } | |
| @override | |
| Future<ApiResponse<File>> takeImageWithCamera({double percentQuality = 1}) async { | |
| final imagePicked = await imagePicker.pickImage( | |
| source: ImageSource.camera, | |
| imageQuality: (100 * percentQuality).toInt(), | |
| ); | |
| if (imagePicked != null) { | |
| // final image = File(imagePicked.path); | |
| final image = await _persistXFileToTempFile(imagePicked); | |
| if (!await image.exists()) { | |
| return ApiResponse(error: InputFailure('Image not found')); | |
| } | |
| return ApiResponse(data: image); | |
| } else { | |
| return ApiResponse( | |
| error: InputFailure('No picture taken'), | |
| ); | |
| } | |
| } | |
| Future<File> _persistXFileToTempFile(XFile imagePicked) async { | |
| final bytes = await imagePicked.readAsBytes(); | |
| final tempDir = await getTemporaryDirectory(); | |
| final extension = p.extension(imagePicked.path); | |
| final fileName = '${DateTime.now().millisecondsSinceEpoch}$extension'; | |
| final newPath = '${tempDir.path}/$fileName'; | |
| return File(newPath).writeAsBytes(bytes); | |
| } | |
| } |
This file contains hidden or 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
| import 'dart:io'; | |
| import '../../core_/base_classes/api_response.dart'; | |
| abstract class PickDeviceImageService { | |
| Future<ApiResponse<File>> chooseImageFromGallery({ | |
| double? sizeLimit, | |
| double percentQuality = 1, | |
| }); | |
| Future<ApiResponse<File>> takeImageWithCamera({double percentQuality = 1}); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment