Skip to content

Commit 9d73a3e

Browse files
feat: Add custom NavigationHandler
1 parent 332fa4a commit 9d73a3e

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/src/chatbox.dart

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef SendMessageHandler = void Function(SendMessageEvent event);
2020
typedef TranslationToggledHandler = void Function(TranslationToggledEvent event);
2121
typedef LoadingStateHandler = void Function(LoadingState state);
2222
typedef MessageActionHandler = void Function(MessageActionEvent event);
23+
typedef NavigationHandler = UrlNavigationAction Function(UrlNavigationRequest navigation);
2324

2425
class SendMessageEvent {
2526
final ConversationData conversation;
@@ -52,6 +53,13 @@ class MessageActionEvent {
5253
message = Message.fromJson(json['message']);
5354
}
5455

56+
enum UrlNavigationAction { allow, deny }
57+
58+
class UrlNavigationRequest {
59+
final url;
60+
UrlNavigationRequest(this.url);
61+
}
62+
5563
/// A messaging UI for just a single conversation.
5664
///
5765
/// Create a Chatbox through [Session.createChatbox] and then call [mount] to show it.
@@ -75,7 +83,7 @@ class ChatBox extends StatefulWidget {
7583
final TranslationToggledHandler? onTranslationToggled;
7684
final LoadingStateHandler? onLoadingStateChanged;
7785
final Map<String, MessageActionHandler>? onCustomMessageAction;
78-
final NavigationDelegate? navigationDelegate;
86+
final NavigationHandler? onUrlNavigation;
7987

8088
const ChatBox({
8189
Key? key,
@@ -94,7 +102,7 @@ class ChatBox extends StatefulWidget {
94102
this.onTranslationToggled,
95103
this.onLoadingStateChanged,
96104
this.onCustomMessageAction,
97-
this.navigationDelegate,
105+
this.onUrlNavigation,
98106
}) : super(key: key);
99107

100108
@override
@@ -201,7 +209,7 @@ class ChatBoxState extends State<ChatBox> {
201209
// We need only the VerticalDragGestureRecognizer in order to be able to scroll through the messages
202210
Factory(() => VerticalDragGestureRecognizer()),
203211
},
204-
navigationDelegate: widget.navigationDelegate,
212+
navigationDelegate: _shouldNavigateToUrl,
205213
);
206214
}
207215

@@ -422,6 +430,25 @@ class ChatBoxState extends State<ChatBox> {
422430
widget.onCustomMessageAction?[action]?.call(MessageActionEvent.fromJson(jsonMessage));
423431
}
424432

433+
FutureOr<NavigationDecision> _shouldNavigateToUrl(
434+
NavigationRequest navigationRequest,
435+
) {
436+
if (widget.onUrlNavigation != null) {
437+
final UrlNavigationAction action = widget.onUrlNavigation!(
438+
UrlNavigationRequest(navigationRequest.url),
439+
);
440+
441+
switch (action) {
442+
case UrlNavigationAction.allow:
443+
return NavigationDecision.navigate;
444+
case UrlNavigationAction.deny:
445+
return NavigationDecision.prevent;
446+
}
447+
}
448+
449+
return NavigationDecision.navigate;
450+
}
451+
425452
/// For internal use only. Implementation detail that may change anytime.
426453
///
427454
/// Return a string with a unique ID

0 commit comments

Comments
 (0)