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