From 846c613fbd92d6cf88e0e4552d188133bdf837c5 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 22 Jan 2024 16:44:27 -0800 Subject: [PATCH] feat: add image picking functionality for web --- app/lib/utils/utils_web.dart | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/app/lib/utils/utils_web.dart b/app/lib/utils/utils_web.dart index d128f62..f1e8921 100644 --- a/app/lib/utils/utils_web.dart +++ b/app/lib/utils/utils_web.dart @@ -113,9 +113,28 @@ class UtilsPlatform { } static Future pickImage() async { - Completer completer = Completer(); - // todo: implement pickImage - return completer.future; + // Create an input element for file upload + final uploadInput = html.FileUploadInputElement(); + uploadInput.accept = 'image/*'; // Accept only image files + + // Trigger the file picker dialog + uploadInput.click(); + + // Wait for the user to select a file + await uploadInput.onChange.first; + + // Get the selected file + final file = uploadInput.files!.first; + + // Read the file as data URL + final reader = html.FileReader(); + reader.readAsDataUrl(file); + + // Wait for the file to be read + await reader.onLoadEnd.first; + + // Return the result + return reader.result; } static Future startFilePicker() async {