SimpleCloudNotifier/flutter/lib/pages/message_view/message_view.dart

71 lines
1.9 KiB
Dart
Raw Normal View History

2024-05-21 23:20:34 +02:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/components/layout/scaffold.dart';
import 'package:simplecloudnotifier/models/message.dart';
import 'package:simplecloudnotifier/state/user_account.dart';
class MessageViewPage extends StatefulWidget {
2024-05-23 17:41:51 +02:00
const MessageViewPage({super.key, required this.message});
2024-05-21 23:20:34 +02:00
2024-05-23 17:41:51 +02:00
final Message message; // Potentially trimmed
2024-05-21 23:20:34 +02:00
@override
State<MessageViewPage> createState() => _MessageViewPageState();
}
class _MessageViewPageState extends State<MessageViewPage> {
late Future<Message>? futureMessage;
@override
void initState() {
super.initState();
futureMessage = fetchMessage();
}
Future<Message> fetchMessage() async {
final acc = Provider.of<UserAccount>(context, listen: false);
2024-05-23 17:41:51 +02:00
return await APIClient.getMessage(acc.auth!, widget.message.messageID);
2024-05-21 23:20:34 +02:00
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return SCNScaffold(
title: 'Message',
2024-05-23 17:41:51 +02:00
showSearch: false,
2024-05-21 23:20:34 +02:00
child: FutureBuilder<Message>(
future: futureMessage,
builder: (context, snapshot) {
if (snapshot.hasData) {
2024-05-23 17:41:51 +02:00
return buildMessageView(snapshot.data!, false);
2024-05-21 23:20:34 +02:00
} else if (snapshot.hasError) {
return Center(child: Text('${snapshot.error}')); //TODO nice error page
2024-05-23 17:41:51 +02:00
} else if (!widget.message.trimmed) {
return buildMessageView(widget.message, true);
} else {
return const Center(child: CircularProgressIndicator());
2024-05-21 23:20:34 +02:00
}
},
),
);
}
2024-05-23 17:41:51 +02:00
Widget buildMessageView(Message message, bool loading) {
2024-05-23 20:05:55 +02:00
//TODO loading true/false indicator
2024-05-23 17:41:51 +02:00
return Center(
child: Column(
children: [
Text(message.title),
Text(message.content ?? ''),
],
),
);
}
2024-05-21 23:20:34 +02:00
}