@@ -9,6 +9,7 @@ import 'package:flutter/gestures.dart';
99
1010import 'package:flutter_inappwebview/flutter_inappwebview.dart' ;
1111import 'package:permission_handler/permission_handler.dart' ;
12+ import 'package:url_launcher/url_launcher.dart' ;
1213
1314import './session.dart' ;
1415import './conversation.dart' ;
@@ -88,7 +89,6 @@ class ChatBox extends StatefulWidget {
8889 final TranslationToggledHandler ? onTranslationToggled;
8990 final LoadingStateHandler ? onLoadingStateChanged;
9091 final Map <String , MessageActionHandler >? onCustomMessageAction;
91- final NavigationHandler ? onUrlNavigation;
9292
9393 const ChatBox ({
9494 Key ? key,
@@ -107,7 +107,6 @@ class ChatBox extends StatefulWidget {
107107 this .onTranslationToggled,
108108 this .onLoadingStateChanged,
109109 this .onCustomMessageAction,
110- this .onUrlNavigation,
111110 }) : super (key: key);
112111
113112 @override
@@ -235,7 +234,18 @@ class ChatBoxState extends State<ChatBox> {
235234 // We need only the VerticalDragGestureRecognizer in order to be able to scroll through the messages
236235 Factory (() => VerticalDragGestureRecognizer ()),
237236 },
238- shouldOverrideUrlLoading: _shouldNavigateToUrl,
237+ shouldOverrideUrlLoading: (InAppWebViewController controller, NavigationAction navigationAction) async {
238+ if (navigationAction.navigationType == NavigationType .LINK_ACTIVATED ) {
239+ if (await launchUrl (navigationAction.request.url! )) {
240+ // We launched the browser, so we don't navigate to the URL in the WebView
241+ return NavigationActionPolicy .CANCEL ;
242+ } else {
243+ // We couldn't launch the external browser, so as a fallback we're using the default action
244+ return NavigationActionPolicy .ALLOW ;
245+ }
246+ }
247+ return NavigationActionPolicy .ALLOW ;
248+ },
239249 );
240250 }
241251
@@ -466,26 +476,6 @@ class ChatBoxState extends State<ChatBox> {
466476 widget.onCustomMessageAction? [action]? .call (MessageActionEvent .fromJson (jsonMessage));
467477 }
468478
469- Future <NavigationActionPolicy ?> _shouldNavigateToUrl (
470- InAppWebViewController controller,
471- NavigationAction navigationAction,
472- ) async {
473- if (widget.onUrlNavigation != null ) {
474- final UrlNavigationAction action = widget.onUrlNavigation !(
475- UrlNavigationRequest (navigationAction.request.url! .rawValue),
476- );
477-
478- switch (action) {
479- case UrlNavigationAction .allow:
480- return NavigationActionPolicy .ALLOW ;
481- case UrlNavigationAction .deny:
482- return NavigationActionPolicy .CANCEL ;
483- }
484- }
485-
486- return NavigationActionPolicy .ALLOW ;
487- }
488-
489479 /// For internal use only. Implementation detail that may change anytime.
490480 ///
491481 /// Return a string with a unique ID
0 commit comments