From d13a8013acfa0a8cd0c339498e556548f8dad4e3 Mon Sep 17 00:00:00 2001 From: Freek van de Ven Date: Thu, 13 Jun 2024 09:33:11 +0200 Subject: [PATCH] feat: add onPopInvoked callback for a popscope inside the userstory --- CHANGELOG.md | 1 + .../src/flutter_chat_navigator_userstory.dart | 67 ++++++++++--------- .../lib/src/flutter_chat_userstory.dart | 21 +++--- .../lib/src/models/chat_configuration.dart | 6 ++ 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0f230e..5ed353a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - fix bug where you could make multiple groups quickly by routing back to the previous screen - fix bug where you would route back to the user selection screen insterad of routing back to the chat overview screen +- Add onPopInvoked callback to the userstory to add custom behaviour for the back button on the chatscreen ## 3.0.0 diff --git a/packages/flutter_chat/lib/src/flutter_chat_navigator_userstory.dart b/packages/flutter_chat/lib/src/flutter_chat_navigator_userstory.dart index 9a9fa89..d3cead9 100644 --- a/packages/flutter_chat/lib/src/flutter_chat_navigator_userstory.dart +++ b/packages/flutter_chat/lib/src/flutter_chat_navigator_userstory.dart @@ -30,48 +30,53 @@ Widget _chatScreenRoute( ChatUserStoryConfiguration configuration, BuildContext context, ) => - ChatScreen( - unreadMessageTextStyle: configuration.unreadMessageTextStyle, - service: configuration.chatService, - options: configuration.chatOptionsBuilder(context), - onNoChats: () async => Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => _newChatScreenRoute( - configuration, - context, - ), - ), - ), - onPressStartChat: () async { - if (configuration.onPressStartChat != null) { - return await configuration.onPressStartChat?.call(); - } - - return Navigator.of(context).push( + PopScope( + canPop: configuration.onPopInvoked == null, + onPopInvoked: (didPop) => + configuration.onPopInvoked?.call(didPop, context), + child: ChatScreen( + unreadMessageTextStyle: configuration.unreadMessageTextStyle, + service: configuration.chatService, + options: configuration.chatOptionsBuilder(context), + onNoChats: () async => Navigator.of(context).push( MaterialPageRoute( builder: (context) => _newChatScreenRoute( configuration, context, ), ), - ); - }, - onPressChat: (chat) async => - configuration.onPressChat?.call(context, chat) ?? - await Navigator.of(context).push( + ), + onPressStartChat: () async { + if (configuration.onPressStartChat != null) { + return await configuration.onPressStartChat?.call(); + } + + return Navigator.of(context).push( MaterialPageRoute( - builder: (context) => _chatDetailScreenRoute( + builder: (context) => _newChatScreenRoute( configuration, context, - chat.id!, ), ), - ), - onDeleteChat: (chat) async => - configuration.onDeleteChat?.call(context, chat) ?? - configuration.chatService.chatOverviewService.deleteChat(chat), - deleteChatDialog: configuration.deleteChatDialog, - translations: configuration.translations, + ); + }, + onPressChat: (chat) async => + configuration.onPressChat?.call(context, chat) ?? + await Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => _chatDetailScreenRoute( + configuration, + context, + chat.id!, + ), + ), + ), + onDeleteChat: (chat) async => + configuration.onDeleteChat?.call(context, chat) ?? + configuration.chatService.chatOverviewService.deleteChat(chat), + deleteChatDialog: configuration.deleteChatDialog, + translations: configuration.translations, + ), ); /// Constructs the chat detail screen route widget. diff --git a/packages/flutter_chat/lib/src/flutter_chat_userstory.dart b/packages/flutter_chat/lib/src/flutter_chat_userstory.dart index dc5c22c..b37d4c8 100644 --- a/packages/flutter_chat/lib/src/flutter_chat_userstory.dart +++ b/packages/flutter_chat/lib/src/flutter_chat_userstory.dart @@ -43,14 +43,19 @@ List getChatStoryRoutes( return buildScreenWithoutTransition( context: context, state: state, - child: configuration.chatPageBuilder?.call( - context, - chatScreen, - ) ?? - Scaffold( - backgroundColor: theme.colorScheme.surface, - body: chatScreen, - ), + child: PopScope( + canPop: configuration.onPopInvoked == null, + onPopInvoked: (didPop) => + configuration.onPopInvoked?.call(didPop, context), + child: configuration.chatPageBuilder?.call( + context, + chatScreen, + ) ?? + Scaffold( + backgroundColor: theme.colorScheme.surface, + body: chatScreen, + ), + ), ); }, ), diff --git a/packages/flutter_chat/lib/src/models/chat_configuration.dart b/packages/flutter_chat/lib/src/models/chat_configuration.dart index 9b8657f..8e00728 100644 --- a/packages/flutter_chat/lib/src/models/chat_configuration.dart +++ b/packages/flutter_chat/lib/src/models/chat_configuration.dart @@ -21,6 +21,7 @@ class ChatUserStoryConfiguration { this.onMessageSubmit, this.onReadChat, this.onUploadImage, + this.onPopInvoked, this.onPressCreateChat, this.onPressCreateGroupChat, this.onPressCompleteGroupChatCreation, @@ -118,6 +119,11 @@ class ChatUserStoryConfiguration { /// Callback function triggered when user profile is pressed. final Function(BuildContext context, ChatUserModel user)? onPressUserProfile; + /// Callback function triggered when the popscope on the chat + /// homepage is triggered. + // ignore: avoid_positional_boolean_parameters + final Function(bool didPop, BuildContext context)? onPopInvoked; + final double? textfieldBottomPadding; final Color? iconDisabledColor;