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')
|
2022-04-23 20:10:02 +00:00
|
|
|
external set _nativeCommunication(Future<dynamic> Function(String method, int viewId, [List? args]) f);
|
2022-04-22 00:24:50 +00:00
|
|
|
|
|
|
|
/// Allows calling the assigned function from Dart as well.
|
|
|
|
@JS()
|
2022-04-23 20:10:02 +00:00
|
|
|
external Future<dynamic> nativeCommunication(String method, int viewId, [List? args]);
|
2022-04-22 00:24:50 +00:00
|
|
|
|
2022-04-23 20:10:02 +00:00
|
|
|
Future<dynamic> _dartNativeCommunication(String method, int viewId, [List? args]) async {
|
2022-04-22 00:24:50 +00:00
|
|
|
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-23 20:10:02 +00:00
|
|
|
var url = args![0] as String;
|
2022-04-22 11:39:21 +00:00
|
|
|
webViewHtmlElement.onLoadStart(url);
|
|
|
|
break;
|
|
|
|
case 'onLoadStop':
|
2022-04-23 20:10:02 +00:00
|
|
|
var url = args![0] as String;
|
2022-04-22 11:39:21 +00:00
|
|
|
webViewHtmlElement.onLoadStop(url);
|
|
|
|
break;
|
|
|
|
case 'onUpdateVisitedHistory':
|
2022-04-23 20:10:02 +00:00
|
|
|
var url = args![0] as String;
|
2022-04-22 11:39:21 +00:00
|
|
|
webViewHtmlElement.onUpdateVisitedHistory(url);
|
2022-04-22 00:24:50 +00:00
|
|
|
break;
|
2022-04-22 12:41:05 +00:00
|
|
|
case 'onScrollChanged':
|
2022-04-23 20:10:02 +00:00
|
|
|
var x = (args![0] as double).toInt();
|
|
|
|
var y = (args[1] as double).toInt();
|
2022-04-22 12:41:05 +00:00
|
|
|
webViewHtmlElement.onScrollChanged(x, y);
|
|
|
|
break;
|
|
|
|
case 'onConsoleMessage':
|
2022-04-23 20:10:02 +00:00
|
|
|
var type = args![0] as String;
|
|
|
|
var message = args[1] as String?;
|
2022-04-22 12:41:05 +00:00
|
|
|
webViewHtmlElement.onConsoleMessage(type, message);
|
|
|
|
break;
|
2022-04-23 20:10:02 +00:00
|
|
|
case 'onCreateWindow':
|
|
|
|
var windowId = args![0] as int;
|
|
|
|
var url = args[1] as String? ?? 'about:blank';
|
|
|
|
var target = args[2] as String?;
|
|
|
|
var windowFeatures = args[3] as String?;
|
|
|
|
return await webViewHtmlElement.onCreateWindow(windowId, url, target, windowFeatures);
|
|
|
|
case 'onWindowFocus':
|
|
|
|
webViewHtmlElement.onWindowFocus();
|
|
|
|
break;
|
|
|
|
case 'onWindowBlur':
|
|
|
|
webViewHtmlElement.onWindowBlur();
|
|
|
|
break;
|
|
|
|
case 'onPrint':
|
|
|
|
var url = args![0] as String?;
|
|
|
|
webViewHtmlElement.onPrint(url);
|
|
|
|
break;
|
|
|
|
case 'onEnterFullscreen':
|
|
|
|
webViewHtmlElement.onEnterFullscreen();
|
|
|
|
break;
|
|
|
|
case 'onExitFullscreen':
|
|
|
|
webViewHtmlElement.onExitFullscreen();
|
|
|
|
break;
|
|
|
|
case 'onTitleChanged':
|
|
|
|
var title = args![0] as String?;
|
|
|
|
webViewHtmlElement.onTitleChanged(title);
|
|
|
|
break;
|
|
|
|
case 'onZoomScaleChanged':
|
|
|
|
var oldScale = args![0] as double;
|
|
|
|
var newScale = args[1] as double;
|
|
|
|
webViewHtmlElement.onZoomScaleChanged(oldScale, newScale);
|
|
|
|
break;
|
2022-04-22 00:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 21:14:51 +00:00
|
|
|
}
|