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:
@@ -1,6 +1,7 @@
import 'dart:io';
import 'package:core/core.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
@@ -8,6 +9,7 @@ import 'package:get/get.dart';
import 'package:model/model.dart';
import 'package:tmail_ui_user/features/email/presentation/email_controller.dart';
import 'package:tmail_ui_user/features/email/presentation/model/text_format.dart';
import 'package:tmail_ui_user/features/email/presentation/model/web_view_state.dart';
import 'package:tmail_ui_user/features/email/presentation/widgets/app_bar_mail_widget_builder.dart';
import 'package:tmail_ui_user/features/email/presentation/widgets/attachment_file_tile_builder.dart';
import 'package:tmail_ui_user/features/email/presentation/widgets/bottom_bar_mail_widget_builder.dart';
@@ -119,7 +121,7 @@ class EmailView extends GetView {
Widget _buildLoadingView() {
return Obx(() => emailController.viewState.value.fold(
(failure) => SizedBox.shrink(),
(success) => success is LoadingState
(success) => success is LoadingState || success is WebViewLoadingState
? Center(child: Padding(
padding: EdgeInsets.only(top: 16, bottom: 16),
child: SizedBox(
@@ -148,8 +150,8 @@ class EmailView extends GetView {
emailController.emailAddressExpandMode.value)
.onOpenExpandAddressReceiverActionClick(() => emailController.toggleDisplayEmailAddressAction(expandMode: ExpandMode.EXPAND))
.build()),
_buildLoadingView(),
_buildListAttachments(context),
_buildLoadingView(),
_buildListMessageContent(),
],
)
@@ -293,6 +295,13 @@ class EmailView extends GetView {
color: AppColor.mailboxTextColor)))
: HtmlContentViewer(
message: messageContents.first.content,
onWebViewLoadStart: (webController, uri) {
emailController.dispatchState(Right(WebViewLoadingState()));
},
onWebViewLoadStop: (webController, uri) {
emailController.clearState();
},
mailtoDelegate: (uri) async {},
);
} else {
return SizedBox.shrink();
@@ -0,0 +1,9 @@
import 'package:core/core.dart';
class WebViewLoadingState extends UIState {
WebViewLoadingState();
@override
List<Object?> get props => [];
}
+2
View File
@@ -1,6 +1,8 @@
name: model
description: A model module contains entities
# 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 +.