mind_util.dart 3.5 KB

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