From e8d98e827422c75b84b5fb724918b3148702e177 Mon Sep 17 00:00:00 2001 From: nyne Date: Tue, 28 Oct 2025 18:42:59 +0800 Subject: [PATCH] Add support for ArrayBuffer to showInputDialog. --- assets/init.js | 2 +- lib/components/js_ui.dart | 19 ++++++++++++++++--- lib/components/message.dart | 6 ++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/assets/init.js b/assets/init.js index d4b503f..2c38ad1 100644 --- a/assets/init.js +++ b/assets/init.js @@ -1334,7 +1334,7 @@ let UI = { * Show an input dialog * @param title {string} * @param validator {(string) => string | null | undefined} - A function that validates the input. If the function returns a string, the dialog will show the error message. - * @param image {string?} - Available since 1.4.6. An optional image to show in the dialog. You can use this to show a captcha. + * @param image {string | ArrayBuffer | null | undefined} - Since 1.4.6, you can pass an image url to show an image in the dialog. Since 1.5.3, you can also pass an ArrayBuffer to show a custom image. * @returns {Promise} - The input value. If the dialog is canceled, return null. */ showInputDialog: (title, validator, image) => { diff --git a/lib/components/js_ui.dart b/lib/components/js_ui.dart index 0a2e0cf..2256fe4 100644 --- a/lib/components/js_ui.dart +++ b/lib/components/js_ui.dart @@ -1,3 +1,5 @@ +import 'dart:typed_data'; + import 'package:flutter/material.dart'; import 'package:flutter_qjs/flutter_qjs.dart'; import 'package:url_launcher/url_launcher_string.dart'; @@ -40,7 +42,6 @@ mixin class JsUiApi { var image = message['image']; if (title is! String) return; if (validator != null && validator is! JSInvokable) return; - if (image != null && image is! String) return; return _showInputDialog(title, validator, image); case 'showSelectDialog': var title = message['title']; @@ -126,13 +127,25 @@ mixin class JsUiApi { controller?.close(); } - Future _showInputDialog(String title, JSInvokable? validator, String? image) async { + Future _showInputDialog(String title, JSInvokable? validator, dynamic image) async { String? result; var func = validator == null ? null : JSAutoFreeFunction(validator); + String? imageUrl; + Uint8List? imageData; + if (image != null) { + if (image is String) { + imageUrl = image; + } else if (image is Uint8List) { + imageData = image; + } else if (image is List) { + imageData = Uint8List.fromList(image); + } + } await showInputDialog( context: App.rootContext, title: title, - image: image, + image: imageUrl, + imageData: imageData, onConfirm: (v) { if (func != null) { var res = func.call([v]); diff --git a/lib/components/message.dart b/lib/components/message.dart index f1c760e..7fa1313 100644 --- a/lib/components/message.dart +++ b/lib/components/message.dart @@ -360,6 +360,7 @@ Future showInputDialog({ String cancelText = "Cancel", RegExp? inputValidator, String? image, + Uint8List? imageData, }) { var controller = TextEditingController(text: initialValue); bool isLoading = false; @@ -379,6 +380,11 @@ Future showInputDialog({ height: 108, child: Image.network(image, fit: BoxFit.none), ).paddingBottom(8), + if (image == null && imageData != null) + SizedBox( + height: 108, + child: Image.memory(imageData, fit: BoxFit.none), + ).paddingBottom(8), TextField( controller: controller, decoration: InputDecoration(