detect when cancel is clicked on web
This commit is contained in:
parent
a1878c8816
commit
911ba7be18
|
@ -2,6 +2,9 @@
|
||||||
#### Android
|
#### Android
|
||||||
- Use MediaStore Opener (which goes through the gallery) instead of default explorer. (Thank you @tmthecoder).
|
- 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
|
## 3.0.0
|
||||||
Adds null safety support ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)).
|
Adds null safety support ([#510](https://github.com/miguelpruivo/flutter_file_picker/issues/510)).
|
||||||
## 2.1.7
|
## 2.1.7
|
||||||
|
|
|
@ -38,7 +38,7 @@ class FilePickerWeb extends FilePicker {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<FilePickerResult> pickFiles({
|
Future<FilePickerResult?> pickFiles({
|
||||||
FileType type = FileType.any,
|
FileType type = FileType.any,
|
||||||
List<String>? allowedExtensions,
|
List<String>? allowedExtensions,
|
||||||
bool allowMultiple = false,
|
bool allowMultiple = false,
|
||||||
|
@ -47,8 +47,8 @@ class FilePickerWeb extends FilePicker {
|
||||||
bool? withData = true,
|
bool? withData = true,
|
||||||
bool? withReadStream = false,
|
bool? withReadStream = false,
|
||||||
}) async {
|
}) async {
|
||||||
final Completer<List<PlatformFile>> filesCompleter =
|
final Completer<List<PlatformFile>?> filesCompleter =
|
||||||
Completer<List<PlatformFile>>();
|
Completer<List<PlatformFile>?>();
|
||||||
|
|
||||||
String accept = _fileType(type, allowedExtensions);
|
String accept = _fileType(type, allowedExtensions);
|
||||||
InputElement uploadInput = FileUploadInputElement() as InputElement;
|
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.onChange.listen(changeEventListener);
|
||||||
uploadInput.addEventListener('change', changeEventListener);
|
uploadInput.addEventListener('change', changeEventListener);
|
||||||
|
|
||||||
|
// Listen focus event for cancelled
|
||||||
|
window.addEventListener('focus', cancelledEventListener);
|
||||||
|
|
||||||
//Add input element to the page body
|
//Add input element to the page body
|
||||||
_target.children.clear();
|
_target.children.clear();
|
||||||
_target.children.add(uploadInput);
|
_target.children.add(uploadInput);
|
||||||
uploadInput.click();
|
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) {
|
static String _fileType(FileType type, List<String>? allowedExtensions) {
|
||||||
|
|
Loading…
Reference in New Issue