262 lines
9.7 KiB
Dart
262 lines
9.7 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:prosappco/src/authentication/authentication_repository.dart';
|
|
import 'package:prosappco/src/components/photo_view.dart';
|
|
import 'package:prosappco/src/components/pop_appbar.dart';
|
|
import 'package:prosappco/src/models/chat_model.dart';
|
|
import 'package:prosappco/src/models/event_model.dart';
|
|
import 'package:prosappco/src/models/user_model.dart';
|
|
|
|
class ChatScreen extends StatefulWidget {
|
|
final String? eventoId;
|
|
const ChatScreen({super.key, this.eventoId});
|
|
|
|
@override
|
|
State<ChatScreen> createState() => _ChatScreenState();
|
|
}
|
|
|
|
class _ChatScreenState extends State<ChatScreen> {
|
|
final _textController = TextEditingController();
|
|
final uid = AuthenticationRepository.instance.getCurrentUserUid();
|
|
UserModel? user;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
Event.getEventById(widget.eventoId!).then((event) {
|
|
if (user == null) {
|
|
if (uid != event.userId) {
|
|
UserModel.getUser(event.userId).then(
|
|
(UserModel s) => setState(() => user = s),
|
|
);
|
|
} else {
|
|
UserModel.getUser(event.professionalId).then(
|
|
(UserModel s) => setState(() => user = s),
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: PopAppbar(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
label: 'Chat'),
|
|
body: Column(
|
|
children: [
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.grey.withOpacity(0.3),
|
|
spreadRadius: 2,
|
|
blurRadius: 3,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
color: const Color(0xFFD6F4FF),
|
|
alignment: Alignment.topCenter,
|
|
child: ListTile(
|
|
leading: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
child: ReferencePhoto(
|
|
ref: user?.photo,
|
|
size: 55,
|
|
sizeCircle: 60,
|
|
),
|
|
),
|
|
title: Text(
|
|
'${user?.name}',
|
|
style: const TextStyle(
|
|
color: Colors.black, fontWeight: FontWeight.w600),
|
|
),
|
|
subtitle: Text(user?.profession ?? ''),
|
|
trailing: const Icon(Icons.keyboard_arrow_right),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.only(top: 10),
|
|
reverse: true,
|
|
child: streamB(uid!),
|
|
),
|
|
),
|
|
Container(
|
|
alignment: Alignment.bottomCenter,
|
|
width: MediaQuery.of(context).size.width,
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
|
|
width: MediaQuery.of(context).size.width,
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextFormField(
|
|
controller: _textController,
|
|
style: const TextStyle(color: Colors.black),
|
|
decoration: InputDecoration(
|
|
hintText: 'Mensaje',
|
|
hintStyle:
|
|
TextStyle(color: Colors.grey[600], fontSize: 16),
|
|
border: OutlineInputBorder(
|
|
borderSide: const BorderSide(
|
|
color: Colors.grey, width: 1.0),
|
|
borderRadius: BorderRadius.circular(50)),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderSide: const BorderSide(
|
|
color: Colors.grey, width: 1.0),
|
|
borderRadius: BorderRadius.circular(50)),
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
horizontal: 20, vertical: 15),
|
|
filled: true,
|
|
fillColor: Colors.grey[200],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
GestureDetector(
|
|
onTap: () async {
|
|
final nuevoMensaje = MessageModel(
|
|
user: uid!,
|
|
content: _textController.text,
|
|
timestamp: DateTime.now());
|
|
|
|
final nuevoMensajeMap = {
|
|
'user': nuevoMensaje.user,
|
|
'content': nuevoMensaje.content,
|
|
'timestamp': nuevoMensaje.timestamp,
|
|
};
|
|
|
|
FirebaseFirestore.instance
|
|
.collection('chats')
|
|
.doc(widget.eventoId)
|
|
.update({
|
|
'message': FieldValue.arrayUnion([nuevoMensajeMap])
|
|
});
|
|
|
|
_textController.clear();
|
|
},
|
|
child: Container(
|
|
height: 50,
|
|
width: 50,
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).primaryColor,
|
|
borderRadius: BorderRadius.circular(30),
|
|
),
|
|
child: const Center(
|
|
child: Icon(
|
|
Icons.send,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
StreamBuilder<DocumentSnapshot<Map<String, dynamic>>> streamB(String uid) {
|
|
return StreamBuilder(
|
|
stream: FirebaseFirestore.instance
|
|
.collection('chats')
|
|
.doc(widget.eventoId)
|
|
.snapshots(),
|
|
builder: (context, snapshot) {
|
|
if (!snapshot.hasData) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
final data = snapshot.data!;
|
|
final chat = ChatModel.fromDocumentSnapshot(data);
|
|
|
|
return Column(children: [
|
|
...chat.messages.map(
|
|
(e) => uid != e.user
|
|
? ListTile(
|
|
title: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
margin: const EdgeInsets.only(right: 60),
|
|
padding: const EdgeInsets.symmetric(
|
|
vertical: 10, horizontal: 16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey.shade200,
|
|
borderRadius: const BorderRadius.only(
|
|
topRight: Radius.circular(20),
|
|
bottomLeft: Radius.circular(20),
|
|
bottomRight: Radius.circular(20),
|
|
),
|
|
),
|
|
child: Text(
|
|
e.content,
|
|
style: const TextStyle(fontSize: 16),
|
|
),
|
|
),
|
|
const SizedBox(width: 5),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
child: Text(
|
|
DateFormat('h:mm a').format(e.timestamp),
|
|
style: const TextStyle(
|
|
color: Colors.grey, fontSize: 12),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: ListTile(
|
|
title: Column(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Container(
|
|
margin: const EdgeInsets.only(left: 60),
|
|
padding: const EdgeInsets.symmetric(
|
|
vertical: 10, horizontal: 16),
|
|
decoration: const BoxDecoration(
|
|
color: Color(0xFFD5EFFF),
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(20),
|
|
bottomLeft: Radius.circular(20),
|
|
bottomRight: Radius.circular(20),
|
|
),
|
|
),
|
|
child: Text(
|
|
e.content,
|
|
style: const TextStyle(fontSize: 16),
|
|
),
|
|
),
|
|
const SizedBox(width: 5),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
child: Text(
|
|
DateFormat('h:mm a').format(e.timestamp),
|
|
style: const TextStyle(
|
|
color: Colors.grey, fontSize: 12),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
)
|
|
]);
|
|
},
|
|
);
|
|
}
|
|
}
|