2023-12-29 11:31:18 +13:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-12-29 10:40:09 +13:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
|
|
|
class RoomPage extends StatefulWidget {
|
|
|
|
const RoomPage({super.key, required this.room});
|
|
|
|
|
|
|
|
final Room room;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<RoomPage> createState() => _RoomPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RoomPageState extends State<RoomPage> {
|
|
|
|
Timeline? timeline;
|
2023-12-29 11:31:18 +13:00
|
|
|
final chatController = TextEditingController();
|
|
|
|
StreamSubscription? updateListener;
|
2023-12-29 10:40:09 +13:00
|
|
|
|
|
|
|
void updateTimeline() async {
|
2023-12-29 11:31:18 +13:00
|
|
|
final newTimeline =
|
|
|
|
await widget.room.getTimeline(eventContextId: widget.room.fullyRead);
|
2023-12-29 10:40:09 +13:00
|
|
|
setState(() {
|
|
|
|
timeline = newTimeline;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
updateTimeline();
|
2023-12-29 11:31:18 +13:00
|
|
|
updateListener = widget.room.onUpdate.stream.listen((event) {
|
2023-12-29 10:40:09 +13:00
|
|
|
updateTimeline();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
2023-12-29 11:31:18 +13:00
|
|
|
updateListener?.cancel();
|
2023-12-29 10:40:09 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
void sendMessage() {}
|
|
|
|
|
2023-12-29 17:19:40 +13:00
|
|
|
List<Event>? get messages => timeline?.events
|
|
|
|
.where((element) => element.type == EventTypes.Message)
|
|
|
|
.toList();
|
|
|
|
|
2023-12-29 11:31:18 +13:00
|
|
|
List<Widget> getChildren() {
|
|
|
|
if (timeline == null) return [const CircularProgressIndicator()];
|
|
|
|
return [
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
2023-12-29 17:19:40 +13:00
|
|
|
itemCount: messages?.length,
|
2023-12-29 11:31:18 +13:00
|
|
|
reverse: true,
|
|
|
|
itemBuilder: (context, index) => ListTile(
|
2023-12-29 17:19:40 +13:00
|
|
|
title: Text(messages![index]
|
|
|
|
.senderFromMemoryOrFallback
|
|
|
|
.displayName
|
2023-12-29 17:10:47 +13:00
|
|
|
.toString()),
|
2023-12-29 17:19:40 +13:00
|
|
|
subtitle: Text(messages![index].body),
|
|
|
|
leading:
|
|
|
|
messages![index].senderFromMemoryOrFallback.avatarUrl !=
|
|
|
|
null
|
|
|
|
? CircleAvatar(
|
|
|
|
foregroundImage: NetworkImage(messages![index]
|
|
|
|
.senderFromMemoryOrFallback
|
|
|
|
.avatarUrl!
|
|
|
|
.getThumbnail(widget.room.client,
|
|
|
|
width: 50, height: 50)
|
|
|
|
.toString()))
|
|
|
|
: null,
|
2023-12-29 11:31:18 +13:00
|
|
|
)),
|
|
|
|
),
|
|
|
|
TextFormField(
|
|
|
|
controller: chatController,
|
|
|
|
decoration: const InputDecoration(hintText: 'Message'),
|
|
|
|
onFieldSubmitted: (value) async {
|
|
|
|
print("Sending text event $value to room ${widget.room.id}...");
|
|
|
|
await widget.room.sendTextEvent(value);
|
|
|
|
chatController.text = '';
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-12-29 10:40:09 +13:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
|
|
title: Text(widget.room.getLocalizedDisplayname()),
|
|
|
|
),
|
|
|
|
body: Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: Column(
|
2023-12-29 11:31:18 +13:00
|
|
|
children: getChildren(),
|
2023-12-29 10:40:09 +13:00
|
|
|
)),
|
|
|
|
floatingActionButton: FloatingActionButton(
|
|
|
|
onPressed: sendMessage,
|
|
|
|
tooltip: 'Send message',
|
|
|
|
child: const Icon(Icons.send),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|