Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

115 строки
4.5 KiB

  1. // ignore_for_file: non_constant_identifier_names
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:universal_io/io.dart';
  5. import 'package:webview_flutter/webview_flutter.dart' as webview_flutter;
  6. import 'package:webview_flutter_android/webview_flutter_android.dart'
  7. as webview_flutter_android;
  8. import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart'
  9. as webview_flutter_wkwebview;
  10. import "package:webview_universal/webview_desktop/webview_desktop.dart"
  11. as webview_desktop;
  12. class WebViewController {
  13. late final webview_desktop.Webview webview_desktop_controller;
  14. late final webview_flutter.WebViewController webview_mobile_controller;
  15. bool is_init = false;
  16. bool is_desktop =
  17. ((Platform.isLinux || Platform.isMacOS || Platform.isWindows) &&
  18. kIsWeb == false);
  19. bool is_mobile = (Platform.isAndroid || Platform.isIOS || kIsWeb);
  20. WebViewController();
  21. Future<void> init({
  22. required BuildContext context,
  23. required void Function(void Function() fn) setState,
  24. required Uri uri,
  25. }) async {
  26. if (is_mobile) {
  27. late final webview_flutter.PlatformWebViewControllerCreationParams params;
  28. if (webview_flutter.WebViewPlatform.instance
  29. is webview_flutter_wkwebview.WebKitWebViewPlatform) {
  30. params =
  31. webview_flutter_wkwebview.WebKitWebViewControllerCreationParams(
  32. allowsInlineMediaPlayback: true,
  33. mediaTypesRequiringUserAction: const <webview_flutter_wkwebview
  34. .PlaybackMediaTypes>{},
  35. );
  36. } else {
  37. params =
  38. const webview_flutter.PlatformWebViewControllerCreationParams();
  39. }
  40. webview_mobile_controller =
  41. webview_flutter.WebViewController.fromPlatformCreationParams(params);
  42. setState(() {});
  43. if (!kIsWeb) {
  44. webview_mobile_controller
  45. .setJavaScriptMode(webview_flutter.JavaScriptMode.unrestricted);
  46. webview_mobile_controller.setNavigationDelegate(
  47. webview_flutter.NavigationDelegate(
  48. onProgress: (int progress) {
  49. debugPrint('WebView is loading (progress : $progress%)');
  50. },
  51. onPageStarted: (String url) {
  52. debugPrint('Page started loading: $url');
  53. },
  54. onPageFinished: (String url) {
  55. debugPrint('Page finished loading: $url');
  56. },
  57. onWebResourceError: (webview_flutter.WebResourceError error) {
  58. debugPrint('''
  59. Page resource error:
  60. code: ${error.errorCode}
  61. description: ${error.description}
  62. errorType: ${error.errorType}
  63. isForMainFrame: ${error.isForMainFrame}
  64. ''');
  65. },
  66. onNavigationRequest: (webview_flutter.NavigationRequest request) {
  67. if (request.url.startsWith('https://www.youtube.com/')) {
  68. debugPrint('blocking navigation to ${request.url}');
  69. return webview_flutter.NavigationDecision.prevent;
  70. }
  71. debugPrint('allowing navigation to ${request.url}');
  72. return webview_flutter.NavigationDecision.navigate;
  73. },
  74. ),
  75. );
  76. webview_mobile_controller.addJavaScriptChannel(
  77. 'Toaster',
  78. onMessageReceived: (webview_flutter.JavaScriptMessage message) {
  79. ScaffoldMessenger.of(context).showSnackBar(
  80. SnackBar(content: Text(message.message)),
  81. );
  82. },
  83. );
  84. }
  85. webview_mobile_controller.loadRequest(uri);
  86. // #docregion platform_features
  87. if (webview_mobile_controller.platform
  88. is webview_flutter_android.AndroidWebViewController) {
  89. webview_flutter_android.AndroidWebViewController.enableDebugging(false);
  90. (webview_mobile_controller.platform
  91. as webview_flutter_android.AndroidWebViewController)
  92. .setMediaPlaybackRequiresUserGesture(false);
  93. }
  94. is_init = true;
  95. } else if (is_desktop) {
  96. bool isWebviewAvailable =
  97. await webview_desktop.WebviewWindow.isWebviewAvailable();
  98. if (isWebviewAvailable) {
  99. webview_desktop.Webview webview_desktop_controller =
  100. await webview_desktop.WebviewWindow.create(
  101. configuration: webview_desktop.CreateConfiguration(
  102. titleBarTopPadding: Platform.isMacOS ? 20 : 0,
  103. ),
  104. );
  105. setState(() {});
  106. is_init = true;
  107. webview_desktop_controller.setBrightness(Brightness.dark);
  108. webview_desktop_controller.launch(uri.toString());
  109. }
  110. }
  111. }
  112. }