detect when cancel is clicked on web

This commit is contained in:
Loc Le 2021-03-30 11:17:52 +07:00 committed by Miguel Ruivo
parent a1878c8816
commit 911ba7be18
2 changed files with 26 additions and 4 deletions

View File

@ -2,6 +2,9 @@
#### Android
- Use MediaStore Opener (which goes through the gallery) instead of default explorer. (Thank you @tmthecoder).
#### Web
- Add event when canceling the picker. (Thank you @letranloc).
## 3.0.0
Adds null safety support ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)).
## 2.1.7

View File

@ -38,7 +38,7 @@ class FilePickerWeb extends FilePicker {
}
@override
Future<FilePickerResult> pickFiles({
Future<FilePickerResult?> pickFiles({
FileType type = FileType.any,
List<String>? allowedExtensions,
bool allowMultiple = false,
@ -47,8 +47,8 @@ class FilePickerWeb extends FilePicker {
bool? withData = true,
bool? withReadStream = false,
}) async {
final Completer<List<PlatformFile>> filesCompleter =
Completer<List<PlatformFile>>();
final Completer<List<PlatformFile>?> filesCompleter =
Completer<List<PlatformFile>?>();
String accept = _fileType(type, allowedExtensions);
InputElement uploadInput = FileUploadInputElement() as InputElement;
@ -108,15 +108,34 @@ class FilePickerWeb extends FilePicker {
});
}
void cancelledEventListener(_) {
window.removeEventListener('focus', cancelledEventListener);
// This listener is called before the input changed event,
// and the `uploadInput.files` value is still null
// Wait for results from js to dart
Future.delayed(Duration(milliseconds: 500)).then((value) {
if (!changeEventTriggered) {
changeEventTriggered = true;
filesCompleter.complete(null);
}
});
}
uploadInput.onChange.listen(changeEventListener);
uploadInput.addEventListener('change', changeEventListener);
// Listen focus event for cancelled
window.addEventListener('focus', cancelledEventListener);
//Add input element to the page body
_target.children.clear();
_target.children.add(uploadInput);
uploadInput.click();
return FilePickerResult(await filesCompleter.future);
final files = await filesCompleter.future;
return files == null ? null : FilePickerResult(files);
}
static String _fileType(FileType type, List<String>? allowedExtensions) {