Skip to content

Commit

Permalink
Extension functions updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham16g committed Dec 17, 2023
1 parent 088d488 commit 886c514
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 69 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ final imageFile = ImageFile(
path: fileFullPath,
);
```
> **Note:** The package have two Extension functions to convert `XFile` (`image_picker` plugin) and `PlatformFile` (`image_picker` plugin) to `ImageFile` object.
> `final imageFile = convertXFileToImageFile(xFileObject);` and `final imageFile = convertPlatformFileToImageFile(platformFileObject);`. This functions will help you to write your picker logic easily.
### ImageFileView
The `ImageFileView` is a widget which is used to display Image using `ImageFile` object. This will work on web as well as mobile platforms.
Expand Down
6 changes: 3 additions & 3 deletions example/lib/custom/default_custom_example.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:example/custom_examples.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';

import '../picker.dart';

class DefaultCustomExample extends StatefulWidget {
const DefaultCustomExample({Key? key}) : super(key: key);

Expand All @@ -14,8 +15,7 @@ class _DefaultCustomExampleState extends State<DefaultCustomExample> {
final controller = MultiImagePickerController(
maxImages: 12,
picker: (bool allowMultiple) {
return imagePickerExtension(
imagePicker: ImagePicker(), allowMultiple: allowMultiple);
return pickImagesUsingImagePicker(allowMultiple);
},
);

Expand Down
6 changes: 3 additions & 3 deletions example/lib/custom/full_custom_example.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:example/custom_examples.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';

import '../picker.dart';

class FullCustomExample extends StatefulWidget {
const FullCustomExample({Key? key}) : super(key: key);

Expand All @@ -14,8 +15,7 @@ class _FullCustomExampleState extends State<FullCustomExample> {
final controller = MultiImagePickerController(
maxImages: 12,
picker: (bool allowMultiple) async {
return await imagePickerExtension(
imagePicker: ImagePicker(), allowMultiple: allowMultiple);
return await pickImagesUsingImagePicker(allowMultiple);
},
);

Expand Down
9 changes: 5 additions & 4 deletions example/lib/custom/selectable_custom_example.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:example/custom_examples.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';

import '../picker.dart';

class SelectableCustomExample extends StatefulWidget {
const SelectableCustomExample({Key? key}) : super(key: key);

Expand All @@ -27,9 +28,9 @@ class _SelectableCustomExampleState extends State<SelectableCustomExample> {
}

final controller = MultiImagePickerController(
maxImages: 10,
picker: (allowMultiple) => imagePickerExtension(
imagePicker: ImagePicker(), allowMultiple: allowMultiple));
maxImages: 10,
picker: (allowMultiple) => pickImagesUsingImagePicker(allowMultiple),
);

@override
Widget build(BuildContext context) {
Expand Down
8 changes: 5 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:example/custom_examples.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';

import 'picker.dart';

void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
Expand Down Expand Up @@ -32,8 +33,9 @@ class DemoPage extends StatefulWidget {
class _DemoPageState extends State<DemoPage> {
final controller = MultiImagePickerController(
maxImages: 10,
picker: (allowMultiple) => imagePickerExtension(
imagePicker: ImagePicker(), allowMultiple: allowMultiple));
picker: (allowMultiple) async {
return await pickImagesUsingImagePicker(allowMultiple);
});

@override
Widget build(BuildContext context) {
Expand Down
19 changes: 10 additions & 9 deletions example/lib/picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ Future<List<ImageFile>> pickImagesUsingImagePicker(bool allowMultiple) async {
}
}
if (xFiles.isNotEmpty) {
return xFiles
.map<ImageFile>((e) => ImageFile(
UniqueKey().toString(),
name: e.name,
extension: e.name.contains(".") ? e.name.split(".").last : "",
path: e.path,
))
.toList();
return xFiles.map<ImageFile>((e) => convertXFileToImageFile(e)).toList();
}
return [];
}
Expand Down Expand Up @@ -60,6 +53,14 @@ Future<List<ImageFile>> pickFilesUsingFilePicker(bool allowMultiple) async {
withData: kIsWeb,
);
if (result != null && result.files.isNotEmpty) {
return result.files
.map(
(e) => convertPlatformFileToImageFile(e),
)
.toList();

/*
the below code can be used if not using convertPlatformFileToImageFile extension.
return result.files
.map(
(e) => ImageFile(UniqueKey().toString(),
Expand All @@ -68,7 +69,7 @@ Future<List<ImageFile>> pickFilesUsingFilePicker(bool allowMultiple) async {
bytes: e.bytes,
path: !kIsWeb ? e.path : null),
)
.toList();
.toList();*/
}
return [];
}
68 changes: 21 additions & 47 deletions lib/src/picker_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,29 @@ import 'package:flutter/foundation.dart';

import 'image_file.dart';

Future<List<ImageFile>> imagePickerExtension({
required dynamic imagePicker,
required bool allowMultiple,
double? maxWidth,
double? maxHeight,
bool requestFullMetaData = true,
}) async {
final xFiles = await imagePicker.pickMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
requestFullMetadata: requestFullMetaData);
if (xFiles.isNotEmpty) {
return xFiles
.map<ImageFile>((e) => ImageFile(
UniqueKey().toString(),
name: e.name,
extension: e.name.contains(".") ? e.name.split(".").last : "",
path: e.path,
))
.toList();
ImageFile convertXFileToImageFile(dynamic xFile) {
try {
return ImageFile(
UniqueKey().toString(),
name: xFile.name,
extension: xFile.name.contains(".") ? xFile.name.split(".").last : "",
path: xFile.path,
);
} catch (e) {
throw Exception(
"The object pass in `convertXFileToImageFile` is not type XFile. Provided object is of type: `${xFile.runtimeType}`");
}
return [];
}

Future<List<ImageFile>> filePickerExtension({
required dynamic filePicker,
required bool allowMultiple,
bool withData = false,
bool withReadStream = false,
List<String> allowedExtensions = /*const ['png', 'jpeg', 'jpg']*/ const [],
}) async {
final result = await filePicker.pickFiles(
allowMultiple: allowMultiple,
// type: FileType.custom,
withData: kIsWeb ? true : withData,
withReadStream: withReadStream);
if (result != null && result.files.isNotEmpty) {
print("files got: ${result.count}");
return result.files
/*.where((e) =>
e.extension != null &&
allowedExtensions.contains(e.extension?.toLowerCase()))
*/
.map((e) => ImageFile(UniqueKey().toString(),
name: e.name,
extension: e.extension!,
bytes: e.bytes,
path: !kIsWeb ? e.path : null))
.toList();
ImageFile convertPlatformFileToImageFile(dynamic platformFile) {
try {
return ImageFile(UniqueKey().toString(),
name: platformFile.name,
extension: platformFile.extension!,
bytes: platformFile.bytes,
path: !kIsWeb ? platformFile.path : null);
} catch (e) {
throw Exception(
"The object pass in `convertPlatformFileToImageFile` is not type PlatformFile. Provided object is of type: `${platformFile.runtimeType}`");
}
return [];
}

0 comments on commit 886c514

Please sign in to comment.