mirror of
https://github.com/Iconica-Development/flutter_chat.git
synced 2025-05-18 18:33:49 +02:00
feat: onPressChatTitle
This commit is contained in:
parent
5e53049a3e
commit
7fd38e3770
9 changed files with 210 additions and 175 deletions
|
@ -4,146 +4,29 @@
|
|||
|
||||
library flutter_community_chat;
|
||||
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_community_chat/service/chat_service.dart';
|
||||
import 'package:flutter_community_chat_interface/flutter_community_chat_interface.dart';
|
||||
import 'package:flutter_community_chat_view/flutter_community_chat_view.dart';
|
||||
import 'package:flutter_image_picker/flutter_image_picker.dart';
|
||||
export 'package:flutter_community_chat_view/flutter_community_chat_view.dart';
|
||||
export 'package:flutter_community_chat/service/chat_service.dart';
|
||||
export 'package:flutter_community_chat_interface/flutter_community_chat_interface.dart';
|
||||
|
||||
class CommunityChat extends StatefulWidget {
|
||||
class CommunityChat extends StatelessWidget {
|
||||
const CommunityChat({
|
||||
required this.dataProvider,
|
||||
this.options = const ChatOptions(),
|
||||
this.translations = const ChatTranslations(),
|
||||
this.imagePickerTheme = const ImagePickerTheme(),
|
||||
this.imagePickerConfig = const ImagePickerConfig(),
|
||||
required this.chatService,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final CommunityChatInterface dataProvider;
|
||||
final ChatOptions options;
|
||||
final ChatTranslations translations;
|
||||
final ImagePickerTheme imagePickerTheme;
|
||||
final ImagePickerConfig imagePickerConfig;
|
||||
|
||||
@override
|
||||
State<CommunityChat> createState() => _CommunityChatState();
|
||||
}
|
||||
|
||||
class _CommunityChatState extends State<CommunityChat> {
|
||||
bool _isFetchingUsers = false;
|
||||
|
||||
Future<void> _push(BuildContext context, Widget widget) =>
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => widget),
|
||||
);
|
||||
|
||||
void _pop(BuildContext context) => Navigator.of(context).pop();
|
||||
|
||||
Future<void> _onPressStartChat(BuildContext context) async {
|
||||
if (!_isFetchingUsers) {
|
||||
_isFetchingUsers = true;
|
||||
await widget.dataProvider.getChatUsers().then(
|
||||
(users) {
|
||||
_isFetchingUsers = false;
|
||||
_push(
|
||||
context,
|
||||
NewChatScreen(
|
||||
options: widget.options,
|
||||
translations: widget.translations,
|
||||
onPressCreateChat: (user) => _onPressChat(
|
||||
context,
|
||||
PersonalChatModel(user: user),
|
||||
popBeforePush: true,
|
||||
),
|
||||
users: users,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onPressChat(
|
||||
BuildContext context,
|
||||
ChatModel chat, {
|
||||
bool popBeforePush = false,
|
||||
}) =>
|
||||
widget.dataProvider.setChat(chat).then((_) {
|
||||
if (popBeforePush) {
|
||||
_pop(context);
|
||||
}
|
||||
_push(
|
||||
context,
|
||||
ChatDetailScreen(
|
||||
options: widget.options,
|
||||
translations: widget.translations,
|
||||
chat: chat,
|
||||
chatMessages: widget.dataProvider.getMessagesStream(),
|
||||
onPressSelectImage: (ChatModel chat) =>
|
||||
_onPressSelectImage(context, chat),
|
||||
onMessageSubmit: (ChatModel chat, String content) =>
|
||||
widget.dataProvider.sendTextMessage(content),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
void _beforeUploadingImage() => ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
duration: const Duration(minutes: 1),
|
||||
content: Row(
|
||||
children: [
|
||||
const SizedBox(
|
||||
width: 25,
|
||||
height: 25,
|
||||
child: CircularProgressIndicator(color: Colors.grey),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0),
|
||||
child: Text(widget.translations.imageUploading),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
_afterUploadingImage() => ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
||||
|
||||
Future<void> _onPressSelectImage(BuildContext context, ChatModel chat) =>
|
||||
showModalBottomSheet<Uint8List?>(
|
||||
context: context,
|
||||
builder: (BuildContext context) =>
|
||||
widget.options.imagePickerContainerBuilder(
|
||||
ImagePicker(
|
||||
customButton: widget.options.closeImagePickerButtonBuilder(
|
||||
context,
|
||||
() => Navigator.of(context).pop(),
|
||||
widget.translations,
|
||||
),
|
||||
imagePickerTheme: widget.imagePickerTheme,
|
||||
imagePickerConfig: widget.imagePickerConfig,
|
||||
),
|
||||
),
|
||||
).then(
|
||||
(image) async {
|
||||
_beforeUploadingImage();
|
||||
|
||||
if (image != null) {
|
||||
await widget.dataProvider.sendImageMessage(image);
|
||||
}
|
||||
|
||||
_afterUploadingImage();
|
||||
},
|
||||
);
|
||||
final ChatService chatService;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => ChatScreen(
|
||||
chats: widget.dataProvider.getChatsStream(),
|
||||
onPressStartChat: () => _onPressStartChat(context),
|
||||
onPressChat: (chat) => _onPressChat(context, chat),
|
||||
onDeleteChat: (ChatModel chat) => widget.dataProvider.deleteChat(chat),
|
||||
options: widget.options,
|
||||
translations: widget.translations,
|
||||
chats: chatService.dataProvider.getChatsStream(),
|
||||
onPressStartChat: () => chatService.onPressStartChat(context),
|
||||
onPressChat: (chat) => chatService.onPressChat(context, chat),
|
||||
onDeleteChat: (ChatModel chat) => chatService.deleteChat(chat),
|
||||
options: chatService.options,
|
||||
translations: chatService.translations(context),
|
||||
);
|
||||
}
|
||||
|
|
127
packages/flutter_community_chat/lib/service/chat_service.dart
Normal file
127
packages/flutter_community_chat/lib/service/chat_service.dart
Normal file
|
@ -0,0 +1,127 @@
|
|||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_community_chat/ui/components/image_loading_snackbar.dart';
|
||||
import 'package:flutter_community_chat_interface/flutter_community_chat_interface.dart';
|
||||
import 'package:flutter_community_chat_view/flutter_community_chat_view.dart';
|
||||
import 'package:flutter_image_picker/flutter_image_picker.dart';
|
||||
|
||||
abstract class ChatService {
|
||||
ChatService({
|
||||
required this.options,
|
||||
required this.imagePickerTheme,
|
||||
required this.imagePickerConfig,
|
||||
required this.dataProvider,
|
||||
});
|
||||
|
||||
final CommunityChatInterface dataProvider;
|
||||
final ChatOptions options;
|
||||
final ImagePickerTheme imagePickerTheme;
|
||||
final ImagePickerConfig imagePickerConfig;
|
||||
bool _isFetchingUsers = false;
|
||||
|
||||
ChatTranslations translations(BuildContext buildContext);
|
||||
|
||||
Future<void> _push(BuildContext context, Widget widget) =>
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (context) => widget),
|
||||
);
|
||||
|
||||
void _pop(BuildContext context) => Navigator.of(context).pop();
|
||||
|
||||
Future<void> onPressStartChat(BuildContext context) async {
|
||||
if (!_isFetchingUsers) {
|
||||
_isFetchingUsers = true;
|
||||
await dataProvider.getChatUsers().then(
|
||||
(users) {
|
||||
_isFetchingUsers = false;
|
||||
_push(
|
||||
context,
|
||||
buildNewChatScreen(context, users),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget buildNewChatScreen(
|
||||
BuildContext context,
|
||||
List<ChatUserModel> users,
|
||||
) =>
|
||||
NewChatScreen(
|
||||
options: options,
|
||||
translations: translations(context),
|
||||
onPressCreateChat: (user) => onPressChat(
|
||||
context,
|
||||
PersonalChatModel(user: user),
|
||||
popBeforePush: true,
|
||||
),
|
||||
users: users,
|
||||
);
|
||||
|
||||
Future<void> onPressChat(
|
||||
BuildContext context,
|
||||
ChatModel chat, {
|
||||
bool popBeforePush = false,
|
||||
}) =>
|
||||
dataProvider.setChat(chat).then((_) {
|
||||
if (popBeforePush) {
|
||||
_pop(context);
|
||||
}
|
||||
_push(
|
||||
context,
|
||||
buildChatDetailScreen(context, chat),
|
||||
);
|
||||
});
|
||||
|
||||
Widget buildChatDetailScreen(
|
||||
BuildContext context,
|
||||
ChatModel chat,
|
||||
) =>
|
||||
ChatDetailScreen(
|
||||
options: options,
|
||||
translations: translations(context),
|
||||
chat: chat,
|
||||
chatMessages: dataProvider.getMessagesStream(),
|
||||
onPressSelectImage: (ChatModel chat) => onPressSelectImage(
|
||||
context,
|
||||
chat,
|
||||
),
|
||||
onMessageSubmit: (ChatModel chat, String content) =>
|
||||
dataProvider.sendTextMessage(content),
|
||||
);
|
||||
|
||||
Future<void> onPressSelectImage(
|
||||
BuildContext context,
|
||||
ChatModel chat,
|
||||
) =>
|
||||
showModalBottomSheet<Uint8List?>(
|
||||
context: context,
|
||||
builder: (BuildContext context) => options.imagePickerContainerBuilder(
|
||||
ImagePicker(
|
||||
customButton: options.closeImagePickerButtonBuilder(
|
||||
context,
|
||||
() => Navigator.of(context).pop(),
|
||||
translations(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
).then(
|
||||
(image) async {
|
||||
var messenger = ScaffoldMessenger.of(context);
|
||||
|
||||
messenger.showSnackBar(
|
||||
getImageLoadingSnackbar(
|
||||
translations(context),
|
||||
),
|
||||
);
|
||||
|
||||
if (image != null) {
|
||||
await dataProvider.sendImageMessage(image);
|
||||
}
|
||||
|
||||
messenger.hideCurrentSnackBar();
|
||||
},
|
||||
);
|
||||
|
||||
Future<void> deleteChat(ChatModel chat) => dataProvider.deleteChat(chat);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_community_chat/flutter_community_chat.dart';
|
||||
|
||||
SnackBar getImageLoadingSnackbar(ChatTranslations translations) => SnackBar(
|
||||
duration: const Duration(minutes: 1),
|
||||
content: Row(
|
||||
children: [
|
||||
const SizedBox(
|
||||
width: 25,
|
||||
height: 25,
|
||||
child: CircularProgressIndicator(color: Colors.grey),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 16.0),
|
||||
child: Text(translations.imageUploading),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
|
@ -179,7 +179,7 @@ packages:
|
|||
description:
|
||||
path: "packages/flutter_community_chat_interface"
|
||||
ref: HEAD
|
||||
resolved-ref: "09686cb451b5dc9c94e1088ce74dbee811064cdb"
|
||||
resolved-ref: "772764f84a2282f2f49de4a0910ae15024ceabd5"
|
||||
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
|
||||
source: git
|
||||
version: "0.0.1"
|
||||
|
@ -188,7 +188,7 @@ packages:
|
|||
description:
|
||||
path: "packages/flutter_community_chat_view"
|
||||
ref: HEAD
|
||||
resolved-ref: "09686cb451b5dc9c94e1088ce74dbee811064cdb"
|
||||
resolved-ref: "772764f84a2282f2f49de4a0910ae15024ceabd5"
|
||||
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
|
||||
source: git
|
||||
version: "0.0.1"
|
||||
|
|
|
@ -23,7 +23,7 @@ dependencies:
|
|||
git:
|
||||
url: https://github.com/Iconica-Development/flutter_image_picker
|
||||
ref: 1.0.3
|
||||
|
||||
|
||||
dev_dependencies:
|
||||
flutter_lints: ^2.0.0
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ packages:
|
|||
name: _flutterfire_internals
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.0.8"
|
||||
version: "1.0.9"
|
||||
analyzer:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -84,21 +84,21 @@ packages:
|
|||
name: cloud_firestore
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.0.5"
|
||||
version: "4.1.0"
|
||||
cloud_firestore_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: cloud_firestore_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "5.8.5"
|
||||
version: "5.9.0"
|
||||
cloud_firestore_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: cloud_firestore_web
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.0.5"
|
||||
version: "3.1.0"
|
||||
code_builder:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -154,28 +154,28 @@ packages:
|
|||
name: firebase_auth
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.1.2"
|
||||
version: "4.1.3"
|
||||
firebase_auth_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_auth_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "6.11.2"
|
||||
version: "6.11.3"
|
||||
firebase_auth_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_auth_web
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "5.1.2"
|
||||
version: "5.1.3"
|
||||
firebase_core:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: firebase_core
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.2.0"
|
||||
version: "2.3.0"
|
||||
firebase_core_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -196,21 +196,21 @@ packages:
|
|||
name: firebase_storage
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "11.0.5"
|
||||
version: "11.0.6"
|
||||
firebase_storage_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_storage_platform_interface
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.1.23"
|
||||
version: "4.1.24"
|
||||
firebase_storage_web:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: firebase_storage_web
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.3.15"
|
||||
version: "3.3.16"
|
||||
fixnum:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -228,7 +228,7 @@ packages:
|
|||
description:
|
||||
path: "packages/flutter_community_chat_interface"
|
||||
ref: HEAD
|
||||
resolved-ref: "09686cb451b5dc9c94e1088ce74dbee811064cdb"
|
||||
resolved-ref: "772764f84a2282f2f49de4a0910ae15024ceabd5"
|
||||
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
|
||||
source: git
|
||||
version: "0.0.1"
|
||||
|
|
|
@ -186,7 +186,7 @@ packages:
|
|||
description:
|
||||
path: "packages/flutter_community_chat_interface"
|
||||
ref: HEAD
|
||||
resolved-ref: "09686cb451b5dc9c94e1088ce74dbee811064cdb"
|
||||
resolved-ref: "772764f84a2282f2f49de4a0910ae15024ceabd5"
|
||||
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
|
||||
source: git
|
||||
version: "0.0.1"
|
||||
|
|
|
@ -17,6 +17,7 @@ class ChatDetailScreen extends StatelessWidget {
|
|||
this.translations = const ChatTranslations(),
|
||||
this.chatMessages,
|
||||
this.onPressSelectImage,
|
||||
this.onPressChatTitle,
|
||||
super.key,
|
||||
});
|
||||
|
||||
|
@ -26,45 +27,50 @@ class ChatDetailScreen extends StatelessWidget {
|
|||
final Stream<List<ChatMessageModel>>? chatMessages;
|
||||
final Function(ChatModel)? onPressSelectImage;
|
||||
final Future<void> Function(ChatModel chat, String text) onMessageSubmit;
|
||||
final Future<void> Function(ChatModel chat)? onPressChatTitle;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
appBar: AppBar(
|
||||
centerTitle: true,
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (chat is PersonalChatModel) ...[
|
||||
ChatImage(
|
||||
image: (chat as PersonalChatModel).user.imageUrl,
|
||||
size: 36.0,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 15.5),
|
||||
child: Text(
|
||||
(chat as PersonalChatModel).user.name ?? '',
|
||||
style: const TextStyle(fontSize: 18),
|
||||
title: GestureDetector(
|
||||
onTap: () =>
|
||||
onPressChatTitle != null ? onPressChatTitle!(chat) : {},
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (chat is PersonalChatModel) ...[
|
||||
ChatImage(
|
||||
image: (chat as PersonalChatModel).user.imageUrl,
|
||||
size: 36.0,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 15.5),
|
||||
child: Text(
|
||||
(chat as PersonalChatModel).user.name ?? '',
|
||||
style: const TextStyle(fontSize: 18),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
if (chat is GroupChatModel) ...[
|
||||
ChatImage(
|
||||
image: (chat as GroupChatModel).imageUrl,
|
||||
size: 36.0,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 15.5),
|
||||
child: Text(
|
||||
(chat as GroupChatModel).title,
|
||||
style: const TextStyle(fontSize: 18),
|
||||
],
|
||||
if (chat is GroupChatModel) ...[
|
||||
ChatImage(
|
||||
image: (chat as GroupChatModel).imageUrl,
|
||||
size: 36.0,
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(left: 15.5),
|
||||
child: Text(
|
||||
(chat as GroupChatModel).title,
|
||||
style: const TextStyle(fontSize: 18),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
body: Column(
|
||||
|
|
|
@ -179,7 +179,7 @@ packages:
|
|||
description:
|
||||
path: "packages/flutter_community_chat_interface"
|
||||
ref: HEAD
|
||||
resolved-ref: "09686cb451b5dc9c94e1088ce74dbee811064cdb"
|
||||
resolved-ref: "772764f84a2282f2f49de4a0910ae15024ceabd5"
|
||||
url: "https://github.com/Iconica-Development/flutter_community_chat.git"
|
||||
source: git
|
||||
version: "0.0.1"
|
||||
|
|
Loading…
Reference in a new issue