Adds mobile Safari support (Web) and conditional imports for backwards compatibility with Xcode 11 (iOS)
This commit is contained in:
parent
acf3f1b248
commit
ba19a7eccf
|
@ -1,3 +1,7 @@
|
||||||
|
## 2.0.2
|
||||||
|
Web: Adds mobile Safari support and other minor improvements.
|
||||||
|
iOS: Adds conditional import for backwards compatibility with Xcode 11.
|
||||||
|
|
||||||
## 2.0.1+2
|
## 2.0.1+2
|
||||||
iOS: Addresses an issue that could prevent users from viewing picked media elements (pictures/videos) from gallery on iOS 14.
|
iOS: Addresses an issue that could prevent users from viewing picked media elements (pictures/videos) from gallery on iOS 14.
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,15 @@
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
#import <MediaPlayer/MediaPlayer.h>
|
#import <MediaPlayer/MediaPlayer.h>
|
||||||
#import <Photos/Photos.h>
|
#import <Photos/Photos.h>
|
||||||
#import <PhotosUI/PHPicker.h>
|
|
||||||
#import <MobileCoreServices/MobileCoreServices.h>
|
#import <MobileCoreServices/MobileCoreServices.h>
|
||||||
|
|
||||||
@interface FilePickerPlugin : NSObject<FlutterPlugin, FlutterStreamHandler, UIDocumentPickerDelegate, UITabBarDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, PHPickerViewControllerDelegate>
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
|
||||||
|
#import <PhotosUI/PHPicker.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
@interface FilePickerPlugin : NSObject<FlutterPlugin, FlutterStreamHandler, UIDocumentPickerDelegate, UITabBarDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
|
||||||
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
|
||||||
|
, PHPickerViewControllerDelegate
|
||||||
|
#endif
|
||||||
|
>
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:html';
|
||||||
import 'dart:html' as html;
|
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
|
@ -10,13 +9,32 @@ import 'file_picker_result.dart';
|
||||||
import 'platform_file.dart';
|
import 'platform_file.dart';
|
||||||
|
|
||||||
class FilePickerWeb extends FilePicker {
|
class FilePickerWeb extends FilePicker {
|
||||||
FilePickerWeb._();
|
Element _target;
|
||||||
|
final String _kFilePickerInputsDomId = '__file_picker_web-file-input';
|
||||||
|
|
||||||
static final FilePickerWeb platform = FilePickerWeb._();
|
static final FilePickerWeb platform = FilePickerWeb._();
|
||||||
|
|
||||||
|
FilePickerWeb._() {
|
||||||
|
_target = _ensureInitialized(_kFilePickerInputsDomId);
|
||||||
|
}
|
||||||
|
|
||||||
static void registerWith(Registrar registrar) {
|
static void registerWith(Registrar registrar) {
|
||||||
FilePicker.platform = platform;
|
FilePicker.platform = platform;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Initializes a DOM container where we can host input elements.
|
||||||
|
Element _ensureInitialized(String id) {
|
||||||
|
Element target = querySelector('#$id');
|
||||||
|
if (target == null) {
|
||||||
|
final Element targetElement = Element.tag('flt-file-picker-inputs')
|
||||||
|
..id = id;
|
||||||
|
|
||||||
|
querySelector('body').children.add(targetElement);
|
||||||
|
target = targetElement;
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<FilePickerResult> pickFiles({
|
Future<FilePickerResult> pickFiles({
|
||||||
FileType type = FileType.any,
|
FileType type = FileType.any,
|
||||||
|
@ -30,40 +48,60 @@ class FilePickerWeb extends FilePicker {
|
||||||
Completer<List<PlatformFile>>();
|
Completer<List<PlatformFile>>();
|
||||||
|
|
||||||
String accept = _fileType(type, allowedExtensions);
|
String accept = _fileType(type, allowedExtensions);
|
||||||
html.InputElement uploadInput = html.FileUploadInputElement();
|
InputElement uploadInput = FileUploadInputElement();
|
||||||
uploadInput.draggable = true;
|
uploadInput.draggable = true;
|
||||||
uploadInput.multiple = allowMultiple;
|
uploadInput.multiple = allowMultiple;
|
||||||
uploadInput.accept = accept;
|
uploadInput.accept = accept;
|
||||||
uploadInput.click();
|
|
||||||
|
|
||||||
uploadInput.onChange.listen((e) {
|
bool changeEventTriggered = false;
|
||||||
final files = uploadInput.files;
|
void changeEventListener(e) {
|
||||||
final reader = html.FileReader();
|
if (changeEventTriggered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
changeEventTriggered = true;
|
||||||
|
|
||||||
List<PlatformFile> pickedFiles = [];
|
final List<File> files = uploadInput.files;
|
||||||
|
final List<PlatformFile> pickedFiles = [];
|
||||||
|
|
||||||
reader.onLoadEnd.listen((e) {
|
void addPickedFile(File file, Uint8List bytes, String path) {
|
||||||
final Uint8List bytes =
|
pickedFiles.add(PlatformFile(
|
||||||
Base64Decoder().convert(reader.result.toString().split(",").last);
|
name: file.name,
|
||||||
|
path: path,
|
||||||
pickedFiles.add(
|
size: bytes != null ? bytes.length ~/ 1024 : -1,
|
||||||
PlatformFile(
|
bytes: bytes,
|
||||||
name: uploadInput.value.replaceAll('\\', '/'),
|
));
|
||||||
path: uploadInput.value,
|
|
||||||
size: bytes.length ~/ 1024,
|
|
||||||
bytes: withData ? bytes : null,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (pickedFiles.length >= files.length) {
|
if (pickedFiles.length >= files.length) {
|
||||||
filesCompleter.complete(pickedFiles);
|
filesCompleter.complete(pickedFiles);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
files.forEach((element) {
|
files.forEach((File file) {
|
||||||
reader.readAsDataUrl(element);
|
if (!withData) {
|
||||||
|
final FileReader reader = FileReader();
|
||||||
|
reader.onLoadEnd.listen((e) {
|
||||||
|
addPickedFile(file, null, reader.result);
|
||||||
|
});
|
||||||
|
reader.readAsDataUrl(file);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final FileReader reader = FileReader();
|
||||||
|
reader.onLoadEnd.listen((e) {
|
||||||
|
addPickedFile(file, reader.result, null);
|
||||||
|
});
|
||||||
|
reader.readAsArrayBuffer(file);
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
uploadInput.onChange.listen(changeEventListener);
|
||||||
|
uploadInput.addEventListener('change', changeEventListener);
|
||||||
|
|
||||||
|
//Add input element to the page body
|
||||||
|
_target.children.clear();
|
||||||
|
_target.children.add(uploadInput);
|
||||||
|
uploadInput.click();
|
||||||
|
|
||||||
return FilePickerResult(await filesCompleter.future);
|
return FilePickerResult(await filesCompleter.future);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
name: file_picker
|
name: file_picker
|
||||||
description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support.
|
description: A package that allows you to use a native file explorer to pick single or multiple absolute file paths, with extension filtering support.
|
||||||
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
|
homepage: https://github.com/miguelpruivo/plugins_flutter_file_picker
|
||||||
version: 2.0.1+2
|
version: 2.0.2
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
|
|
Loading…
Reference in New Issue