Skip to content

Commit 9dd9e3f

Browse files
authored
Merge pull request #15 from Alexander-Ordina/feature/navigation-delegate
feat: Adds navigationDelegate for handling links
2 parents b59dc43 + 3f454b0 commit 9dd9e3f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lib/src/chatbox.dart

Lines changed: 33 additions & 0 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 navigationRequest);
2324

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

56+
enum UrlNavigationAction { allow, deny }
57+
58+
class UrlNavigationRequest {
59+
final String url;
60+
61+
UrlNavigationRequest(
62+
this.url,
63+
);
64+
}
65+
5566
/// A messaging UI for just a single conversation.
5667
///
5768
/// Create a Chatbox through [Session.createChatbox] and then call [mount] to show it.
@@ -75,6 +86,7 @@ class ChatBox extends StatefulWidget {
7586
final TranslationToggledHandler? onTranslationToggled;
7687
final LoadingStateHandler? onLoadingStateChanged;
7788
final Map<String, MessageActionHandler>? onCustomMessageAction;
89+
final NavigationHandler? onUrlNavigation;
7890

7991
const ChatBox({
8092
Key? key,
@@ -93,6 +105,7 @@ class ChatBox extends StatefulWidget {
93105
this.onTranslationToggled,
94106
this.onLoadingStateChanged,
95107
this.onCustomMessageAction,
108+
this.onUrlNavigation,
96109
}) : super(key: key);
97110

98111
@override
@@ -199,6 +212,7 @@ class ChatBoxState extends State<ChatBox> {
199212
// We need only the VerticalDragGestureRecognizer in order to be able to scroll through the messages
200213
Factory(() => VerticalDragGestureRecognizer()),
201214
},
215+
navigationDelegate: _shouldNavigateToUrl,
202216
);
203217
}
204218

@@ -419,6 +433,25 @@ class ChatBoxState extends State<ChatBox> {
419433
widget.onCustomMessageAction?[action]?.call(MessageActionEvent.fromJson(jsonMessage));
420434
}
421435

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

0 commit comments

Comments
 (0)