-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WASM support for Flutter web #1481
Changes from 8 commits
1ded925
100ec8f
5f40665
d580dbb
b34d9dd
ca2f19f
ad868de
2e15033
f63f787
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||
import 'dart:async'; | ||||||
import 'dart:html'; | ||||||
import 'dart:js_interop'; | ||||||
import 'package:web/web.dart'; | ||||||
import 'dart:typed_data'; | ||||||
|
||||||
import 'package:file_picker/file_picker.dart'; | ||||||
|
@@ -23,12 +24,13 @@ class FilePickerWeb extends FilePicker { | |||||
|
||||||
/// Initializes a DOM container where we can host input elements. | ||||||
Element _ensureInitialized(String id) { | ||||||
Element? target = querySelector('#$id'); | ||||||
Element? target = document.querySelector('#$id'); | ||||||
if (target == null) { | ||||||
final Element targetElement = Element.tag('flt-file-picker-inputs') | ||||||
..id = id; | ||||||
final Element targetElement = document.createElement( | ||||||
'flt-file-picker-inputs', | ||||||
)..id = id; | ||||||
|
||||||
querySelector('body')!.children.add(targetElement); | ||||||
document.querySelector('body')!.children.add(targetElement); | ||||||
target = targetElement; | ||||||
} | ||||||
return target; | ||||||
|
@@ -58,7 +60,9 @@ class FilePickerWeb extends FilePicker { | |||||
Completer<List<PlatformFile>?>(); | ||||||
|
||||||
String accept = _fileType(type, allowedExtensions); | ||||||
InputElement uploadInput = FileUploadInputElement() as InputElement; | ||||||
HTMLInputElement uploadInput = | ||||||
document.createElement('input') as HTMLInputElement; | ||||||
uploadInput.type = 'file'; | ||||||
uploadInput.draggable = true; | ||||||
uploadInput.multiple = allowMultiple; | ||||||
uploadInput.accept = accept; | ||||||
|
@@ -70,13 +74,13 @@ class FilePickerWeb extends FilePicker { | |||||
onFileLoading(FilePickerStatus.picking); | ||||||
} | ||||||
|
||||||
void changeEventListener(e) async { | ||||||
void changeEventListener(Event e) async { | ||||||
if (changeEventTriggered) { | ||||||
return; | ||||||
} | ||||||
changeEventTriggered = true; | ||||||
|
||||||
final List<File> files = uploadInput.files!; | ||||||
final FileList files = uploadInput.files!; | ||||||
final List<PlatformFile> pickedFiles = []; | ||||||
|
||||||
void addPickedFile( | ||||||
|
@@ -101,7 +105,12 @@ class FilePickerWeb extends FilePicker { | |||||
} | ||||||
} | ||||||
|
||||||
for (File file in files) { | ||||||
for (var i = 0; i < files.length; i++) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored |
||||||
final File? file = files.item(i); | ||||||
if (file == null) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
if (withReadStream) { | ||||||
addPickedFile(file, null, null, _openFileReadStream(file)); | ||||||
continue; | ||||||
|
@@ -110,16 +119,18 @@ class FilePickerWeb extends FilePicker { | |||||
if (!withData) { | ||||||
final FileReader reader = FileReader(); | ||||||
reader.onLoadEnd.listen((e) { | ||||||
addPickedFile(file, null, reader.result as String?, null); | ||||||
String? result = (reader.result as JSString?)?.toDart; | ||||||
addPickedFile(file, null, result, null); | ||||||
}); | ||||||
reader.readAsDataUrl(file); | ||||||
reader.readAsDataURL(file); | ||||||
continue; | ||||||
} | ||||||
|
||||||
final syncCompleter = Completer<void>(); | ||||||
final FileReader reader = FileReader(); | ||||||
reader.onLoadEnd.listen((e) { | ||||||
addPickedFile(file, reader.result as Uint8List?, null, null); | ||||||
ByteBuffer? byteBuffer = (reader.result as JSArrayBuffer?)?.toDart; | ||||||
addPickedFile(file, byteBuffer?.asUint8List(), null, null); | ||||||
syncCompleter.complete(); | ||||||
}); | ||||||
reader.readAsArrayBuffer(file); | ||||||
|
@@ -129,8 +140,8 @@ class FilePickerWeb extends FilePicker { | |||||
} | ||||||
} | ||||||
|
||||||
void cancelledEventListener(_) { | ||||||
window.removeEventListener('focus', cancelledEventListener); | ||||||
void cancelledEventListener(Event _) { | ||||||
window.removeEventListener('focus', cancelledEventListener.toJS); | ||||||
|
||||||
// This listener is called before the input changed event, | ||||||
// and the `uploadInput.files` value is still null | ||||||
|
@@ -144,14 +155,18 @@ class FilePickerWeb extends FilePicker { | |||||
} | ||||||
|
||||||
uploadInput.onChange.listen(changeEventListener); | ||||||
uploadInput.addEventListener('change', changeEventListener); | ||||||
uploadInput.addEventListener('cancel', cancelledEventListener); | ||||||
uploadInput.addEventListener('change', changeEventListener.toJS); | ||||||
uploadInput.addEventListener('cancel', cancelledEventListener.toJS); | ||||||
|
||||||
// Listen focus event for cancelled | ||||||
window.addEventListener('focus', cancelledEventListener); | ||||||
window.addEventListener('focus', cancelledEventListener.toJS); | ||||||
|
||||||
//Add input element to the page body | ||||||
_target.children.clear(); | ||||||
var firstChild = _target.firstChild; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to use the real type instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refactored |
||||||
while (firstChild != null) { | ||||||
_target.removeChild(firstChild); | ||||||
firstChild = _target.firstChild; | ||||||
} | ||||||
_target.children.add(uploadInput); | ||||||
uploadInput.click(); | ||||||
|
||||||
|
@@ -193,7 +208,7 @@ class FilePickerWeb extends FilePicker { | |||||
: start + _readStreamChunkSize; | ||||||
final blob = file.slice(start, end); | ||||||
reader.readAsArrayBuffer(blob); | ||||||
await reader.onLoad.first; | ||||||
await EventStreamProviders.loadEvent.forTarget(reader).first; | ||||||
yield reader.result as List<int>; | ||||||
start += _readStreamChunkSize; | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor calls document.createElement under the hood.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored