iosWebViewFix/flutter_inappwebview_web/lib/web/headless_inappwebview_manag...

84 lines
2.9 KiB
Dart
Raw Normal View History

import 'dart:html';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
import 'web_platform_manager.dart';
import 'in_app_web_view_web_element.dart';
import 'headless_in_app_web_view_web_element.dart';
class HeadlessInAppWebViewManager {
static final Map<String, HeadlessInAppWebViewWebElement?> webViews = {};
static late MethodChannel _sharedChannel;
late BinaryMessenger _messenger;
HeadlessInAppWebViewManager({required BinaryMessenger messenger}) {
this._messenger = messenger;
HeadlessInAppWebViewManager._sharedChannel = MethodChannel(
'com.pichillilorenzo/flutter_headless_inappwebview',
const StandardMethodCodec(),
_messenger,
);
2022-10-11 14:19:36 +00:00
HeadlessInAppWebViewManager._sharedChannel
.setMethodCallHandler((call) async {
try {
return await handleMethod(call);
} on Error catch (e) {
print(e);
print(e.stackTrace);
}
});
}
Future<dynamic> handleMethod(MethodCall call) async {
switch (call.method) {
case "run":
String id = call.arguments["id"];
2022-04-25 21:21:26 +00:00
Map<String, dynamic> params =
call.arguments["params"].cast<String, dynamic>();
run(id, params);
break;
default:
throw UnimplementedError("Unimplemented ${call.method} method");
}
return null;
}
void run(String id, Map<String, dynamic> params) {
var webView = InAppWebViewWebElement(viewId: id, messenger: _messenger);
var headlessWebView = HeadlessInAppWebViewWebElement(
2022-04-25 21:21:26 +00:00
id: id, messenger: _messenger, webView: webView);
WebPlatformManager.webViews.putIfAbsent(id, () => webView);
HeadlessInAppWebViewManager.webViews.putIfAbsent(id, () => headlessWebView);
2022-04-30 19:22:31 +00:00
prepare(webView, params);
headlessWebView.onWebViewCreated();
webView.makeInitialLoad();
}
void prepare(InAppWebViewWebElement webView, Map<String, dynamic> params) {
webView.iframeContainer.style.display = 'none';
2022-04-30 19:22:31 +00:00
Map<String, num>? initialSize = params["initialSize"]?.cast<String, num>();
if (initialSize != null) {
2022-10-20 14:50:11 +00:00
webView.iframeContainer.style.width =
initialSize["width"].toString() + 'px';
webView.iframeContainer.style.height =
initialSize["height"].toString() + 'px';
2022-04-30 19:22:31 +00:00
}
2022-04-25 21:21:26 +00:00
Map<String, dynamic> initialSettings =
2022-10-11 14:19:36 +00:00
params["initialSettings"].cast<String, dynamic>();
if (initialSettings.isEmpty) {
webView.initialSettings = InAppWebViewSettings();
} else {
webView.initialSettings = InAppWebViewSettings.fromMap(initialSettings);
}
2022-04-25 21:21:26 +00:00
webView.initialUrlRequest = URLRequest.fromMap(
params["initialUrlRequest"]?.cast<String, dynamic>());
webView.initialFile = params["initialFile"];
2022-04-25 21:21:26 +00:00
webView.initialData = InAppWebViewInitialData.fromMap(
params["initialData"]?.cast<String, dynamic>());
document.body?.append(webView.iframeContainer);
webView.prepare();
}
2022-04-25 21:21:26 +00:00
}