浏览代码

优化资源文件加载,基本稳定可用

gavin.chen 2 年之前
父节点
当前提交
8026c54704
共有 49 个文件被更改,包括 143 次插入175 次删除
  1. 134 172
      lib/pages/webview_page/view.dart
  2. 9 3
      pubspec.yaml
  3. 0 0
      threejsview/MainPage.html
  4. 0 0
      threejsview/js/build/THREE.MeshLine.js
  5. 0 0
      threejsview/js/build/three.js
  6. 0 0
      threejsview/js/controls/OrbitControls.js
  7. 0 0
      threejsview/js/controls/TrackballControls.js
  8. 0 0
      threejsview/js/customScript/CefInterface.js
  9. 0 0
      threejsview/js/customScript/EventCenter.js
  10. 0 0
      threejsview/js/customScript/InitPage.js
  11. 0 0
      threejsview/js/customScript/PointsConverter.js
  12. 0 0
      threejsview/js/customScript/Vector3Extention.js
  13. 0 0
      threejsview/js/customScript/clipWorkflow/ClipDataDefine.js
  14. 0 0
      threejsview/js/customScript/clipWorkflow/ClipMeshUpgrader.js
  15. 0 0
      threejsview/js/customScript/clipWorkflow/ClipMouseEvent.js
  16. 0 0
      threejsview/js/customScript/clipWorkflow/ClipPlaneMover.js
  17. 0 0
      threejsview/js/customScript/clipWorkflow/ClipPlaneRotator.js
  18. 0 0
      threejsview/js/customScript/manager/ClipPlaneManager.js
  19. 0 0
      threejsview/js/customScript/manager/CubeEdgesManager.js
  20. 0 0
      threejsview/js/customScript/manager/LinesDrawingManager.js
  21. 0 0
      threejsview/js/customScript/manager/SpriteManager.js
  22. 0 0
      threejsview/js/customScript/utility/CubeFaceNormalHelper.js
  23. 0 0
      threejsview/js/customScript/utility/LineIntersectionHelper.js
  24. 0 0
      threejsview/js/customScript/utility/LoaderUtility.js
  25. 0 0
      threejsview/js/customScript/utility/OSHelper.js
  26. 0 0
      threejsview/js/customScript/utility/PolyPointsTool.js
  27. 0 0
      threejsview/js/gui/dat.gui.min.js
  28. 0 0
      threejsview/js/gui/stats.min.js
  29. 二进制
      web/3DWeb/FaceImage/1.jpg
  30. 二进制
      web/3DWeb/FaceImage/2.jpg
  31. 二进制
      web/3DWeb/FaceImage/3.jpg
  32. 二进制
      web/3DWeb/FaceImage/4.jpg
  33. 二进制
      web/3DWeb/FaceImage/5.jpg
  34. 二进制
      web/3DWeb/FaceImage/6.jpg
  35. 0 0
      web/ButtonImage/AI.png
  36. 0 0
      web/ButtonImage/AIDown.png
  37. 0 0
      web/ButtonImage/clip.png
  38. 0 0
      web/ButtonImage/clipMouseDown.png
  39. 0 0
      web/ButtonImage/delete.png
  40. 0 0
      web/ButtonImage/deleteMouseDown.png
  41. 0 0
      web/ButtonImage/load.gif
  42. 0 0
      web/ButtonImage/move.png
  43. 0 0
      web/ButtonImage/moveMouseDown.png
  44. 0 0
      web/ButtonImage/reset.png
  45. 0 0
      web/ButtonImage/resetMouseDown.png
  46. 0 0
      web/ButtonImage/rotate+.png
  47. 0 0
      web/ButtonImage/rotate+MouseDown.png
  48. 0 0
      web/ButtonImage/rotate-.png
  49. 0 0
      web/ButtonImage/rotate-MouseDown.png

+ 134 - 172
lib/pages/webview_page/view.dart

@@ -16,19 +16,14 @@ class WebViewXPage extends StatefulWidget {
 
 class _WebViewXPageState extends State<WebViewXPage> {
   late WebViewXController webviewController;
-  final executeJsErrorMessage =
-      'Failed to execute this script, please check your code. Continue to use the app without this script.';
-  final javaScriptsFromAssets = [];
-  Size get screenSize => MediaQuery.of(context).size;
+  final executeJsErrorMessage = '脚本执行失败,请检查代码是否正确嵌入';
 
-  Future<String> _loadJSFromAssets(String path) {
-    return rootBundle.loadString(path);
-  }
+  /// 预加载的脚本文件
+  List<String> scriptsFromAssets = [];
+  Size get screenSize => MediaQuery.of(context).size;
 
-  @override
-  void initState() {
-    // TODO: implement initState
-    super.initState();
+  /// 预加载脚本文件
+  Future<String> _preloadJS() async {
     final scriptsList = [
       "threejsview/js/build/three.js",
       "threejsview/js/build/THREE.MeshLine.js",
@@ -56,9 +51,17 @@ class _WebViewXPageState extends State<WebViewXPage> {
       "threejsview/js/customScript/utility/OSHelper.js",
     ];
     for (String path in scriptsList) {
-      _loadJSFromAssets(path).then((value) {
-        javaScriptsFromAssets.add(value);
-      });
+      var value = await _loadJSFromAssets(path);
+      scriptsFromAssets.add(value);
+    }
+    return Future.value("");
+  }
+
+  Future<String> _loadJSFromAssets(String path) async {
+    try {
+      return await rootBundle.loadString(path);
+    } catch (e) {
+      return Future.error(e);
     }
   }
 
@@ -122,149 +125,45 @@ class _WebViewXPageState extends State<WebViewXPage> {
   }
 
   Widget _buildWebViewX() {
-    return WebViewX(
-      key: const ValueKey('webviewx'),
-      initialContent: "<h2> Hello webviewX ! <h2>",
-      initialSourceType: SourceType.html,
-      height: screenSize.height / 1.5,
-      width: min(screenSize.width * 0.8, 1024),
-      onWebViewCreated: (controller) => webviewController = controller,
-      onPageStarted: (src) => debugPrint('A new page has started loading'),
-      onPageFinished: (src) => debugPrint('The page has finished loading'),
-      jsContent:
-          javaScriptsFromAssets.map((e) => EmbeddedJsContent(js: e)).toSet(),
-      dartCallBacks: {
-        DartCallback(
-          name: 'TestDartCallback',
-          callBack: (msg) => showSnackBar(msg.toString(), context),
-        )
-      },
-      webSpecificParams: const WebSpecificParams(
-        printDebugInfo: true,
-      ),
-      mobileSpecificParams: const MobileSpecificParams(
-        androidEnableHybridComposition: true,
-      ),
-      navigationDelegate: (navigation) {
-        debugPrint(navigation.content.sourceType.toString());
-        return NavigationDecision.navigate;
-      },
-    );
-  }
-
-  void _setUrl() {
-    webviewController.loadContent(
-      'https://flutter.dev',
-      SourceType.url,
-    );
-  }
-
-  void _setUrlBypass() {
-    webviewController.loadContent(
-      'https://www.baidu.com/',
-      SourceType.urlBypass,
-    );
-  }
-
-  void _setHtmlFromAssets() {
-    webviewController.loadContent(
-      'threejsview/MainPage.html',
-      SourceType.html,
-      fromAssets: true,
-    );
-  }
-
-  Future<void> _goForward() async {
-    if (await webviewController.canGoForward()) {
-      await webviewController.goForward();
-      showSnackBar('Did go forward', context);
-    } else {
-      showSnackBar('Cannot go forward', context);
-    }
-  }
-
-  Future<void> _goBack() async {
-    if (await webviewController.canGoBack()) {
-      await webviewController.goBack();
-      showSnackBar('Did go back', context);
-    } else {
-      showSnackBar('Cannot go back', context);
-    }
-  }
-
-  void _reload() {
-    webviewController.reload();
-  }
-
-  void _toggleIgnore() {
-    final ignoring = webviewController.ignoresAllGestures;
-    webviewController.setIgnoreAllGestures(!ignoring);
-    showSnackBar('Ignore events = ${!ignoring}', context);
-  }
-
-  Future<void> _evalRawJsInGlobalContext() async {
-    try {
-      final result = await webviewController.evalRawJavascript(
-        '2+2',
-        inGlobalContext: true,
-      );
-      showSnackBar('The result is $result', context);
-    } catch (e) {
-      showAlertDialog(
-        executeJsErrorMessage,
-        context,
-      );
-    }
-  }
-
-  Future<void> _callPlatformIndependentJsMethod() async {
-    try {
-      await webviewController.callJsMethod('testPlatformIndependentMethod', []);
-    } catch (e) {
-      showAlertDialog(
-        executeJsErrorMessage,
-        context,
-      );
-    }
-  }
-
-  Future<void> _callPlatformSpecificJsMethod() async {
-    try {
-      await webviewController
-          .callJsMethod('testPlatformSpecificMethod', ['Hi']);
-    } catch (e) {
-      showAlertDialog(
-        executeJsErrorMessage,
-        context,
-      );
-    }
-  }
-
-  Future<void> _getWebviewContent() async {
-    try {
-      final content = await webviewController.getContent();
-      showAlertDialog(content.source, context);
-    } catch (e) {
-      showAlertDialog('Failed to execute this task.', context);
-    }
-  }
-
-  Widget buildSpace({
-    Axis direction = Axis.horizontal,
-    double amount = 0.2,
-    bool flex = true,
-  }) {
-    return flex
-        ? Flexible(
-            child: FractionallySizedBox(
-              widthFactor: direction == Axis.horizontal ? amount : null,
-              heightFactor: direction == Axis.vertical ? amount : null,
-            ),
-          )
-        : SizedBox(
-            width: direction == Axis.horizontal ? amount : null,
-            height: direction == Axis.vertical ? amount : null,
-          );
+    return FutureBuilder<String>(
+        future: _preloadJS(),
+        builder: (context, AsyncSnapshot<String> snapshot) {
+          if (snapshot.hasData) {
+            return WebViewX(
+              key: const ValueKey('webviewx'),
+              initialContent: "<h2> Hello webviewX ! <h2>",
+              initialSourceType: SourceType.html,
+              height: screenSize.height / 1.5,
+              width: min(screenSize.width * 0.8, 1024),
+              onWebViewCreated: (controller) => webviewController = controller,
+              onPageStarted: (src) =>
+                  debugPrint('A new page has started loading'),
+              onPageFinished: (src) =>
+                  debugPrint('The page has finished loading'),
+              jsContent: scriptsFromAssets
+                  .map((e) => EmbeddedJsContent(js: e))
+                  .toSet(),
+              dartCallBacks: {
+                DartCallback(
+                  name: 'TestDartCallback',
+                  callBack: (msg) => showSnackBar(msg.toString(), context),
+                )
+              },
+              webSpecificParams: const WebSpecificParams(
+                printDebugInfo: true,
+              ),
+              mobileSpecificParams: const MobileSpecificParams(
+                androidEnableHybridComposition: true,
+              ),
+              navigationDelegate: (navigation) {
+                debugPrint(navigation.content.sourceType.toString());
+                return NavigationDecision.navigate;
+              },
+            );
+          } else {
+            return const CircularProgressIndicator();
+          }
+        });
   }
 
   List<Widget> _buildButtons() {
@@ -296,21 +195,6 @@ class _WebViewXPageState extends State<WebViewXPage> {
         onTap: _evalRawJsInGlobalContext,
       ),
       buildSpace(direction: Axis.vertical, flex: false, amount: 20.0),
-      createButton(
-        text: '测试与平台无关的 JS 代码 (console.log)',
-        onTap: _callPlatformIndependentJsMethod,
-      ),
-      buildSpace(direction: Axis.vertical, flex: false, amount: 20.0),
-      createButton(
-        text: '调用区分平台的 JS 方法,结果会返回到 flutter 方法',
-        onTap: _callPlatformSpecificJsMethod,
-      ),
-      buildSpace(direction: Axis.vertical, flex: false, amount: 20.0),
-      createButton(
-        text: '返回当前 webview 内容',
-        onTap: _getWebviewContent,
-      ),
-      buildSpace(direction: Axis.vertical, flex: false, amount: 20.0),
       const Text(
         "👇👇 Threejs 页面相关(建议顺序执行) 👇👇",
         style: TextStyle(fontSize: 20),
@@ -372,4 +256,82 @@ class _WebViewXPageState extends State<WebViewXPage> {
       );
     }
   }
+
+  void _setUrlBypass() {
+    webviewController.loadContent(
+      'https://www.baidu.com/',
+      SourceType.urlBypass,
+    );
+  }
+
+  void _setHtmlFromAssets() {
+    webviewController.loadContent(
+      'threejsview/MainPage.html',
+      SourceType.html,
+      fromAssets: true,
+    );
+  }
+
+  Future<void> _goForward() async {
+    if (await webviewController.canGoForward()) {
+      await webviewController.goForward();
+      showSnackBar('Did go forward', context);
+    } else {
+      showSnackBar('Cannot go forward', context);
+    }
+  }
+
+  Future<void> _goBack() async {
+    if (await webviewController.canGoBack()) {
+      await webviewController.goBack();
+      showSnackBar('Did go back', context);
+    } else {
+      showSnackBar('Cannot go back', context);
+    }
+  }
+
+  void _reload() {
+    webviewController.reload();
+  }
+
+  /// 事件忽略开关
+  void _toggleIgnore() {
+    final ignoring = webviewController.ignoresAllGestures;
+    webviewController.setIgnoreAllGestures(!ignoring);
+    showSnackBar('Ignore events = ${!ignoring}', context);
+  }
+
+  /// 在浏览器环境全局执行
+  Future<void> _evalRawJsInGlobalContext() async {
+    try {
+      final result = await webviewController.evalRawJavascript(
+        '2+2',
+        inGlobalContext: true,
+      );
+      showSnackBar('The result is $result', context);
+    } catch (e) {
+      showAlertDialog(
+        executeJsErrorMessage,
+        context,
+      );
+    }
+  }
+
+  Widget buildSpace({
+    Axis direction = Axis.horizontal,
+    double amount = 0.2,
+    bool flex = true,
+  }) {
+    return flex
+        ? Flexible(
+            child: FractionallySizedBox(
+              widthFactor: direction == Axis.horizontal ? amount : null,
+              heightFactor: direction == Axis.vertical ? amount : null,
+            ),
+          )
+        : SizedBox(
+            width: direction == Axis.horizontal ? amount : null,
+            height: direction == Axis.vertical ? amount : null,
+          );
+  }
 }

+ 9 - 3
pubspec.yaml

@@ -59,9 +59,15 @@ flutter:
   uses-material-design: true
 
   # To add assets to your application, add an assets section, like this:
-  # assets:
-  #   - images/a_dot_burr.jpeg
-  #   - images/a_dot_ham.jpeg
+  assets:
+    - threejsview/
+    - threejsview/js/gui/
+    - threejsview/js/customScript/
+    - threejsview/js/customScript/utility/
+    - threejsview/js/customScript/manager/
+    - threejsview/js/customScript/clipWorkflow/
+    - threejsview/js/controls/
+    - threejsview/js/build/
 
   # An image asset can refer to one or more resolution-specific "variants", see
   # https://flutter.dev/assets-and-images/#resolution-aware.

+ 0 - 0
assets/threejsview/MainPage.html → threejsview/MainPage.html


+ 0 - 0
assets/threejsview/js/build/THREE.MeshLine.js → threejsview/js/build/THREE.MeshLine.js


+ 0 - 0
assets/threejsview/js/build/three.js → threejsview/js/build/three.js


+ 0 - 0
assets/threejsview/js/controls/OrbitControls.js → threejsview/js/controls/OrbitControls.js


+ 0 - 0
assets/threejsview/js/controls/TrackballControls.js → threejsview/js/controls/TrackballControls.js


+ 0 - 0
assets/threejsview/js/customScript/CefInterface.js → threejsview/js/customScript/CefInterface.js


+ 0 - 0
assets/threejsview/js/customScript/EventCenter.js → threejsview/js/customScript/EventCenter.js


+ 0 - 0
assets/threejsview/js/customScript/InitPage.js → threejsview/js/customScript/InitPage.js


+ 0 - 0
assets/threejsview/js/customScript/PointsConverter.js → threejsview/js/customScript/PointsConverter.js


+ 0 - 0
assets/threejsview/js/customScript/Vector3Extention.js → threejsview/js/customScript/Vector3Extention.js


+ 0 - 0
assets/threejsview/js/customScript/clipWorkflow/ClipDataDefine.js → threejsview/js/customScript/clipWorkflow/ClipDataDefine.js


+ 0 - 0
assets/threejsview/js/customScript/clipWorkflow/ClipMeshUpgrader.js → threejsview/js/customScript/clipWorkflow/ClipMeshUpgrader.js


+ 0 - 0
assets/threejsview/js/customScript/clipWorkflow/ClipMouseEvent.js → threejsview/js/customScript/clipWorkflow/ClipMouseEvent.js


+ 0 - 0
assets/threejsview/js/customScript/clipWorkflow/ClipPlaneMover.js → threejsview/js/customScript/clipWorkflow/ClipPlaneMover.js


+ 0 - 0
assets/threejsview/js/customScript/clipWorkflow/ClipPlaneRotator.js → threejsview/js/customScript/clipWorkflow/ClipPlaneRotator.js


+ 0 - 0
assets/threejsview/js/customScript/manager/ClipPlaneManager.js → threejsview/js/customScript/manager/ClipPlaneManager.js


+ 0 - 0
assets/threejsview/js/customScript/manager/CubeEdgesManager.js → threejsview/js/customScript/manager/CubeEdgesManager.js


+ 0 - 0
assets/threejsview/js/customScript/manager/LinesDrawingManager.js → threejsview/js/customScript/manager/LinesDrawingManager.js


+ 0 - 0
assets/threejsview/js/customScript/manager/SpriteManager.js → threejsview/js/customScript/manager/SpriteManager.js


+ 0 - 0
assets/threejsview/js/customScript/utility/CubeFaceNormalHelper.js → threejsview/js/customScript/utility/CubeFaceNormalHelper.js


+ 0 - 0
assets/threejsview/js/customScript/utility/LineIntersectionHelper.js → threejsview/js/customScript/utility/LineIntersectionHelper.js


+ 0 - 0
assets/threejsview/js/customScript/utility/LoaderUtility.js → threejsview/js/customScript/utility/LoaderUtility.js


+ 0 - 0
assets/threejsview/js/customScript/utility/OSHelper.js → threejsview/js/customScript/utility/OSHelper.js


+ 0 - 0
assets/threejsview/js/customScript/utility/PolyPointsTool.js → threejsview/js/customScript/utility/PolyPointsTool.js


+ 0 - 0
assets/threejsview/js/gui/dat.gui.min.js → threejsview/js/gui/dat.gui.min.js


+ 0 - 0
assets/threejsview/js/gui/stats.min.js → threejsview/js/gui/stats.min.js


二进制
web/3DWeb/FaceImage/1.jpg


二进制
web/3DWeb/FaceImage/2.jpg


二进制
web/3DWeb/FaceImage/3.jpg


二进制
web/3DWeb/FaceImage/4.jpg


二进制
web/3DWeb/FaceImage/5.jpg


二进制
web/3DWeb/FaceImage/6.jpg


+ 0 - 0
assets/threejsview/ButtonImage/AI.png → web/ButtonImage/AI.png


+ 0 - 0
assets/threejsview/ButtonImage/AIDown.png → web/ButtonImage/AIDown.png


+ 0 - 0
assets/threejsview/ButtonImage/clip.png → web/ButtonImage/clip.png


+ 0 - 0
assets/threejsview/ButtonImage/clipMouseDown.png → web/ButtonImage/clipMouseDown.png


+ 0 - 0
assets/threejsview/ButtonImage/delete.png → web/ButtonImage/delete.png


+ 0 - 0
assets/threejsview/ButtonImage/deleteMouseDown.png → web/ButtonImage/deleteMouseDown.png


+ 0 - 0
assets/threejsview/ButtonImage/load.gif → web/ButtonImage/load.gif


+ 0 - 0
assets/threejsview/ButtonImage/move.png → web/ButtonImage/move.png


+ 0 - 0
assets/threejsview/ButtonImage/moveMouseDown.png → web/ButtonImage/moveMouseDown.png


+ 0 - 0
assets/threejsview/ButtonImage/reset.png → web/ButtonImage/reset.png


+ 0 - 0
assets/threejsview/ButtonImage/resetMouseDown.png → web/ButtonImage/resetMouseDown.png


+ 0 - 0
assets/threejsview/ButtonImage/rotate+.png → web/ButtonImage/rotate+.png


+ 0 - 0
assets/threejsview/ButtonImage/rotate+MouseDown.png → web/ButtonImage/rotate+MouseDown.png


+ 0 - 0
assets/threejsview/ButtonImage/rotate-.png → web/ButtonImage/rotate-.png


+ 0 - 0
assets/threejsview/ButtonImage/rotate-MouseDown.png → web/ButtonImage/rotate-MouseDown.png