TF-105 Handle event click link and loading content of HtmlContentViewer

This commit is contained in:
dab246
2021-10-08 11:18:38 +07:00
committed by Dat H. Pham
parent 8fb57725df
commit 15e1417730
5 changed files with 70 additions and 6 deletions
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:url_launcher/url_launcher.dart' as launcher;
class _HtmlGenerationArguments {
final String message;
@@ -35,12 +36,22 @@ class HtmlContentViewer extends StatefulWidget {
/// Register this callback if you want a reference to the [InAppWebViewController].
final void Function(InAppWebViewController controller)? onWebViewCreated;
final void Function(InAppWebViewController controller, Uri? uri)? onWebViewLoadStart;
final void Function(InAppWebViewController controller, Uri? uri)? onWebViewLoadStop;
/// Handler for mailto: links
final Future Function(Uri mailto)? mailtoDelegate;
const HtmlContentViewer({
Key? key,
required this.message,
this.blockExternalImages = false,
this.onError,
this.onWebViewCreated,
this.onWebViewLoadStart,
this.onWebViewLoadStop,
this.mailtoDelegate,
}) : super(key: key);
@override
@@ -144,6 +155,11 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
enableViewportScale: true,
)
),
onLoadStart: (controller, uri) {
if (widget.onWebViewLoadStart != null) {
widget.onWebViewLoadStart!(controller, uri);
}
},
onWebViewCreated: widget.onWebViewCreated,
onLoadStop: (controller, uri) async {
var scrollHeight = (await controller.evaluateJavascript(source: 'document.body.scrollHeight'));
@@ -162,10 +178,34 @@ class _HtmlContentViewState extends State<HtmlContentViewer> {
_webViewContentHeight = double.tryParse('${scrollHeight + 10.0}');
});
}
if (widget.onWebViewLoadStop != null) {
widget.onWebViewLoadStop!(controller, uri);
}
},
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
gestureRecognizers: {
Factory<LongPressGestureRecognizer>(() => LongPressGestureRecognizer()),
},
);
}
Future<NavigationActionPolicy> _shouldOverrideUrlLoading(
InAppWebViewController controller,
NavigationAction request
) async {
final requestUri = request.request.url!;
final mailtoHandler = widget.mailtoDelegate;
if (mailtoHandler != null && requestUri.isScheme('mailto')) {
await mailtoHandler(requestUri);
return NavigationActionPolicy.CANCEL;
}
final url = requestUri.toString();
if (await launcher.canLaunch(url)) {
await launcher.launch(url);
return NavigationActionPolicy.CANCEL;
} else {
return NavigationActionPolicy.ALLOW;
}
}
}
+8 -4
View File
@@ -1,6 +1,8 @@
name: core
description: Core Module
# As we are not publishing this Installer in pub.dev, we can add publish_to: 'none'
publish_to: none
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
@@ -59,11 +61,13 @@ dependencies:
# getwidget
getwidget: 2.0.4
# enough_html_editor
enough_html_editor:
# flutter_inappwebview
flutter_inappwebview:
git:
url: git://github.com/dab246/enough_html_editor.git
ref: fix_bug_enough_platform_widgets
url: https://github.com/robert-virkus/flutter_inappwebview.git
# url_launcher
url_launcher: 6.0.6
dev_dependencies:
flutter_test: