Skip to content

Commit 5ef13c6

Browse files
author
Franco Bugnano
committed
Replace the onUrlNavigation callback with internal logic
1 parent 5d5d894 commit 5ef13c6

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

lib/src/chatbox.dart

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter/gestures.dart';
99

1010
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
1111
import 'package:permission_handler/permission_handler.dart';
12+
import 'package:url_launcher/url_launcher.dart';
1213

1314
import './session.dart';
1415
import './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

pubspec.lock

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ packages:
166166
name: flutter_inappwebview
167167
url: "https://pub.dartlang.org"
168168
source: hosted
169-
version: "6.0.0-beta.12"
169+
version: "6.0.0-beta.22"
170170
flutter_inappwebview_internal_annotations:
171171
dependency: transitive
172172
description:
@@ -180,21 +180,21 @@ packages:
180180
name: flutter_local_notifications
181181
url: "https://pub.dartlang.org"
182182
source: hosted
183-
version: "9.9.1"
183+
version: "13.0.0"
184184
flutter_local_notifications_linux:
185185
dependency: transitive
186186
description:
187187
name: flutter_local_notifications_linux
188188
url: "https://pub.dartlang.org"
189189
source: hosted
190-
version: "0.5.1"
190+
version: "3.0.0"
191191
flutter_local_notifications_platform_interface:
192192
dependency: transitive
193193
description:
194194
name: flutter_local_notifications_platform_interface
195195
url: "https://pub.dartlang.org"
196196
source: hosted
197-
version: "5.0.0"
197+
version: "6.0.0"
198198
flutter_test:
199199
dependency: "direct dev"
200200
description: flutter
@@ -370,14 +370,70 @@ packages:
370370
name: timezone
371371
url: "https://pub.dartlang.org"
372372
source: hosted
373-
version: "0.8.0"
373+
version: "0.9.1"
374374
typed_data:
375375
dependency: transitive
376376
description:
377377
name: typed_data
378378
url: "https://pub.dartlang.org"
379379
source: hosted
380380
version: "1.3.0"
381+
url_launcher:
382+
dependency: "direct main"
383+
description:
384+
name: url_launcher
385+
url: "https://pub.dartlang.org"
386+
source: hosted
387+
version: "6.1.8"
388+
url_launcher_android:
389+
dependency: transitive
390+
description:
391+
name: url_launcher_android
392+
url: "https://pub.dartlang.org"
393+
source: hosted
394+
version: "6.0.23"
395+
url_launcher_ios:
396+
dependency: transitive
397+
description:
398+
name: url_launcher_ios
399+
url: "https://pub.dartlang.org"
400+
source: hosted
401+
version: "6.0.18"
402+
url_launcher_linux:
403+
dependency: transitive
404+
description:
405+
name: url_launcher_linux
406+
url: "https://pub.dartlang.org"
407+
source: hosted
408+
version: "3.0.2"
409+
url_launcher_macos:
410+
dependency: transitive
411+
description:
412+
name: url_launcher_macos
413+
url: "https://pub.dartlang.org"
414+
source: hosted
415+
version: "3.0.2"
416+
url_launcher_platform_interface:
417+
dependency: transitive
418+
description:
419+
name: url_launcher_platform_interface
420+
url: "https://pub.dartlang.org"
421+
source: hosted
422+
version: "2.1.1"
423+
url_launcher_web:
424+
dependency: transitive
425+
description:
426+
name: url_launcher_web
427+
url: "https://pub.dartlang.org"
428+
source: hosted
429+
version: "2.0.14"
430+
url_launcher_windows:
431+
dependency: transitive
432+
description:
433+
name: url_launcher_windows
434+
url: "https://pub.dartlang.org"
435+
source: hosted
436+
version: "3.0.2"
381437
vector_math:
382438
dependency: transitive
383439
description:

pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ dependencies:
1515
flutter:
1616
sdk: flutter
1717

18-
flutter_inappwebview: ^6.0.0-beta.12
18+
flutter_inappwebview: ^6.0.0-beta.22
1919
crypto: ^3.0.1
2020
firebase_core: ^2.3.0
2121
firebase_messaging: ^14.1.1
22-
flutter_local_notifications: ^9.6.0
22+
flutter_local_notifications: ^13.0.0
2323
http: ^0.13.4
2424
flutter_apns_only: ^1.5.2
2525
permission_handler: ^10.1.0
26+
url_launcher: ^6.1.8
2627

2728
dev_dependencies:
2829
flutter_test:

0 commit comments

Comments
 (0)