From f48538506ec8f1ab9babcfd71860384911acf339 Mon Sep 17 00:00:00 2001 From: Franco Bugnano Date: Wed, 12 Apr 2023 08:56:35 +0200 Subject: [PATCH 1/2] Re-enabled the onUrlNavigation callback on the ChatBox --- lib/src/chatbox.dart | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/src/chatbox.dart b/lib/src/chatbox.dart index 19b9da7..bfb5639 100644 --- a/lib/src/chatbox.dart +++ b/lib/src/chatbox.dart @@ -89,6 +89,7 @@ class ChatBox extends StatefulWidget { final TranslationToggledHandler? onTranslationToggled; final LoadingStateHandler? onLoadingStateChanged; final Map? onCustomMessageAction; + final NavigationHandler? onUrlNavigation; const ChatBox({ Key? key, @@ -107,6 +108,7 @@ class ChatBox extends StatefulWidget { this.onTranslationToggled, this.onLoadingStateChanged, this.onCustomMessageAction, + this.onUrlNavigation, }) : super(key: key); @override @@ -237,15 +239,27 @@ class ChatBoxState extends State { }, shouldOverrideUrlLoading: (InAppWebViewController controller, NavigationAction navigationAction) async { if (navigationAction.navigationType == NavigationType.LINK_ACTIVATED) { - if (await launchUrl(navigationAction.request.url!)) { - // We launched the browser, so we don't navigate to the URL in the WebView - return NavigationActionPolicy.CANCEL; - } else { - // We couldn't launch the external browser, so as a fallback we're using the default action - return NavigationActionPolicy.ALLOW; + var shouldOpenBrowser = true; + + if (widget.onUrlNavigation != null) { + // The onUrlNavigation function has been defined, so let's see if we should open the browser or not + if (widget.onUrlNavigation!(UrlNavigationRequest(navigationAction.request.url!.rawValue)) == UrlNavigationAction.deny) { + shouldOpenBrowser = false; + } + } + + if (shouldOpenBrowser) { + if (await launchUrl(navigationAction.request.url!)) { + // We launched the browser, so we don't navigate to the URL in the WebView + return NavigationActionPolicy.CANCEL; + } else { + // We couldn't launch the external browser, so as a fallback we're using the default action + return NavigationActionPolicy.ALLOW; + } } } - return NavigationActionPolicy.ALLOW; + + return NavigationActionPolicy.CANCEL; }, ); } From a9d0cb52a73cdee78f7da6c6a907bf74bd0ca1ec Mon Sep 17 00:00:00 2001 From: Franco Bugnano Date: Wed, 19 Apr 2023 12:59:22 +0200 Subject: [PATCH 2/2] Fix default return of CANCEL --- lib/src/chatbox.dart | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/src/chatbox.dart b/lib/src/chatbox.dart index bfb5639..c14c04d 100644 --- a/lib/src/chatbox.dart +++ b/lib/src/chatbox.dart @@ -239,27 +239,23 @@ class ChatBoxState extends State { }, shouldOverrideUrlLoading: (InAppWebViewController controller, NavigationAction navigationAction) async { if (navigationAction.navigationType == NavigationType.LINK_ACTIVATED) { - var shouldOpenBrowser = true; - if (widget.onUrlNavigation != null) { // The onUrlNavigation function has been defined, so let's see if we should open the browser or not if (widget.onUrlNavigation!(UrlNavigationRequest(navigationAction.request.url!.rawValue)) == UrlNavigationAction.deny) { - shouldOpenBrowser = false; + return NavigationActionPolicy.CANCEL; } } - if (shouldOpenBrowser) { - if (await launchUrl(navigationAction.request.url!)) { - // We launched the browser, so we don't navigate to the URL in the WebView - return NavigationActionPolicy.CANCEL; - } else { - // We couldn't launch the external browser, so as a fallback we're using the default action - return NavigationActionPolicy.ALLOW; - } + if (await launchUrl(navigationAction.request.url!)) { + // We launched the browser, so we don't navigate to the URL in the WebView + return NavigationActionPolicy.CANCEL; + } else { + // We couldn't launch the external browser, so as a fallback we're using the default action + return NavigationActionPolicy.ALLOW; } } - return NavigationActionPolicy.CANCEL; + return NavigationActionPolicy.ALLOW; }, ); }