wind/lib/thread_screen.dart

194 lines
6.8 KiB
Dart
Raw Permalink Normal View History

2022-04-18 22:23:30 +00:00
import 'package:enough_mail/mime_message.dart';
2022-04-15 02:46:18 +00:00
import 'package:flutter/material.dart';
2022-04-15 04:03:42 +00:00
import 'package:provider/provider.dart';
import 'package:wind/message_item_view.dart';
import 'package:wind/thread_list_view.dart';
import 'package:wind/thread_model.dart';
class ThreadScreenArguments {
final ThreadItem item;
ThreadScreenArguments(this.item);
}
2022-04-15 02:46:18 +00:00
class ThreadScreen extends StatefulWidget {
ThreadScreen({Key? key, required this.threadNumber}) : super(key: key);
late int threadNumber;
2022-04-15 02:46:18 +00:00
@override
State<StatefulWidget> createState() => ThreadScreenState(threadNumber);
2022-04-15 02:46:18 +00:00
}
class ThreadScreenState extends State<ThreadScreen> {
ThreadScreenState(this.threadNumber);
2022-04-18 23:03:38 +00:00
late ThreadModel model;
2022-04-15 04:03:42 +00:00
late int threadNumber;
2022-04-15 02:46:18 +00:00
@override
Widget build(BuildContext context) {
2022-04-18 23:03:38 +00:00
model = Provider.of<ThreadModel>(context, listen: false);
2022-04-15 02:46:18 +00:00
return Scaffold(
appBar: AppBar(
2022-04-18 23:03:38 +00:00
title: Text("Тред #${this.threadNumber}"),
actions: [
TextButton.icon(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Обновление треда...')),
);
model.update();
},
label: const Text("Обновить"),
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary),
icon: Icon(Icons.sync))
],
2022-04-15 02:46:18 +00:00
),
2022-04-15 04:03:42 +00:00
body: Center(
child: Container(
2022-04-18 22:23:30 +00:00
width: 650,
2022-04-18 23:03:38 +00:00
child: Consumer<ThreadModel>(
builder: ((context, value, child) =>
FutureBuilder<List<MessageItem>>(
future: _fetch(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<MessageItem> data = List.from(snapshot.data!);
data.insert(
1, MessageItem("reply", 0, "", "", "", "")); // reply
return _listView(data);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
return Center(child: CircularProgressIndicator());
},
))),
2022-04-15 04:03:42 +00:00
)),
2022-04-15 02:46:18 +00:00
);
}
2022-04-15 04:03:42 +00:00
Future<List<MessageItem>> _fetch(BuildContext context) async {
List<MessageItem> posts = [];
var threadPosts = await model.getThread(threadNumber);
posts.addAll(threadPosts);
var opPost = await model.getPost(threadNumber);
posts.insert(0, opPost);
return posts;
2022-04-15 04:03:42 +00:00
}
Widget _listView(List<MessageItem> data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
if (index == 1) {
2022-04-18 22:23:30 +00:00
return SendMessageForm(opPost: data[0].originalMessage!);
}
2022-04-18 22:23:30 +00:00
return MessageItemView(
item: data[index],
isOpPost: index == 0,
isLast: index == data.length - 1);
2022-04-15 04:03:42 +00:00
});
}
2022-04-15 02:46:18 +00:00
}
class SendMessageForm extends StatefulWidget {
2022-04-18 22:23:30 +00:00
const SendMessageForm({Key? key, required this.opPost}) : super(key: key);
final MimeMessage opPost;
@override
SendMessageFormState createState() {
2022-04-18 22:23:30 +00:00
return SendMessageFormState(opPost);
}
}
// Create a corresponding State class.
// This class holds data related to the form.
class SendMessageFormState extends State<SendMessageForm> {
2022-04-18 22:23:30 +00:00
SendMessageFormState(this.opPost);
final MimeMessage opPost;
// Create a global key that uniquely identifies the Form widget
// and allows validation of the form.
//
// Note: This is a GlobalKey<FormState>,
// not a GlobalKey<MyCustomFormState>.
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
2022-04-18 22:23:30 +00:00
child: Container(
margin: EdgeInsets.only(left: 16, right: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
child: Column(children: [
Consumer<ThreadModel>(
builder: ((context, value, child) => TextFormField(
controller: value.commentTextController,
minLines: 5,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Комментарий"),
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Пожалуйста, введите текст';
}
2022-04-18 22:23:30 +00:00
return null;
},
2022-04-18 22:23:30 +00:00
))),
Consumer<ThreadModel>(
builder: (((context, value, child) => Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
Provider.of<ThreadModel>(context,
listen: false)
.postMessage(opPost,
value.commentTextController.text)
2022-04-18 23:03:38 +00:00
.then((responseCode) {
if (responseCode == 240) {
2022-04-18 22:23:30 +00:00
ScaffoldMessenger.of(context)
.showSnackBar(
const SnackBar(
content: Text('Пост отправлен!')),
);
2022-04-18 23:03:38 +00:00
Provider.of<ThreadModel>(context,
listen: false)
.update();
2022-04-18 22:23:30 +00:00
}
});
value.commentTextController.text = "";
}
},
child: const Text('Отправить'),
)
])))),
)
]),
)
],
),
),
);
}
}