forked from rustdesk/rustdesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_page.dart
180 lines (172 loc) · 7.14 KB
/
chat_page.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import 'package:dash_chat_2/dash_chat_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/common.dart';
import 'package:flutter_hbb/models/chat_model.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import '../../mobile/pages/home_page.dart';
enum ChatPageType {
mobileMain,
desktopCM,
}
class ChatPage extends StatelessWidget implements PageShape {
late final ChatModel chatModel;
final ChatPageType? type;
ChatPage({ChatModel? chatModel, this.type}) {
this.chatModel = chatModel ?? gFFI.chatModel;
}
@override
final title = translate("Chat");
@override
final icon = unreadTopRightBuilder(gFFI.chatModel.mobileUnreadSum);
@override
final appBarActions = [
PopupMenuButton<MessageKey>(
tooltip: "",
icon: unreadTopRightBuilder(gFFI.chatModel.mobileUnreadSum,
icon: Icon(Icons.group)),
itemBuilder: (context) {
// only mobile need [appBarActions], just bind gFFI.chatModel
final chatModel = gFFI.chatModel;
return chatModel.messages.entries.map((entry) {
final key = entry.key;
final user = entry.value.chatUser;
final client = gFFI.serverModel.clients
.firstWhereOrNull((e) => e.id == key.connId);
final connected =
gFFI.serverModel.clients.any((e) => e.id == key.connId);
return PopupMenuItem<MessageKey>(
child: Row(
children: [
Icon(
key.isOut
? Icons.call_made_rounded
: Icons.call_received_rounded,
color: MyTheme.accent)
.marginOnly(right: 6),
Text("${user.firstName} ${user.id}"),
if (connected)
Container(
width: 10,
height: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color.fromARGB(255, 46, 205, 139)),
).marginSymmetric(horizontal: 2),
if (client != null)
unreadMessageCountBuilder(client.unreadChatMessageCount)
.marginOnly(left: 4)
],
),
value: key,
);
}).toList();
},
onSelected: (key) {
gFFI.chatModel.changeCurrentKey(key);
})
];
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: chatModel,
child: Container(
color: Theme.of(context).scaffoldBackgroundColor,
child: Consumer<ChatModel>(
builder: (context, chatModel, child) {
final readOnly = type == ChatPageType.mobileMain &&
(chatModel.currentKey.connId == ChatModel.clientModeID ||
gFFI.serverModel.clients.every((e) =>
e.id != chatModel.currentKey.connId ||
chatModel.currentUser == null)) ||
type == ChatPageType.desktopCM &&
gFFI.serverModel.clients
.firstWhereOrNull(
(e) => e.id == chatModel.currentKey.connId)
?.disconnected ==
true;
return Stack(
children: [
LayoutBuilder(builder: (context, constraints) {
final chat = DashChat(
onSend: chatModel.send,
currentUser: chatModel.me,
messages: chatModel
.messages[chatModel.currentKey]?.chatMessages ??
[],
readOnly: readOnly,
inputOptions: InputOptions(
focusNode: chatModel.inputNode,
textController: chatModel.textController,
inputTextStyle: TextStyle(
fontSize: 14,
color: Theme.of(context).textTheme.titleLarge?.color),
inputDecoration: InputDecoration(
isDense: true,
hintText: translate('Write a message'),
filled: true,
fillColor: Theme.of(context).colorScheme.background,
contentPadding: EdgeInsets.all(10),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
borderSide: const BorderSide(
width: 1,
style: BorderStyle.solid,
),
),
),
sendButtonBuilder: defaultSendButton(
padding:
EdgeInsets.symmetric(horizontal: 6, vertical: 0),
color: MyTheme.accent,
icon: Icons.send_rounded,
),
),
messageOptions: MessageOptions(
showOtherUsersAvatar: false,
showOtherUsersName: false,
textColor: Colors.white,
maxWidth: constraints.maxWidth * 0.7,
messageTextBuilder: (message, _, __) {
final isOwnMessage = message.user.id.isBlank!;
return Column(
crossAxisAlignment: isOwnMessage
? CrossAxisAlignment.end
: CrossAxisAlignment.start,
children: <Widget>[
Text(message.text,
style: TextStyle(color: Colors.white)),
Text(
"${message.createdAt.hour}:${message.createdAt.minute.toString().padLeft(2, '0')}",
style: TextStyle(
color: Colors.white,
fontSize: 8,
),
).marginOnly(top: 3),
],
);
},
messageDecorationBuilder:
(message, previousMessage, nextMessage) {
final isOwnMessage = message.user.id.isBlank!;
return defaultMessageDecoration(
color:
isOwnMessage ? MyTheme.accent : Colors.blueGrey,
borderTopLeft: 8,
borderTopRight: 8,
borderBottomRight: isOwnMessage ? 2 : 8,
borderBottomLeft: isOwnMessage ? 8 : 2,
);
},
),
);
return SelectionArea(child: chat);
}),
],
).paddingOnly(bottom: 8);
},
),
),
);
}
}