rustdesk/flutter/lib/common/widgets/chat_page.dart

175 lines
7.3 KiB
Dart
Raw Normal View History

2022-08-04 17:24:02 +08:00
import 'package:dash_chat_2/dash_chat_2.dart';
2023-05-20 05:21:13 +08:00
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/common.dart';
import 'package:flutter_hbb/models/chat_model.dart';
2023-05-20 05:21:13 +08:00
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import '../../mobile/pages/home_page.dart';
class ChatPage extends StatelessWidget implements PageShape {
2022-08-11 10:19:12 +08:00
late final ChatModel chatModel;
ChatPage({ChatModel? chatModel}) {
this.chatModel = chatModel ?? gFFI.chatModel;
}
@override
2022-03-23 15:28:21 +08:00
final title = translate("Chat");
@override
final icon = Icon(Icons.chat);
@override
2022-03-25 16:34:27 +08:00
final appBarActions = [
PopupMenuButton<int>(
2022-04-05 00:51:47 +08:00
icon: Icon(Icons.group),
2022-03-25 16:34:27 +08:00
itemBuilder: (context) {
2022-08-11 10:19:12 +08:00
// only mobile need [appBarActions], just bind gFFI.chatModel
final chatModel = gFFI.chatModel;
2022-03-25 16:34:27 +08:00
return chatModel.messages.entries.map((entry) {
final id = entry.key;
2022-05-16 00:01:27 +08:00
final user = entry.value.chatUser;
2022-03-25 16:34:27 +08:00
return PopupMenuItem<int>(
2022-08-04 17:24:02 +08:00
child: Text("${user.firstName} ${user.id}"),
2022-03-25 16:34:27 +08:00
value: id,
);
}).toList();
},
onSelected: (id) {
gFFI.chatModel.changeCurrentID(id);
2022-03-25 16:34:27 +08:00
})
];
@override
Widget build(BuildContext context) {
2022-03-25 16:34:27 +08:00
return ChangeNotifierProvider.value(
2023-05-20 05:21:13 +08:00
value: chatModel,
child: Container(
color: Theme.of(context).scaffoldBackgroundColor,
padding: EdgeInsets.only(top: 14.0, bottom: 14.0, left: 14.0),
2023-05-20 05:21:13 +08:00
child: Consumer<ChatModel>(
builder: (context, chatModel, child) {
final currentUser = chatModel.currentUser;
return Container(
padding: EdgeInsets.symmetric(vertical: 5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Theme.of(context).colorScheme.background,
),
child: Stack(
children: [
LayoutBuilder(builder: (context, constraints) {
final chat = DashChat(
onSend: (chatMsg) {
chatModel.send(chatMsg);
chatModel.inputNode.requestFocus();
},
currentUser: chatModel.me,
messages: chatModel
.messages[chatModel.currentID]?.chatMessages ??
[],
inputOptions: InputOptions(
2023-05-20 05:21:13 +08:00
sendOnEnter: true,
focusNode: chatModel.inputNode,
inputTextStyle: TextStyle(
fontSize: 14,
color:
Theme.of(context).textTheme.titleLarge?.color),
inputDecoration: isDesktop
? 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,
2022-10-14 22:50:13 +08:00
),
2023-05-20 05:21:13 +08:00
),
)
: defaultInputDecoration(
hintText: translate('Write a message'),
fillColor:
Theme.of(context).colorScheme.background,
),
sendButtonBuilder: defaultSendButton(
padding:
EdgeInsets.symmetric(horizontal: 6, vertical: 0),
2023-05-20 21:12:52 +08:00
color: MyTheme.accent,
2023-05-20 05:21:13 +08:00
icon: FluentIcons.send_24_filled,
),
),
messageOptions: MessageOptions(
showOtherUsersAvatar: false,
2023-05-20 21:12:52 +08:00
showOtherUsersName: false,
textColor: Colors.white,
maxWidth: constraints.maxWidth * 0.7,
2023-02-02 19:05:57 +08:00
messageTextBuilder: (message, _, __) {
final isOwnMessage =
message.user.id == currentUser.id;
return Column(
crossAxisAlignment: isOwnMessage
2023-05-20 05:21:13 +08:00
? CrossAxisAlignment.start
: CrossAxisAlignment.end,
2023-02-02 19:05:57 +08:00
children: <Widget>[
Text(message.text,
style: TextStyle(color: Colors.white)),
2023-05-20 05:21:13 +08:00
Text(
2023-05-20 21:12:52 +08:00
"${message.createdAt.hour}:${message.createdAt.minute.toString().padLeft(2, '0')}",
2023-05-20 05:21:13 +08:00
style: TextStyle(
color: Colors.white,
fontSize: 8,
2023-02-02 19:05:57 +08:00
),
2023-05-20 05:21:13 +08:00
).marginOnly(top: 3),
2023-02-02 19:05:57 +08:00
],
);
},
2023-05-20 05:21:13 +08:00
messageDecorationBuilder: (message, __, ___) {
final isOwnMessage =
message.user.id == currentUser.id;
return defaultMessageDecoration(
color: isOwnMessage
2023-05-20 21:12:52 +08:00
? Colors.blueGrey
: MyTheme.accent,
2023-05-20 05:21:13 +08:00
borderTopLeft: 8,
borderTopRight: 8,
borderBottomRight: isOwnMessage ? 8 : 2,
borderBottomLeft: isOwnMessage ? 2 : 8,
);
}),
);
return SelectionArea(child: chat);
}),
desktopType == DesktopType.cm ||
chatModel.currentID == ChatModel.clientModeID
? SizedBox.shrink()
: Padding(
2022-04-05 00:51:47 +08:00
padding: EdgeInsets.all(12),
child: Row(
children: [
Icon(Icons.account_circle,
color: MyTheme.accent80),
SizedBox(width: 5),
Text(
2022-08-04 17:24:02 +08:00
"${currentUser.firstName} ${currentUser.id}",
2023-05-20 21:12:52 +08:00
style: TextStyle(color: MyTheme.accent),
2022-04-05 00:51:47 +08:00
),
],
2023-05-20 05:21:13 +08:00
),
),
],
2023-05-20 05:21:13 +08:00
),
);
},
),
),
);
}
}