mind_util.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:typed_data';
  4. import 'dart:ui';
  5. import 'package:dsbridge_flutter/dsbridge_flutter.dart';
  6. import 'package:electronic_assistant/resource/colors.gen.dart';
  7. import 'package:flutter_svg/svg.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. import 'package:webview_flutter/webview_flutter.dart';
  10. import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
  11. import 'package:xml/xml.dart' as xml;
  12. class MindUtil {
  13. MindUtil._();
  14. static const String functionToJsUpdateMind = "updateValue";
  15. static const String functionToJsExport = "export";
  16. static DWebViewController createMindWebViewController() {
  17. late final PlatformWebViewControllerCreationParams params;
  18. if (WebViewPlatform.instance is WebKitWebViewPlatform) {
  19. params = WebKitWebViewControllerCreationParams(
  20. allowsInlineMediaPlayback: true,
  21. mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
  22. );
  23. } else {
  24. params = const PlatformWebViewControllerCreationParams();
  25. }
  26. DWebViewController webViewController =
  27. DWebViewController.fromPlatformCreationParams(params);
  28. webViewController.setJavaScriptMode(JavaScriptMode.unrestricted);
  29. webViewController.setBackgroundColor(ColorName.white);
  30. webViewController.enableZoom(false);
  31. return webViewController;
  32. }
  33. static Future<File> convertToFile(Uint8List bytes, String fileName) async {
  34. final directory = await getTemporaryDirectory();
  35. final file = File('${directory.path}/mind/$fileName');
  36. if (!file.existsSync()) {
  37. file.createSync(recursive: true);
  38. }
  39. await file.writeAsBytes(bytes);
  40. return file;
  41. }
  42. static Future<void> convertMapDataToToFile(
  43. dynamic data, String fileName) async {
  44. Uint8List? bytes = convertToUint8List(data);
  45. if (bytes == null) {
  46. return;
  47. }
  48. final directory = await getTemporaryDirectory();
  49. final file = File('${directory.path}/$fileName');
  50. await file.writeAsBytes(bytes);
  51. }
  52. static Uint8List? convertToUint8List(dynamic value) {
  53. if (value is Map) {
  54. final data = value.values.toList().cast<int>();
  55. return Uint8List.fromList(data);
  56. }
  57. if (value is String) {
  58. return base64Decode(value.split(',').last);
  59. }
  60. return null;
  61. }
  62. static Future<Uint8List?> svgToPng(String svgString) async {
  63. // first parse svgString to get width and height using xml plugin
  64. final document = xml.XmlDocument.parse(svgString);
  65. final svgElement = document.rootElement;
  66. final widthAttribute =
  67. svgElement.getAttribute('width')!.replaceAll("px", "");
  68. final heightAttribute =
  69. svgElement.getAttribute('height')!.replaceAll("px", "");
  70. final svgDrawableRoot =
  71. await vg.loadPicture(SvgStringLoader(svgString), null);
  72. // same thing
  73. // Convert to ui.Image. toImage() takes width and height as parameters
  74. // you need to find the best size to suit your needs and take into account the screen DPI
  75. final image = await svgDrawableRoot.picture.toImage(
  76. int.parse(widthAttribute.toString()),
  77. int.parse(heightAttribute.toString()));
  78. ByteData? bytes = await image.toByteData(format: ImageByteFormat.png);
  79. return bytes?.buffer.asUint8List();
  80. }
  81. static Future<File> saveToFile(dynamic value, String fileName) async {
  82. Uint8List? bytes = convertToUint8List(value);
  83. if (bytes == null) {
  84. throw Exception('bytes is null');
  85. }
  86. return convertToFile(bytes, fileName);
  87. }
  88. }