added main stub for web support, updated example, updated debug log

This commit is contained in:
Lorenzo Pichilli 2022-04-26 15:45:36 +02:00
parent df6fe3f8ce
commit 85949e9858
9 changed files with 60 additions and 48 deletions

View File

@ -34,19 +34,16 @@ class _HeadlessInAppWebViewExampleScreenState
print("CONSOLE MESSAGE: " + consoleMessage.message); print("CONSOLE MESSAGE: " + consoleMessage.message);
}, },
onLoadStart: (controller, url) async { onLoadStart: (controller, url) async {
print("onLoadStart $url");
setState(() { setState(() {
this.url = url.toString(); this.url = url.toString();
}); });
}, },
onLoadStop: (controller, url) async { onLoadStop: (controller, url) async {
print("onLoadStop $url");
setState(() { setState(() {
this.url = url.toString(); this.url = url.toString();
}); });
}, },
onUpdateVisitedHistory: (controller, url, androidIsReload) { onUpdateVisitedHistory: (controller, url, androidIsReload) {
print("onUpdateVisitedHistory $url");
setState(() { setState(() {
this.url = url.toString(); this.url = url.toString();
}); });
@ -106,6 +103,9 @@ class _HeadlessInAppWebViewExampleScreenState
child: ElevatedButton( child: ElevatedButton(
onPressed: () { onPressed: () {
headlessWebView?.dispose(); headlessWebView?.dispose();
setState(() {
this.url = "";
});
}, },
child: Text("Dispose HeadlessInAppWebView")), child: Text("Dispose HeadlessInAppWebView")),
) )

View File

@ -19,19 +19,17 @@ class MyInAppBrowser extends InAppBrowser {
@override @override
Future onLoadStart(url) async { Future onLoadStart(url) async {
print("\n\nStarted $url\n\n");
} }
@override @override
Future onLoadStop(url) async { Future onLoadStop(url) async {
pullToRefreshController?.endRefreshing(); pullToRefreshController?.endRefreshing();
print("\n\nStopped $url\n\n");
} }
@override @override
void onLoadError(url, code, message) { void onLoadError(url, code, message) {
pullToRefreshController?.endRefreshing(); pullToRefreshController?.endRefreshing();
print("Can't load $url.. Error: $message");
} }
@override @override
@ -39,7 +37,6 @@ class MyInAppBrowser extends InAppBrowser {
if (progress == 100) { if (progress == 100) {
pullToRefreshController?.endRefreshing(); pullToRefreshController?.endRefreshing();
} }
print("Progress: $progress");
} }
@override @override
@ -53,25 +50,6 @@ class MyInAppBrowser extends InAppBrowser {
print("\n\nOverride ${navigationAction.request.url}\n\n"); print("\n\nOverride ${navigationAction.request.url}\n\n");
return NavigationActionPolicy.ALLOW; return NavigationActionPolicy.ALLOW;
} }
@override
void onLoadResource(response) {
print("Started at: " +
response.startTime.toString() +
"ms ---> duration: " +
response.duration.toString() +
"ms " +
(response.url ?? '').toString());
}
@override
void onConsoleMessage(consoleMessage) {
print("""
console output:
message: ${consoleMessage.message}
messageLevel: ${consoleMessage.messageLevel.toValue()}
""");
}
} }
class InAppBrowserExampleScreen extends StatefulWidget { class InAppBrowserExampleScreen extends StatefulWidget {

View File

@ -19,10 +19,7 @@
* *
*/ */
@JS()
library flutter_inappwebview; library flutter_inappwebview;
import 'package:js/js.dart';
export 'src/main.dart'; export 'src/main.dart';
export 'src/web/main.dart'; export 'src/web/main_stub.dart' if (dart.library.html) 'src/web/main.dart';

View File

@ -1,9 +1,11 @@
import 'dart:async'; import 'dart:async';
import 'dart:collection'; import 'dart:collection';
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:developer' as developer;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_inappwebview/src/util.dart'; import '../util.dart';
import 'chrome_safari_browser_settings.dart'; import 'chrome_safari_browser_settings.dart';
@ -42,6 +44,13 @@ class ChromeSafariBrowserNotOpenedException implements Exception {
///- Android native WebView ///- Android native WebView
///- iOS ///- iOS
class ChromeSafariBrowser { class ChromeSafariBrowser {
///Enables [ChromeSafariBrowser] debug logging info.
///
///The default value is the same value of [kDebugMode],
///so it is enabled by default when the application is compiled in debug mode
///and disabled when it is not.
static bool debugLogging = kDebugMode;
///View ID used internally. ///View ID used internally.
late final String id; late final String id;
@ -60,7 +69,19 @@ class ChromeSafariBrowser {
_isOpened = false; _isOpened = false;
} }
_debugLog(String method, dynamic args) {
if (ChromeSafariBrowser.debugLogging) {
String message =
"ChromeSafariBrowser ID " + id + " calling \"" +
method.toString() + "\" using " + args.toString();
developer.log(message,
name: this.runtimeType.toString());
}
}
Future<dynamic> handleMethod(MethodCall call) async { Future<dynamic> handleMethod(MethodCall call) async {
_debugLog(call.method, call.arguments);
switch (call.method) { switch (call.method) {
case "onChromeSafariBrowserOpened": case "onChromeSafariBrowserOpened":
onOpened(); onOpened();

View File

@ -663,7 +663,7 @@ class _InAppWebViewState extends State<InAppWebView> {
@override @override
void dispose() { void dispose() {
int viewId = _controller.getViewId(); dynamic viewId = _controller.getViewId();
if (kIsWeb && WebPlatformManager.webViews.containsKey(viewId)) { if (kIsWeb && WebPlatformManager.webViews.containsKey(viewId)) {
WebPlatformManager.webViews.remove(viewId); WebPlatformManager.webViews.remove(viewId);
} }

View File

@ -105,14 +105,22 @@ class InAppWebViewController
localStorage: LocalStorage(this), sessionStorage: SessionStorage(this)); localStorage: LocalStorage(this), sessionStorage: SessionStorage(this));
} }
Future<dynamic> handleMethod(MethodCall call) async { _debugLog(String method, dynamic args) {
if (WebView.debugLogging) {
if (WebView.debugLogging && (call.method.startsWith("on") || call.method.startsWith("should")) && String viewId = (getViewId() ?? _inAppBrowser?.id).toString();
call.method != "onCallJsHandler") { String message =
developer.log( (_inAppBrowser == null ? "WebView ID " : "InAppBrowser ID " + viewId) +
call.method.toString() + ": using " + call.arguments.toString(), " calling \"" +
method.toString() + "\" using " + args.toString();
developer.log(message,
name: this.runtimeType.toString()); name: this.runtimeType.toString());
} }
}
Future<dynamic> handleMethod(MethodCall call) async {
if (WebView.debugLogging && call.method != "onCallJsHandler") {
_debugLog(call.method, call.arguments);
}
switch (call.method) { switch (call.method) {
case "onLoadStart": case "onLoadStart":
@ -1032,11 +1040,7 @@ class InAppWebViewController
// decode args to json // decode args to json
List<dynamic> args = jsonDecode(call.arguments["args"]); List<dynamic> args = jsonDecode(call.arguments["args"]);
if (WebView.debugLogging && (handlerName.startsWith("on") || handlerName.startsWith("should"))) { _debugLog(handlerName, args);
developer.log(
handlerName.toString() + ": using " + args.toString(),
name: this.runtimeType.toString());
}
switch (handlerName) { switch (handlerName) {
case "onLoadResource": case "onLoadResource":
@ -3038,7 +3042,7 @@ class InAppWebViewController
} }
///Used internally. ///Used internally.
int getViewId() { dynamic getViewId() {
return _id; return _id;
} }
} }

View File

@ -1,6 +1,9 @@
import 'dart:collection'; import 'dart:collection';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter_inappwebview/flutter_inappwebview_web.dart';
import '../pull_to_refresh/pull_to_refresh_controller.dart'; import '../pull_to_refresh/pull_to_refresh_controller.dart';
import '../context_menu.dart'; import '../context_menu.dart';
@ -10,10 +13,14 @@ import 'in_app_webview_controller.dart';
import 'in_app_webview_settings.dart'; import 'in_app_webview_settings.dart';
import 'headless_in_app_webview.dart'; import 'headless_in_app_webview.dart';
///Abstract class that represents a WebView. Used by [InAppWebView] and [HeadlessInAppWebView]. ///Abstract class that represents a WebView. Used by [InAppWebView], [HeadlessInAppWebView] and the WebView of [InAppBrowser].
abstract class WebView { abstract class WebView {
///Enables WebView debug logging info. Logging is on by default. ///Enables [WebView] debug logging info.
static bool debugLogging = true; ///
///The default value is the same value of [kDebugMode],
///so it is enabled by default when the application is compiled in debug mode
///and disabled when it is not.
static bool debugLogging = kDebugMode;
///The window id of a [CreateWindowAction.windowId]. ///The window id of a [CreateWindowAction.windowId].
final int? windowId; final int? windowId;

View File

@ -0,0 +1,6 @@
///Stub for web support.
class FlutterInAppWebViewWebPlatform {
static void registerWith(dynamic registrar) {
}
}

View File

@ -35,7 +35,6 @@ flutter:
pluginClass: FlutterInAppWebViewWebPlatform pluginClass: FlutterInAppWebViewWebPlatform
fileName: flutter_inappwebview_web.dart fileName: flutter_inappwebview_web.dart
assets: assets:
- packages/flutter_inappwebview/assets/t_rex_runner/t-rex.html - packages/flutter_inappwebview/assets/t_rex_runner/t-rex.html
- packages/flutter_inappwebview/assets/t_rex_runner/t-rex.css - packages/flutter_inappwebview/assets/t_rex_runner/t-rex.css