-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversation_screen.dart
155 lines (137 loc) · 5.03 KB
/
conversation_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import 'package:flutter/material.dart';
import 'package:heyther/views/home.dart';
import 'package:heyther/services/Database.dart';
import 'package:heyther/widgets/header_widget.dart';
class ConversationScreen extends StatefulWidget{
final String chatroomID;
final String chatUser;
ConversationScreen(this.chatroomID, this.chatUser);
@override
ConversationScreenState createState()=> ConversationScreenState();
}
class ConversationScreenState extends State<ConversationScreen>{
DatabaseMethods databaseMethods= new DatabaseMethods();
TextEditingController MessageController= new TextEditingController();
Stream ChatMessageStream;
@override
void initState(){
databaseMethods.getMessages(widget.chatroomID).then((value){
setState(() {
ChatMessageStream = value;
});
});
super.initState();
}
sendMessage(String message){
Map<String, dynamic> MessageMap={
"Content": message,
"sender": currentUser.username,
"timestamp": DateTime.now().millisecondsSinceEpoch,
};
databaseMethods.addMessage(widget.chatroomID, MessageMap);
MessageController.clear();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: header(context, strTitle: "Chat with ${widget.chatUser}", isAppTitle: false),
body:Container(
color: Colors.black,
child:Stack(
children:[
ChatMessageList(),
Container(
alignment: Alignment.bottomCenter,
child: Container(
decoration: BoxDecoration(
color: Color.fromRGBO(125,125,125,.5),
),
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child:
Row(
children:[
Expanded(
child: TextField(
controller: MessageController,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
hintText: "Message Content",
hintStyle: TextStyle(color: Colors.white54),
border: InputBorder.none
)
)
),
GestureDetector(
onTap: (){
print("message sent");
sendMessage(MessageController.text);
//MessageController.clear();
},
child: Container(
height: 40,
width: 40,
padding: EdgeInsets.all(12),
alignment: Alignment.center,
child: Icon(Icons.send)
),
),
]
),
)
)
]
),
)
);
}
Widget ChatMessageList(){
return StreamBuilder(
stream: ChatMessageStream,
builder: (context,snapshot){
return snapshot.hasData ? ListView.builder(
itemCount: snapshot.data.documents.length,
itemBuilder: (context,index){
print((snapshot.data.docs[index].data()["timestamp"].toString()));
return MessageTile(snapshot.data.docs[index].data()["Content"],
snapshot.data.docs[index].data()["sender"]==currentUser.username,
int.parse(snapshot.data.docs[index].data()["timestamp"].toString()),
widget.chatroomID
);
}
):
Container();
}
);
}
}
class MessageTile extends StatelessWidget{
final String message;
MessageTile(this.message, this.isSender, this.timestamp, this.chatroomid);
final bool isSender;
final int timestamp;
final String chatroomid;
DatabaseMethods databaseMethods= new DatabaseMethods();
@override
Widget build(BuildContext context){
return GestureDetector(
onTap: ()=> isSender? databaseMethods.deleteMessage(timestamp,chatroomid): print("cannot delete other user's messages"),
child: Container(
margin:EdgeInsets.symmetric(vertical:4,horizontal:16),
width: MediaQuery.of(context).size.width,
alignment: isSender? Alignment.centerRight : Alignment.centerLeft,
child: Container(
padding: EdgeInsets.symmetric(vertical:24,horizontal:24),
decoration: BoxDecoration(
color: isSender? Colors.blue : Colors.grey,
borderRadius: isSender? BorderRadius.only(topLeft:Radius.circular(23),topRight: Radius.circular(23),bottomLeft: Radius.circular(23))
:BorderRadius.only(topLeft:Radius.circular(23),topRight: Radius.circular(23),bottomRight: Radius.circular(23))
),
child: Text(
message,
style: TextStyle(color: Colors.white, fontSize: 16,),
)
)
)
);
}
}