// SPDX-FileCopyrightText: 2022 Iconica // // SPDX-License-Identifier: BSD-3-Clause library flutter_community_chat; import 'dart:typed_data'; import 'package:flutter/material.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'; class CommunityChat extends StatefulWidget { const CommunityChat({ required this.dataProvider, this.options = const ChatOptions(), this.translations = const ChatTranslations(), this.imagePickerTheme = const ImagePickerTheme(), this.imagePickerConfig = const ImagePickerConfig(), super.key, }); final CommunityChatInterface dataProvider; final ChatOptions options; final ChatTranslations translations; final ImagePickerTheme imagePickerTheme; final ImagePickerConfig imagePickerConfig; @override State createState() => _CommunityChatState(); } class _CommunityChatState extends State { bool _isFetchingUsers = false; Future _push(BuildContext context, Widget widget) => Navigator.of(context).push( MaterialPageRoute(builder: (context) => widget), ); Future _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), ); }, users: users, ), ); }, ); } } Future _onPressChat(BuildContext context, ChatModel chat) async { widget.dataProvider.setChat(chat); _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), ), ); } Future _onPressSelectImage(BuildContext context, ChatModel chat) => showModalBottomSheet( 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) { if (image != null) { return widget.dataProvider.sendImageMessage(image); } }, ); @override Widget build(BuildContext context) => ChatScreen( chats: widget.dataProvider.getChatsStream(), onPressStartChat: () => _onPressStartChat(context), onPressChat: (chat) => _onPressChat(context, chat), options: widget.options, translations: widget.translations, ); }