import 'dart:io'; import 'dart:typed_data'; import 'dart:ui'; import 'package:dsbridge_flutter/dsbridge_flutter.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_svg/svg.dart'; import 'package:path_provider/path_provider.dart'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart'; import 'package:xml/xml.dart' as xml; class MindUtil { MindUtil._(); static const String functionToJsUpdateMind = "updateValue"; static const String functionToJsExport = "export"; static DWebViewController createMindWebViewController() { late final PlatformWebViewControllerCreationParams params; if (WebViewPlatform.instance is WebKitWebViewPlatform) { params = WebKitWebViewControllerCreationParams( allowsInlineMediaPlayback: true, mediaTypesRequiringUserAction: const {}, ); } else { params = const PlatformWebViewControllerCreationParams(); } DWebViewController webViewController = DWebViewController.fromPlatformCreationParams(params); webViewController.setJavaScriptMode(JavaScriptMode.unrestricted); webViewController.enableZoom(false); return webViewController; } static Future convertToFile(Uint8List bytes, String fileName) async { final directory = await getTemporaryDirectory(); final file = File('${directory.path}/$fileName'); await file.writeAsBytes(bytes); } static Future convertMapDataToToFile( dynamic data, String fileName) async { Uint8List? bytes = convertMapToUint8List(data); if (bytes == null) { return; } final directory = await getTemporaryDirectory(); final file = File('${directory.path}/$fileName'); await file.writeAsBytes(bytes); } static Uint8List? convertMapToUint8List(dynamic map) { if (map is Map) { final value = map.values.toList().cast(); return Uint8List.fromList(value); } return null; } static Future svgToPng( String svgString, BuildContext context) async { final pictureInfo = await vg.loadPicture(SvgStringLoader(svgString), context); final image = await pictureInfo.picture.toImage(100, 100); final byteData = await image.toByteData(format: ImageByteFormat.png); if (byteData == null) { throw Exception('Unable to convert SVG to PNG'); } final pngBytes = byteData.buffer.asUint8List(); return pngBytes; } static Future svgToPng2(String svgString) async { // first parse svgString to get width and height using xml plugin final document = xml.XmlDocument.parse(svgString); final svgElement = document.rootElement; final widthAttribute = svgElement.getAttribute('width')!.replaceAll("px", ""); final heightAttribute = svgElement.getAttribute('height')!.replaceAll("px", ""); final svgDrawableRoot = await vg.loadPicture(SvgStringLoader(svgString), null); // same thing // Convert to ui.Image. toImage() takes width and height as parameters // you need to find the best size to suit your needs and take into account the screen DPI final image = await svgDrawableRoot.picture.toImage( int.parse(widthAttribute.toString()), int.parse(heightAttribute.toString())); ByteData? bytes = await image.toByteData(format: ImageByteFormat.png); return bytes?.buffer.asUint8List(); } }