mirror of
https://github.com/Iconica-Development/flutter_media_picker.git
synced 2025-05-19 00:43:45 +02:00
feat: add file picker
This commit is contained in:
parent
cd44468067
commit
ef3a839b7f
8 changed files with 120 additions and 11 deletions
|
@ -97,6 +97,21 @@ class _MediaPickerState extends ConsumerState<MediaPickerPage> {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
MediaPickerInputFile(
|
||||||
|
pickFile: mediaService.pickFile,
|
||||||
|
fileExtensions: [
|
||||||
|
'pdf',
|
||||||
|
'doc',
|
||||||
|
'png',
|
||||||
|
'jpg',
|
||||||
|
'docx',
|
||||||
|
'bmp',
|
||||||
|
'gif',
|
||||||
|
],
|
||||||
|
checkPageSettings: {
|
||||||
|
'title': 'Bestand delen',
|
||||||
|
},
|
||||||
|
),
|
||||||
],
|
],
|
||||||
mediaCheckPage: (Widget displayResult,
|
mediaCheckPage: (Widget displayResult,
|
||||||
Map<String, dynamic>? inputSettings,
|
Map<String, dynamic>? inputSettings,
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
|
||||||
abstract class MediaPickerService {
|
abstract class MediaPickerService {
|
||||||
/// Returns [Uint8List] based on given [ImageSource].
|
/// Returns [Uint8List] based on given [ImageSource].
|
||||||
Future<Uint8List?> pickImageFile();
|
Future<Uint8List?> pickImageFile();
|
||||||
|
@ -12,5 +14,5 @@ abstract class MediaPickerService {
|
||||||
Future<Uint8List?> pickVideoFile();
|
Future<Uint8List?> pickVideoFile();
|
||||||
|
|
||||||
/// Returns [Uint8List] based on given [File].
|
/// Returns [Uint8List] based on given [File].
|
||||||
Future<Uint8List?> pickFile();
|
Future<FilePickerResult?> pickFile(List<String> fileExtensions);
|
||||||
}
|
}
|
||||||
|
|
78
lib/src/inputs/input_file.dart
Normal file
78
lib/src/inputs/input_file.dart
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
// SPDX-FileCopyrightText: 2022 Iconica
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_media_picker/src/abstracts/media_picker_input.dart';
|
||||||
|
|
||||||
|
import 'package:path/path.dart' as path;
|
||||||
|
|
||||||
|
import 'package:flutter_media_picker/src/media_result.dart';
|
||||||
|
|
||||||
|
/// Input for photo used by [MediaPicker].
|
||||||
|
class MediaPickerInputFile implements MediaPickerInput {
|
||||||
|
MediaPickerInputFile({
|
||||||
|
this.label = "File",
|
||||||
|
this.fileExtensions = const ['pdf', 'jpg', 'png'],
|
||||||
|
this.checkPageSettings,
|
||||||
|
this.onComplete,
|
||||||
|
this.pickFile,
|
||||||
|
});
|
||||||
|
|
||||||
|
final Future<FilePickerResult?> Function(List<String>)? pickFile;
|
||||||
|
final List<String> fileExtensions;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String label;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<MediaResult> onPressed(BuildContext context) async {
|
||||||
|
var file = await pickFile?.call(fileExtensions);
|
||||||
|
|
||||||
|
if (file != null && file.files.first.bytes != null) {
|
||||||
|
return MediaResult(
|
||||||
|
fileValue: file.files.first.bytes,
|
||||||
|
fileType: path.extension(file.files.first.name),
|
||||||
|
fileName: file.files.first.name,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return MediaResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Widget> displayResult(MediaResult result) async {
|
||||||
|
if (result.fileValue != null) {
|
||||||
|
switch (result.fileType) {
|
||||||
|
case '.png':
|
||||||
|
case '.jpg':
|
||||||
|
case '.jpeg':
|
||||||
|
case '.svg':
|
||||||
|
case '.webp':
|
||||||
|
case '.bmp':
|
||||||
|
case '.gif':
|
||||||
|
return Image.memory(
|
||||||
|
result.fileValue!,
|
||||||
|
height: 250,
|
||||||
|
);
|
||||||
|
case '.pdf':
|
||||||
|
case '.doc':
|
||||||
|
case '.docx':
|
||||||
|
return Text(result.fileName!);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
// return Image.memory(
|
||||||
|
// result.fileValue!,
|
||||||
|
// height: 250,
|
||||||
|
// );
|
||||||
|
}
|
||||||
|
|
||||||
|
return Container();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic>? checkPageSettings;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void Function(MediaResult value)? onComplete;
|
||||||
|
}
|
|
@ -26,13 +26,7 @@ class MediaPickerInputPhoto implements MediaPickerInput {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<MediaResult> onPressed(BuildContext context) async {
|
Future<MediaResult> onPressed(BuildContext context) async {
|
||||||
var image = await showModalBottomSheet<Uint8List?>(
|
var image = await pickFile?.call();
|
||||||
context: context,
|
|
||||||
backgroundColor: Colors.white,
|
|
||||||
builder: (BuildContext context) => const ImagePicker(
|
|
||||||
imagePickerConfig: ImagePickerConfig(
|
|
||||||
maxHeight: 300, maxWidth: 200, imageQuality: 40),
|
|
||||||
));
|
|
||||||
|
|
||||||
if (image != null && image.isNotEmpty) {
|
if (image != null && image.isNotEmpty) {
|
||||||
return MediaResult(
|
return MediaResult(
|
||||||
|
|
|
@ -6,3 +6,4 @@ export 'input_audio.dart';
|
||||||
export 'input_photo.dart';
|
export 'input_photo.dart';
|
||||||
export 'input_text.dart';
|
export 'input_text.dart';
|
||||||
export 'input_video.dart';
|
export 'input_video.dart';
|
||||||
|
export 'input_file.dart';
|
||||||
|
|
|
@ -187,6 +187,7 @@ class MediaPicker extends ConsumerWidget {
|
||||||
MediaResult result = MediaResult(
|
MediaResult result = MediaResult(
|
||||||
fileValue: content.fileValue,
|
fileValue: content.fileValue,
|
||||||
textValue: content.textValue,
|
textValue: content.textValue,
|
||||||
|
fileType: content.fileType,
|
||||||
checkPageResults: results,
|
checkPageResults: results,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ class MediaResult {
|
||||||
this.textValue,
|
this.textValue,
|
||||||
this.fileValue,
|
this.fileValue,
|
||||||
this.checkPageResults,
|
this.checkPageResults,
|
||||||
|
this.fileType,
|
||||||
|
this.fileName,
|
||||||
});
|
});
|
||||||
|
|
||||||
/// For textfield returns actual text,
|
/// For textfield returns actual text,
|
||||||
|
@ -21,4 +23,10 @@ class MediaResult {
|
||||||
|
|
||||||
/// Returns the values from the checkPageResults if checkpage is set.
|
/// Returns the values from the checkPageResults if checkpage is set.
|
||||||
final Map<String, dynamic>? checkPageResults;
|
final Map<String, dynamic>? checkPageResults;
|
||||||
|
|
||||||
|
/// Returns the filetype when a file is selected with the FilePicker.
|
||||||
|
final String? fileType;
|
||||||
|
|
||||||
|
/// Returns the file name when a file is selected with the FilePicker.
|
||||||
|
final String? fileName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_media_picker/src/abstracts/media_picker_service.dart';
|
import 'package:flutter_media_picker/src/abstracts/media_picker_service.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
@ -17,7 +18,6 @@ class MediaPickerFileService implements MediaPickerService {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Uint8List?> pickImageFile() async {
|
Future<Uint8List?> pickImageFile() async {
|
||||||
|
|
||||||
var image = await ImagePicker().pickImage(source: ImageSource.camera);
|
var image = await ImagePicker().pickImage(source: ImageSource.camera);
|
||||||
|
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
|
@ -50,7 +50,17 @@ class MediaPickerFileService implements MediaPickerService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Uint8List?> pickFile() async {
|
Future<FilePickerResult?> pickFile(List<String> extensions) async {
|
||||||
return null;
|
var file = await FilePicker.platform.pickFiles(
|
||||||
|
withData: true,
|
||||||
|
type: FileType.custom,
|
||||||
|
allowedExtensions: extensions,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (file != null) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Future.value(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue