mirror of
https://github.com/Iconica-Development/flutter_chat.git
synced 2025-05-19 10:53:51 +02:00
feat: fix opening multiple 'New Chat screens'
This commit is contained in:
parent
bd40856e1a
commit
9e0378fe74
1 changed files with 37 additions and 20 deletions
|
@ -11,7 +11,7 @@ import 'package:flutter_community_chat_view/flutter_community_chat_view.dart';
|
||||||
import 'package:flutter_image_picker/flutter_image_picker.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_view/flutter_community_chat_view.dart';
|
||||||
|
|
||||||
class CommunityChat extends StatelessWidget {
|
class CommunityChat extends StatefulWidget {
|
||||||
const CommunityChat({
|
const CommunityChat({
|
||||||
required this.dataProvider,
|
required this.dataProvider,
|
||||||
this.options = const ChatOptions(),
|
this.options = const ChatOptions(),
|
||||||
|
@ -27,17 +27,29 @@ class CommunityChat extends StatelessWidget {
|
||||||
final ImagePickerTheme imagePickerTheme;
|
final ImagePickerTheme imagePickerTheme;
|
||||||
final ImagePickerConfig imagePickerConfig;
|
final ImagePickerConfig imagePickerConfig;
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<CommunityChat> createState() => _CommunityChatState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CommunityChatState extends State<CommunityChat> {
|
||||||
|
bool _isFetchingUsers = false;
|
||||||
|
|
||||||
Future<void> _push(BuildContext context, Widget widget) =>
|
Future<void> _push(BuildContext context, Widget widget) =>
|
||||||
Navigator.of(context).push(
|
Navigator.of(context).push(
|
||||||
MaterialPageRoute(builder: (context) => widget),
|
MaterialPageRoute(builder: (context) => widget),
|
||||||
);
|
);
|
||||||
|
|
||||||
Future<void> _onPressStartChat(BuildContext context) =>
|
Future<void> _onPressStartChat(BuildContext context) async {
|
||||||
dataProvider.getChatUsers().then((users) => _push(
|
if (!_isFetchingUsers) {
|
||||||
|
_isFetchingUsers = true;
|
||||||
|
await widget.dataProvider.getChatUsers().then(
|
||||||
|
(users) {
|
||||||
|
_isFetchingUsers = false;
|
||||||
|
_push(
|
||||||
context,
|
context,
|
||||||
NewChatScreen(
|
NewChatScreen(
|
||||||
options: options,
|
options: widget.options,
|
||||||
translations: translations,
|
translations: widget.translations,
|
||||||
onPressCreateChat: (user) {
|
onPressCreateChat: (user) {
|
||||||
_onPressChat(
|
_onPressChat(
|
||||||
context,
|
context,
|
||||||
|
@ -46,21 +58,25 @@ class CommunityChat extends StatelessWidget {
|
||||||
},
|
},
|
||||||
users: users,
|
users: users,
|
||||||
),
|
),
|
||||||
));
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _onPressChat(BuildContext context, ChatModel chat) async {
|
Future<void> _onPressChat(BuildContext context, ChatModel chat) async {
|
||||||
dataProvider.setChat(chat);
|
widget.dataProvider.setChat(chat);
|
||||||
_push(
|
_push(
|
||||||
context,
|
context,
|
||||||
ChatDetailScreen(
|
ChatDetailScreen(
|
||||||
options: options,
|
options: widget.options,
|
||||||
translations: translations,
|
translations: widget.translations,
|
||||||
chat: chat,
|
chat: chat,
|
||||||
chatMessages: dataProvider.getMessagesStream(),
|
chatMessages: widget.dataProvider.getMessagesStream(),
|
||||||
onPressSelectImage: (ChatModel chat) =>
|
onPressSelectImage: (ChatModel chat) =>
|
||||||
_onPressSelectImage(context, chat),
|
_onPressSelectImage(context, chat),
|
||||||
onMessageSubmit: (ChatModel chat, String content) =>
|
onMessageSubmit: (ChatModel chat, String content) =>
|
||||||
dataProvider.sendTextMessage(content),
|
widget.dataProvider.sendTextMessage(content),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -68,31 +84,32 @@ class CommunityChat extends StatelessWidget {
|
||||||
Future<void> _onPressSelectImage(BuildContext context, ChatModel chat) =>
|
Future<void> _onPressSelectImage(BuildContext context, ChatModel chat) =>
|
||||||
showModalBottomSheet<Uint8List?>(
|
showModalBottomSheet<Uint8List?>(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (BuildContext context) => options.imagePickerContainerBuilder(
|
builder: (BuildContext context) =>
|
||||||
|
widget.options.imagePickerContainerBuilder(
|
||||||
ImagePicker(
|
ImagePicker(
|
||||||
customButton: options.closeImagePickerButtonBuilder(
|
customButton: widget.options.closeImagePickerButtonBuilder(
|
||||||
context,
|
context,
|
||||||
() => Navigator.of(context).pop(),
|
() => Navigator.of(context).pop(),
|
||||||
translations,
|
widget.translations,
|
||||||
),
|
),
|
||||||
imagePickerTheme: imagePickerTheme,
|
imagePickerTheme: widget.imagePickerTheme,
|
||||||
imagePickerConfig: imagePickerConfig,
|
imagePickerConfig: widget.imagePickerConfig,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
).then(
|
).then(
|
||||||
(image) {
|
(image) {
|
||||||
if (image != null) {
|
if (image != null) {
|
||||||
return dataProvider.sendImageMessage(image);
|
return widget.dataProvider.sendImageMessage(image);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) => ChatScreen(
|
Widget build(BuildContext context) => ChatScreen(
|
||||||
chats: dataProvider.getChatsStream(),
|
chats: widget.dataProvider.getChatsStream(),
|
||||||
onPressStartChat: () => _onPressStartChat(context),
|
onPressStartChat: () => _onPressStartChat(context),
|
||||||
onPressChat: (chat) => _onPressChat(context, chat),
|
onPressChat: (chat) => _onPressChat(context, chat),
|
||||||
options: options,
|
options: widget.options,
|
||||||
translations: translations,
|
translations: widget.translations,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue