mind_util.dart 3.5 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/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<void> 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. }
  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<void> 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. }