3
3
// found in the LICENSE file.
4
4
5
5
import 'dart:async' ;
6
- import 'dart:html ' ;
6
+ import 'dart:js_interop ' ;
7
7
8
8
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart' ;
9
9
import 'package:flutter/foundation.dart' show visibleForTesting;
10
10
import 'package:flutter/services.dart' ;
11
+ import 'package:web/helpers.dart' ;
11
12
12
13
/// Class to manipulate the DOM with the intention of reading files from it.
13
14
class DomHelper {
14
15
/// Default constructor, initializes the container DOM element.
15
16
DomHelper () {
16
17
final Element body = querySelector ('body' )! ;
17
- body.children. add (_container);
18
+ body.appendChild (_container);
18
19
}
19
20
20
- final Element _container = Element . tag ('file-selector' );
21
+ final Element _container = createElementTag ('file-selector' );
21
22
22
23
/// Sets the <input /> attributes and waits for a file to be selected.
23
24
Future <List <XFile >> getFiles ({
24
25
String accept = '' ,
25
26
bool multiple = false ,
26
- @visibleForTesting FileUploadInputElement ? input,
27
+ @visibleForTesting HTMLInputElement ? input,
27
28
}) {
28
29
final Completer <List <XFile >> completer = Completer <List <XFile >>();
29
- final FileUploadInputElement inputElement =
30
- input ?? FileUploadInputElement ();
30
+ final HTMLInputElement inputElement =
31
+ input ?? (createElementTag ('input' ) as HTMLInputElement )
32
+ ..type = 'file' ;
31
33
32
- _container.children. add (
34
+ _container.appendChild (
33
35
inputElement
34
36
..accept = accept
35
37
..multiple = multiple,
36
38
);
37
39
38
40
inputElement.onChange.first.then ((_) {
39
- final List <XFile > files =
40
- inputElement.files! .map (_convertFileToXFile).toList ();
41
+ final List <XFile > files = Iterable <File >.generate (
42
+ inputElement.files! .length,
43
+ (int i) => inputElement.files! .item (i)! )
44
+ .map (_convertFileToXFile)
45
+ .toList ();
41
46
inputElement.remove ();
42
47
completer.complete (files);
43
48
});
@@ -52,10 +57,13 @@ class DomHelper {
52
57
completer.completeError (platformException);
53
58
});
54
59
55
- inputElement.addEventListener ('cancel' , (Event event) {
56
- inputElement.remove ();
57
- completer.complete (< XFile > []);
58
- });
60
+ inputElement.addEventListener (
61
+ 'cancel' ,
62
+ (Event event) {
63
+ inputElement.remove ();
64
+ completer.complete (< XFile > []);
65
+ }.toJS,
66
+ );
59
67
60
68
// TODO(dit): Reimplement this with the showPicker() API, https://github.com/flutter/flutter/issues/130365
61
69
inputElement.click ();
@@ -64,10 +72,12 @@ class DomHelper {
64
72
}
65
73
66
74
XFile _convertFileToXFile (File file) => XFile (
67
- Url .createObjectUrl (file),
75
+ // TODO(srujzs): This is necessary in order to support package:web 0.4.0.
76
+ // This was not needed with 0.3.0, hence the lint.
77
+ // ignore: unnecessary_cast
78
+ URL .createObjectURL (file as JSObject ),
68
79
name: file.name,
69
80
length: file.size,
70
- lastModified: DateTime .fromMillisecondsSinceEpoch (
71
- file.lastModified ?? DateTime .now ().millisecondsSinceEpoch),
81
+ lastModified: DateTime .fromMillisecondsSinceEpoch (file.lastModified),
72
82
);
73
83
}
0 commit comments