iosWebViewFix/lib/src/web/web_platform.dart

70 lines
2.4 KiB
Dart
Raw Normal View History

2022-04-21 21:14:51 +00:00
import 'dart:async';
import 'package:flutter/services.dart';
import 'web_platform_manager.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'shims/dart_ui.dart' as ui;
import 'in_app_web_view_web_element.dart';
2022-04-22 00:24:50 +00:00
import 'package:js/js.dart';
2022-04-21 21:14:51 +00:00
/// Builds an iframe based WebView.
///
/// This is used as the default implementation for [WebView] on web.
class FlutterInAppWebViewWebPlatform {
/// Constructs a new instance of [FlutterInAppWebViewWebPlatform].
FlutterInAppWebViewWebPlatform(Registrar registrar) {
ui.platformViewRegistry.registerViewFactory(
'com.pichillilorenzo/flutter_inappwebview',
(int viewId) {
var webView = InAppWebViewWebElement(viewId: viewId, messenger: registrar);
WebPlatformManager.webViews.putIfAbsent(viewId, () => webView);
return webView.iframe;
});
}
static void registerWith(Registrar registrar) {
final pluginInstance = FlutterInAppWebViewWebPlatform(registrar);
2022-04-22 00:24:50 +00:00
_nativeCommunication = allowInterop(_dartNativeCommunication);
2022-04-21 21:14:51 +00:00
}
/// Handles method calls over the MethodChannel of this plugin.
Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
default:
throw PlatformException(
code: 'Unimplemented',
details: 'flutter_inappwebview for web doesn\'t implement \'${call.method}\'',
);
}
}
2022-04-22 00:24:50 +00:00
}
/// Allows assigning a function to be callable from `window.flutter_inappwebview.nativeCommunication()`
@JS('flutter_inappwebview.nativeCommunication')
external set _nativeCommunication(void Function(String method, int viewId, [List? args]) f);
/// Allows calling the assigned function from Dart as well.
@JS()
external void nativeCommunication();
void _dartNativeCommunication(String method, int viewId, [List? args]) {
if (WebPlatformManager.webViews.containsKey(viewId)) {
var webViewHtmlElement = WebPlatformManager.webViews[viewId] as InAppWebViewWebElement;
switch (method) {
2022-04-22 11:39:21 +00:00
case 'onLoadStart':
2022-04-22 00:24:50 +00:00
String url = args![0] as String;
2022-04-22 11:39:21 +00:00
webViewHtmlElement.onLoadStart(url);
break;
case 'onLoadStop':
String url = args![0] as String;
webViewHtmlElement.onLoadStop(url);
break;
case 'onUpdateVisitedHistory':
String url = args![0] as String;
webViewHtmlElement.onUpdateVisitedHistory(url);
2022-04-22 00:24:50 +00:00
break;
}
}
2022-04-21 21:14:51 +00:00
}