controller.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:typed_data';
  5. import 'dart:ui';
  6. import 'package:flutter/material.dart';
  7. import 'package:path_provider/path_provider.dart';
  8. import 'package:electronic_assistant/base/base_controller.dart';
  9. import 'package:electronic_assistant/module/talk/mindmap/mind_util.dart';
  10. import 'package:electronic_assistant/resource/assets.gen.dart';
  11. import 'package:flutter/rendering.dart';
  12. import 'package:flutter/widgets.dart';
  13. import 'package:get/get.dart';
  14. import 'package:get/get_core/src/get_main.dart';
  15. import 'package:webview_flutter/webview_flutter.dart';
  16. import '../../../data/bean/template_bean.dart';
  17. import '../controller.dart';
  18. class MindMapController extends BaseController {
  19. final String? talkId;
  20. MindMapController(this.talkId);
  21. TalkController get talkController => Get.find<TalkController>(tag: talkId);
  22. List<TemplateBean>? get templateList => talkController.templateList.value;
  23. int? get templateSelectId => talkController.templateSelectId.value;
  24. RxBool get isShowMindFullScreen => talkController.isShowMindFullScreen;
  25. StreamSubscription? _talkSummaryListener;
  26. GlobalKey repaintKey = GlobalKey();
  27. @override
  28. void onReady() async {
  29. super.onReady();
  30. // await talkController.webViewController
  31. // .loadRequest(Uri.parse('http://192.168.10.144:9528/'));
  32. // await talkController.webViewController.loadFlutterAsset(Assets.images.test);
  33. // talkController.webViewController.setBackgroundColor(Colors.blue);
  34. talkController.webViewController.loadFlutterAsset(Assets.html.index);
  35. talkController.webViewController.setNavigationDelegate(
  36. NavigationDelegate(
  37. onPageFinished: (String url) async {
  38. _dealTalkSummaryUpdate(talkController.talkBean.value?.summary.value);
  39. },
  40. onNavigationRequest: (NavigationRequest request) {
  41. return NavigationDecision.navigate;
  42. },
  43. ),
  44. );
  45. _talkSummaryListener =
  46. talkController.talkBean.value?.summary.listen((summary) {
  47. _dealTalkSummaryUpdate(summary);
  48. });
  49. }
  50. // // Capture WebView content as a PNG image
  51. // Future<void> _captureAndSaveScreenshot() async {
  52. // try {
  53. // // Render the content inside RepaintBoundary to a picture
  54. // RenderRepaintBoundary boundary = repaintKey.currentContext!
  55. // .findRenderObject() as RenderRepaintBoundary;
  56. // var image = await boundary.toImage(
  57. // pixelRatio: 3); // Higher pixel ratio for better quality
  58. //
  59. // // Convert the image to byte data
  60. // ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
  61. // Uint8List uint8List = byteData!.buffer.asUint8List();
  62. //
  63. // // Get the path to save the image
  64. // final directory = await getApplicationDocumentsDirectory();
  65. // final path = '${directory.path}/webview_screenshot.png';
  66. //
  67. // // Save the file
  68. // final file = File(path);
  69. // await file.writeAsBytes(uint8List);
  70. //
  71. // print('Screenshot saved at $path');
  72. // } catch (e) {
  73. // print("Error taking screenshot: $e");
  74. // }
  75. // }
  76. void _dealTalkSummaryUpdate(String? summary) {
  77. debugPrint('mindMap summary: $summary');
  78. talkController.webViewController
  79. .callHandler(MindUtil.functionToJsUpdateMind, args: [summary],
  80. handler: (value) {
  81. debugPrint('mindMap summary: $value');
  82. });
  83. }
  84. void addTemplateClick() {}
  85. void fullScreenClick() {
  86. talkController.isShowMindFullScreen.value = true;
  87. }
  88. void exitFullScreenClick() {
  89. talkController.onExitMindFullScreen();
  90. }
  91. @override
  92. void onClose() {
  93. super.onClose();
  94. _talkSummaryListener?.cancel();
  95. }
  96. }