Skip to content

Commit b55caac

Browse files
committed
Use HTML label element instead of HTML button element
And we'll have one less HTML element and no custom script to click the HTML input element.
1 parent 4757e07 commit b55caac

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

lib/src/flutter_web_file_selector_platform_view_web.dart

+18-36
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,28 @@ class WebFileSelectorPlatformView {
144144

145145
final inputElementID = _inputElementID!;
146146

147-
final divElement = web.HTMLDivElement();
148-
divElement.style.width = '100%';
149-
divElement.style.height = '100%';
147+
// Add HTML label element
148+
final labelElement = web.HTMLLabelElement();
149+
labelElement.htmlFor = inputElementID;
150+
151+
labelElement.style.display = 'block';
152+
labelElement.style.position = 'absolute';
153+
labelElement.style.opacity = '0.001';
154+
labelElement.style.width = '100%';
155+
labelElement.style.height = '100%';
156+
labelElement.style.border = '0';
157+
labelElement.style.backgroundColor = 'transparent';
158+
labelElement.style.cursor = 'pointer';
150159

151160
// Add HTML input element
152161
{
153162
final inputElement = web.HTMLInputElement();
154163
inputElement.id = inputElementID;
155164

156-
inputElement.hidden = true.toJS;
165+
inputElement.style.position = 'absolute';
166+
inputElement.style.inset = '0';
167+
inputElement.style.display = 'none';
168+
157169
inputElement.type = 'file';
158170
inputElement.title = '';
159171
inputElement.accept = accept ?? '';
@@ -194,40 +206,10 @@ class WebFileSelectorPlatformView {
194206
}
195207
});
196208

197-
divElement.append(inputElement);
198-
}
199-
200-
// Add HTML button element
201-
{
202-
final buttonElement = web.HTMLButtonElement();
203-
204-
buttonElement.text = '';
205-
buttonElement.style.opacity = '0.001';
206-
buttonElement.style.width = '100%';
207-
buttonElement.style.height = '100%';
208-
buttonElement.style.border = '0';
209-
buttonElement.style.backgroundColor = 'transparent';
210-
buttonElement.style.cursor = 'pointer';
211-
212-
buttonElement.onClick.listen((event) {
213-
if (onData != null) {
214-
final target = event.target as web.HTMLElement;
215-
if (target.parentElement != null) {
216-
final element = web.document.getElementById(inputElementID);
217-
if (element != null) {
218-
final inputElement = element;
219-
if (inputElement is web.HTMLInputElement) {
220-
inputElement.click();
221-
}
222-
}
223-
}
224-
}
225-
});
226-
227-
divElement.append(buttonElement);
209+
labelElement.append(inputElement);
228210
}
229211

230-
return divElement;
212+
return labelElement;
231213
},
232214
);
233215
}

0 commit comments

Comments
 (0)