Skip to content

Commit f485385

Browse files
author
Franco Bugnano
committed
Re-enabled the onUrlNavigation callback on the ChatBox
1 parent 740671b commit f485385

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lib/src/chatbox.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class ChatBox extends StatefulWidget {
8989
final TranslationToggledHandler? onTranslationToggled;
9090
final LoadingStateHandler? onLoadingStateChanged;
9191
final Map<String, MessageActionHandler>? onCustomMessageAction;
92+
final NavigationHandler? onUrlNavigation;
9293

9394
const ChatBox({
9495
Key? key,
@@ -107,6 +108,7 @@ class ChatBox extends StatefulWidget {
107108
this.onTranslationToggled,
108109
this.onLoadingStateChanged,
109110
this.onCustomMessageAction,
111+
this.onUrlNavigation,
110112
}) : super(key: key);
111113

112114
@override
@@ -237,15 +239,27 @@ class ChatBoxState extends State<ChatBox> {
237239
},
238240
shouldOverrideUrlLoading: (InAppWebViewController controller, NavigationAction navigationAction) async {
239241
if (navigationAction.navigationType == NavigationType.LINK_ACTIVATED) {
240-
if (await launchUrl(navigationAction.request.url!)) {
241-
// We launched the browser, so we don't navigate to the URL in the WebView
242-
return NavigationActionPolicy.CANCEL;
243-
} else {
244-
// We couldn't launch the external browser, so as a fallback we're using the default action
245-
return NavigationActionPolicy.ALLOW;
242+
var shouldOpenBrowser = true;
243+
244+
if (widget.onUrlNavigation != null) {
245+
// The onUrlNavigation function has been defined, so let's see if we should open the browser or not
246+
if (widget.onUrlNavigation!(UrlNavigationRequest(navigationAction.request.url!.rawValue)) == UrlNavigationAction.deny) {
247+
shouldOpenBrowser = false;
248+
}
249+
}
250+
251+
if (shouldOpenBrowser) {
252+
if (await launchUrl(navigationAction.request.url!)) {
253+
// We launched the browser, so we don't navigate to the URL in the WebView
254+
return NavigationActionPolicy.CANCEL;
255+
} else {
256+
// We couldn't launch the external browser, so as a fallback we're using the default action
257+
return NavigationActionPolicy.ALLOW;
258+
}
246259
}
247260
}
248-
return NavigationActionPolicy.ALLOW;
261+
262+
return NavigationActionPolicy.CANCEL;
249263
},
250264
);
251265
}

0 commit comments

Comments
 (0)