You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.6 KiB
Dart
92 lines
2.6 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:mc_common_app/utils/app_permission_handler.dart';
|
|
|
|
abstract class CommonAppServices {
|
|
Future<List<File>> pickMultipleImages();
|
|
|
|
Future<File?> pickImageFromPhone(int sourceFlag);
|
|
|
|
Future<List<File>?> pickMultipleFiles(BuildContext context);
|
|
|
|
Future<File?> pickFile(BuildContext context,
|
|
{required FileType fileType, List<String>? allowedExtensions});
|
|
}
|
|
|
|
class CommonServicesImp implements CommonAppServices {
|
|
@override
|
|
Future<File?> pickImageFromPhone(int sourceFlag) async {
|
|
final picker = ImagePicker();
|
|
final pickedImage = await picker.pickImage(
|
|
source: sourceFlag == 0 ? ImageSource.camera : ImageSource.gallery,
|
|
);
|
|
final pickedImageFile = File(pickedImage!.path);
|
|
return pickedImageFile;
|
|
}
|
|
|
|
@override
|
|
Future<List<File>?> pickMultipleFiles(BuildContext context) async {
|
|
FilePickerResult? result;
|
|
|
|
final status = await AppPermissions.checkStoragePermissions(context);
|
|
if (status) {
|
|
result = await FilePicker.platform.pickFiles(allowMultiple: true, type: FileType.custom, allowedExtensions: ['pdf']);
|
|
}
|
|
|
|
List<File> pickedFiles = [];
|
|
if (result != null) {
|
|
for (var element in result.files) {
|
|
if (element.path != null) {
|
|
pickedFiles.add(File(element.path!));
|
|
}
|
|
}
|
|
}
|
|
return pickedFiles;
|
|
}
|
|
|
|
@override
|
|
Future<List<File>> pickMultipleImages() async {
|
|
final picker = ImagePicker();
|
|
final pickedImagesXFiles = await picker.pickMultiImage();
|
|
|
|
List<File> pickedImages = [];
|
|
if (pickedImagesXFiles == null) {
|
|
return [];
|
|
}
|
|
if (pickedImagesXFiles.isEmpty) {
|
|
return [];
|
|
}
|
|
for (var element in pickedImagesXFiles) {
|
|
pickedImages.add(File(element.path));
|
|
}
|
|
return pickedImages;
|
|
}
|
|
|
|
@override
|
|
Future<File?> pickFile(BuildContext context,
|
|
{required FileType fileType, List<String>? allowedExtensions}) async {
|
|
FilePickerResult? result;
|
|
|
|
final status = await AppPermissions.checkStoragePermissions(context);
|
|
if (status) {
|
|
result = await FilePicker.platform.pickFiles(
|
|
allowMultiple: true,
|
|
type: fileType,
|
|
allowedExtensions: allowedExtensions);
|
|
}
|
|
|
|
List<File> pickedFiles = [];
|
|
if (result != null) {
|
|
for (var element in result.files) {
|
|
if (element.path != null) {
|
|
pickedFiles.add(File(element.path!));
|
|
}
|
|
}
|
|
}
|
|
return pickedFiles.length > 0 ? pickedFiles.first : null;
|
|
}
|
|
}
|