veilidchat/lib/components/chat_component.dart

129 lines
3.6 KiB
Dart
Raw Normal View History

2023-07-29 10:55:35 -04:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:intl/date_symbol_data_local.dart';
import 'package:uuid/uuid.dart';
2023-08-02 21:09:28 -04:00
import '../tools/theme_service.dart';
2023-07-29 10:55:35 -04:00
class ChatComponent extends ConsumerStatefulWidget {
const ChatComponent({super.key});
@override
ChatComponentState createState() => ChatComponentState();
}
class ChatComponentState extends ConsumerState<ChatComponent> {
List<types.Message> _messages = [];
final _unfocusNode = FocusNode();
@override
void initState() {
super.initState();
_loadMessages();
}
@override
void dispose() {
_unfocusNode.dispose();
super.dispose();
}
void _loadMessages() {
final messages = <types.Message>[
types.TextMessage(
id: "abcd",
text: "Hello!",
author: types.User(
id: "1234",
firstName: "Foo",
lastName: "Bar",
role: types.Role.user))
];
_messages = messages;
}
final _user = const types.User(
id: '82091008-a484-4a89-ae75-a22bf8d6f3ac',
);
void _addMessage(types.Message message) {
setState(() {
_messages.insert(0, message);
});
}
void _handleSendPressed(types.PartialText message) {
final textMessage = types.TextMessage(
author: _user,
createdAt: DateTime.now().millisecondsSinceEpoch,
id: const Uuid().v4(),
text: message.text,
);
_addMessage(textMessage);
}
2023-08-02 21:09:28 -04:00
void _handleAttachmentPressed() {
//
}
2023-07-29 10:55:35 -04:00
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
2023-08-02 21:09:28 -04:00
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
final chatTheme = scale.toChatTheme();
final textTheme = Theme.of(context).textTheme;
2023-07-29 10:55:35 -04:00
//
2023-08-02 21:09:28 -04:00
return DefaultTextStyle(
style: textTheme.bodySmall!,
child: Align(
2023-08-05 01:00:46 -04:00
alignment: AlignmentDirectional.centerEnd,
child: Stack(
children: [
Column(
2023-07-29 10:55:35 -04:00
children: [
2023-08-05 01:00:46 -04:00
Container(
height: 48,
decoration: BoxDecoration(
color: scale.primaryScale.subtleBackground,
),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Padding(
padding:
const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 0),
child: Text("current contact",
textAlign: TextAlign.start,
style: textTheme.titleMedium),
2023-07-29 10:55:35 -04:00
),
2023-08-05 01:00:46 -04:00
),
),
Expanded(
child: Container(
decoration: const BoxDecoration(),
child: Chat(
theme: chatTheme,
messages: _messages,
//onAttachmentPressed: _handleAttachmentPressed,
//onMessageTap: _handleMessageTap,
//onPreviewDataFetched: _handlePreviewDataFetched,
2023-08-02 21:09:28 -04:00
2023-08-05 01:00:46 -04:00
onSendPressed: _handleSendPressed,
showUserAvatars: true,
showUserNames: true,
user: _user,
2023-07-29 10:55:35 -04:00
),
2023-08-05 01:00:46 -04:00
),
2023-07-29 10:55:35 -04:00
),
],
),
2023-08-05 01:00:46 -04:00
],
),
));
2023-07-29 10:55:35 -04:00
}
}