Skip to content

Commit b49edf9

Browse files
author
Franco Bugnano
committed
Ported onUrlNavigation to InAppWebView
1 parent b8f16bc commit b49edf9

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

lib/src/chatbox.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class MessageActionEvent {
5555
message = Message.fromJson(json['message']);
5656
}
5757

58-
enum UrlNavigationAction { allow, deny }
59-
6058
class UrlNavigationRequest {
6159
final String url;
6260

@@ -65,6 +63,8 @@ class UrlNavigationRequest {
6563
);
6664
}
6765

66+
enum UrlNavigationAction { deny, allow }
67+
6868
/// A messaging UI for just a single conversation.
6969
///
7070
/// Create a Chatbox through [Session.createChatbox] and then call [mount] to show it.
@@ -206,6 +206,7 @@ class ChatBoxState extends State<ChatBox> {
206206
initialSettings: InAppWebViewSettings(
207207
useHybridComposition: true,
208208
disableInputAccessoryView: true,
209+
useShouldOverrideUrlLoading: true,
209210
),
210211
onWebViewCreated: _onWebViewCreated,
211212
onLoadStop: _onLoadStop,
@@ -234,7 +235,7 @@ class ChatBoxState extends State<ChatBox> {
234235
// We need only the VerticalDragGestureRecognizer in order to be able to scroll through the messages
235236
Factory(() => VerticalDragGestureRecognizer()),
236237
},
237-
navigationDelegate: _shouldNavigateToUrl,
238+
shouldOverrideUrlLoading: _shouldNavigateToUrl,
238239
);
239240
}
240241

@@ -391,10 +392,10 @@ class ChatBoxState extends State<ChatBox> {
391392
controller.addJavaScriptHandler(handlerName: 'JSCCustomMessageAction', callback: _jscCustomMessageAction);
392393

393394
String htmlData = await rootBundle.loadString('packages/talkjs_flutter/assets/index.html');
394-
controller.loadData(data: htmlData, baseUrl: Uri.parse("https://app.talkjs.com"));
395+
controller.loadData(data: htmlData, baseUrl: WebUri("https://app.talkjs.com"));
395396
}
396397

397-
void _onLoadStop(InAppWebViewController controller, Uri? url) async {
398+
void _onLoadStop(InAppWebViewController controller, WebUri? url) async {
398399
if (kDebugMode) {
399400
print('📗 chatbox._onLoadStop ($url)');
400401
}
@@ -465,23 +466,24 @@ class ChatBoxState extends State<ChatBox> {
465466
widget.onCustomMessageAction?[action]?.call(MessageActionEvent.fromJson(jsonMessage));
466467
}
467468

468-
FutureOr<NavigationDecision> _shouldNavigateToUrl(
469-
NavigationRequest navigationRequest,
470-
) {
469+
Future<NavigationActionPolicy?> _shouldNavigateToUrl(
470+
InAppWebViewController controller,
471+
NavigationAction navigationAction,
472+
) async {
471473
if (widget.onUrlNavigation != null) {
472474
final UrlNavigationAction action = widget.onUrlNavigation!(
473-
UrlNavigationRequest(navigationRequest.url),
475+
UrlNavigationRequest(navigationAction.request.url!.rawValue),
474476
);
475477

476478
switch (action) {
477479
case UrlNavigationAction.allow:
478-
return NavigationDecision.navigate;
480+
return NavigationActionPolicy.ALLOW;
479481
case UrlNavigationAction.deny:
480-
return NavigationDecision.prevent;
482+
return NavigationActionPolicy.CANCEL;
481483
}
482484
}
483485

484-
return NavigationDecision.navigate;
486+
return NavigationActionPolicy.ALLOW;
485487
}
486488

487489
/// For internal use only. Implementation detail that may change anytime.

lib/src/conversationlist.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ class ConversationListState extends State<ConversationList> {
207207
controller.addJavaScriptHandler(handlerName: 'JSCLoadingState', callback: _jscLoadingState);
208208

209209
String htmlData = await rootBundle.loadString('packages/talkjs_flutter/assets/index.html');
210-
controller.loadData(data: htmlData, baseUrl: Uri.parse("https://app.talkjs.com"));
210+
controller.loadData(data: htmlData, baseUrl: WebUri("https://app.talkjs.com"));
211211
}
212212

213-
void _onLoadStop(InAppWebViewController controller, Uri? url) async {
213+
void _onLoadStop(InAppWebViewController controller, WebUri? url) async {
214214
if (kDebugMode) {
215215
print('📗 conversationlist._onLoadStop ($url)');
216216
}

pubspec.lock

Lines changed: 2 additions & 2 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.3"
169+
version: "6.0.0-beta.12"
170170
flutter_inappwebview_internal_annotations:
171171
dependency: transitive
172172
description:
@@ -401,4 +401,4 @@ packages:
401401
version: "6.1.0"
402402
sdks:
403403
dart: ">=2.17.0 <3.0.0"
404-
flutter: ">=2.8.1"
404+
flutter: ">=3.0.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dependencies:
1515
flutter:
1616
sdk: flutter
1717

18-
flutter_inappwebview: ^6.0.0-beta.3
18+
flutter_inappwebview: ^6.0.0-beta.12
1919
crypto: ^3.0.1
2020
firebase_core: ^1.24.0
2121
firebase_messaging: ^13.1.0

0 commit comments

Comments
 (0)