Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overrideUrlLoading param added to chatbox #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/src/chatbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ChatBox extends StatefulWidget {
final TranslationToggledHandler? onTranslationToggled;
final LoadingStateHandler? onLoadingStateChanged;
final Map<String, MessageActionHandler>? onCustomMessageAction;
final Future<bool> Function(Uri uri)? overrideUrlLoading;

const ChatBox({
Key? key,
Expand All @@ -107,6 +108,7 @@ class ChatBox extends StatefulWidget {
this.onTranslationToggled,
this.onLoadingStateChanged,
this.onCustomMessageAction,
this.overrideUrlLoading,
}) : super(key: key);

@override
Expand Down Expand Up @@ -236,16 +238,20 @@ class ChatBoxState extends State<ChatBox> {
return PermissionResponse(resources: permissionRequest.resources, action: granted ? PermissionResponseAction.GRANT : PermissionResponseAction.DENY);
},
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;
final overridden = widget.overrideUrlLoading == null ? false : await widget.overrideUrlLoading!(navigationAction.request.url!);
if (!overridden) {
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;
}
}
return NavigationActionPolicy.ALLOW;
}
return NavigationActionPolicy.ALLOW;
return NavigationActionPolicy.CANCEL;
},
);
}
Expand Down