Add ability to send messages

This commit is contained in:
Brandon Presley 2023-12-29 11:31:18 +13:00
parent 705aeb568f
commit c5ae0f0e85

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
@ -12,10 +14,12 @@ class RoomPage extends StatefulWidget {
class _RoomPageState extends State<RoomPage> {
Timeline? timeline;
final chatController = TextEditingController();
StreamSubscription? updateListener;
void updateTimeline() async {
final newTimeline = await widget.room.getTimeline(
eventContextId: widget.room.fullyRead, onUpdate: () => setState(() {}));
final newTimeline =
await widget.room.getTimeline(eventContextId: widget.room.fullyRead);
setState(() {
timeline = newTimeline;
});
@ -25,7 +29,7 @@ class _RoomPageState extends State<RoomPage> {
void initState() {
super.initState();
updateTimeline();
widget.room.onUpdate.stream.listen((event) {
updateListener = widget.room.onUpdate.stream.listen((event) {
updateTimeline();
});
}
@ -33,12 +37,37 @@ class _RoomPageState extends State<RoomPage> {
@override
void dispose() {
super.dispose();
timeline?.cancelSubscriptions();
widget.room.onUpdate.close();
updateListener?.cancel();
}
void sendMessage() {}
List<Widget> getChildren() {
if (timeline == null) return [const CircularProgressIndicator()];
return [
Expanded(
child: ListView.builder(
itemCount: timeline?.events.length,
reverse: true,
itemBuilder: (context, index) => ListTile(
title: Text(timeline!.events[index].senderFromMemoryOrFallback
.displayName ??
""),
subtitle: Text(timeline!.events[index].body),
)),
),
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 = '';
},
),
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -49,23 +78,7 @@ class _RoomPageState extends State<RoomPage> {
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: timeline == null
? [const CircularProgressIndicator()]
: [
Expanded(
child: ListView.builder(
itemCount: timeline?.events.length,
reverse: true,
itemBuilder: (context, index) => ListTile(
title: Text(timeline!
.events[index]
.senderFromMemoryOrFallback
.displayName ??
""),
subtitle: Text(timeline!.events[index].body),
)),
)
],
children: getChildren(),
)),
floatingActionButton: FloatingActionButton(
onPressed: sendMessage,