mirror of
https://github.com/ChronosX88/wind.git
synced 2024-11-09 09:11:00 +00:00
Prototype send message form, fix bug with route
This commit is contained in:
parent
650461a883
commit
755679c90f
@ -36,7 +36,27 @@ class MyApp extends StatelessWidget {
|
|||||||
primarySwatch: Colors.indigo,
|
primarySwatch: Colors.indigo,
|
||||||
),
|
),
|
||||||
home: MyHomePage(title: 'Wind'),
|
home: MyHomePage(title: 'Wind'),
|
||||||
routes: {'/thread': (context) => ThreadScreen()},
|
onGenerateRoute: (settings) {
|
||||||
|
Widget? pageView;
|
||||||
|
if (settings.name != null) {
|
||||||
|
var uriData = Uri.parse(settings.name!);
|
||||||
|
|
||||||
|
switch (uriData.path) {
|
||||||
|
case '/thread':
|
||||||
|
pageView = ThreadScreen(
|
||||||
|
threadNumber:
|
||||||
|
int.parse(uriData.queryParametersAll['num']!.first));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
pageView = MyHomePage(title: 'Wind');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pageView != null) {
|
||||||
|
return MaterialPageRoute(
|
||||||
|
settings: settings, builder: (BuildContext context) => pageView!);
|
||||||
|
}
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,56 +13,55 @@ class MessageItemView extends StatelessWidget {
|
|||||||
margin: this.isOpPost ? EdgeInsets.only(bottom: 10) : EdgeInsets.all(0),
|
margin: this.isOpPost ? EdgeInsets.only(bottom: 10) : EdgeInsets.all(0),
|
||||||
child: Card(
|
child: Card(
|
||||||
elevation: 5,
|
elevation: 5,
|
||||||
child: InkWell(
|
child: Column(
|
||||||
splashColor: Colors.indigo.withAlpha(30),
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
onTap: () => {},
|
mainAxisSize: MainAxisSize.min,
|
||||||
child: Column(
|
children: [
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
isOpPost
|
||||||
mainAxisSize: MainAxisSize.min,
|
? Container(
|
||||||
children: [
|
child: Text(
|
||||||
isOpPost
|
item.subject!,
|
||||||
? Container(
|
|
||||||
child: Text(
|
|
||||||
item.subject!,
|
|
||||||
style: TextStyle(
|
|
||||||
fontWeight: FontWeight.bold, fontSize: 21),
|
|
||||||
),
|
|
||||||
margin: EdgeInsets.only(top: 16, left: 16, right: 16),
|
|
||||||
)
|
|
||||||
: Container(),
|
|
||||||
Container(
|
|
||||||
child: Row(
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
item.author,
|
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontWeight: FontWeight.normal,
|
fontWeight: FontWeight.bold, fontSize: 21),
|
||||||
color: Colors.blue,
|
|
||||||
fontSize: 15),
|
|
||||||
),
|
),
|
||||||
SizedBox(width: 5),
|
margin: EdgeInsets.only(top: 16, left: 16, right: 16),
|
||||||
Text(
|
)
|
||||||
item.date,
|
: Container(),
|
||||||
style: TextStyle(fontSize: 15),
|
Container(
|
||||||
),
|
child: Row(
|
||||||
SizedBox(width: 5),
|
children: [
|
||||||
Text(
|
Text(
|
||||||
|
item.author,
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.normal,
|
||||||
|
color: Colors.blue,
|
||||||
|
fontSize: 15),
|
||||||
|
),
|
||||||
|
SizedBox(width: 5),
|
||||||
|
Text(
|
||||||
|
item.date,
|
||||||
|
style: TextStyle(fontSize: 15),
|
||||||
|
),
|
||||||
|
SizedBox(width: 5),
|
||||||
|
InkWell(
|
||||||
|
child: Text(
|
||||||
"#${item.number}",
|
"#${item.number}",
|
||||||
style: TextStyle(fontSize: 15, color: Colors.grey),
|
style: TextStyle(fontSize: 15, color: Colors.grey),
|
||||||
)
|
),
|
||||||
],
|
onTap: () => {},
|
||||||
),
|
)
|
||||||
margin: isOpPost
|
],
|
||||||
? EdgeInsets.only(
|
|
||||||
top: 5, bottom: 2, left: 16, right: 16)
|
|
||||||
: EdgeInsets.only(top: 16, left: 16),
|
|
||||||
),
|
),
|
||||||
Container(
|
margin: isOpPost
|
||||||
child: Text(item.body, style: TextStyle(fontSize: 17)),
|
? EdgeInsets.only(top: 5, bottom: 2, left: 16, right: 16)
|
||||||
margin: EdgeInsets.all(16),
|
: EdgeInsets.only(top: 16, left: 16),
|
||||||
)
|
),
|
||||||
],
|
Container(
|
||||||
),
|
child:
|
||||||
|
SelectableText(item.body, style: TextStyle(fontSize: 17)),
|
||||||
|
margin: EdgeInsets.all(16),
|
||||||
|
)
|
||||||
|
],
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,14 +127,18 @@ class NNTPClient {
|
|||||||
return threads;
|
return threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Tuple2<int, MimeMessage>>> getThread(
|
Future<MimeMessage> getPost(int postNumber) async {
|
||||||
int threadNumber) async {
|
var resp = await _sendCommand("ARTICLE", [postNumber.toString()]);
|
||||||
|
resp.lines.removeLast();
|
||||||
|
return MimeMessage.parseFromText(resp.lines.join("\r\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Tuple2<int, MimeMessage>>> getThread(int threadNumber) async {
|
||||||
if (currentGroup == null) throw new ArgumentError("current group is null");
|
if (currentGroup == null) throw new ArgumentError("current group is null");
|
||||||
|
|
||||||
List<Tuple2<int, MimeMessage>> threads = [];
|
List<Tuple2<int, MimeMessage>> threads = [];
|
||||||
|
|
||||||
var newThreadList = await _sendCommand(
|
var newThreadList = await _sendCommand("THREAD", [threadNumber.toString()]);
|
||||||
"THREAD", [threadNumber.toString()]);
|
|
||||||
|
|
||||||
newThreadList.lines.removeAt(0);
|
newThreadList.lines.removeAt(0);
|
||||||
newThreadList.lines.removeLast(); // remove dot
|
newThreadList.lines.removeLast(); // remove dot
|
||||||
|
@ -100,8 +100,8 @@ class ThreadListItemView extends StatelessWidget {
|
|||||||
elevation: 5,
|
elevation: 5,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
splashColor: Colors.indigo.withAlpha(30),
|
splashColor: Colors.indigo.withAlpha(30),
|
||||||
onTap: () => Navigator.pushNamed(context, "/thread",
|
onTap: () =>
|
||||||
arguments: ThreadScreenArguments(item)),
|
Navigator.pushNamed(context, "/thread?num=${item.number}"),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
@ -5,6 +5,24 @@ import 'package:wind/nntp_client.dart';
|
|||||||
class ThreadModel extends ChangeNotifier {
|
class ThreadModel extends ChangeNotifier {
|
||||||
NNTPClient? client;
|
NNTPClient? client;
|
||||||
|
|
||||||
|
String _commentText = "";
|
||||||
|
String get commentText => _commentText;
|
||||||
|
set commentText(String text) {
|
||||||
|
_commentText = text;
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<MessageItem> getPost(int number) async {
|
||||||
|
var msg = await client!.getPost(number);
|
||||||
|
return MessageItem(
|
||||||
|
msg.getHeaderValue("Message-Id")!,
|
||||||
|
number,
|
||||||
|
msg.getHeaderValue("Subject")!,
|
||||||
|
msg.getHeaderValue("From")!,
|
||||||
|
msg.getHeaderValue("Date")!,
|
||||||
|
msg.decodeTextPlainPart()!);
|
||||||
|
}
|
||||||
|
|
||||||
Future<List<MessageItem>> getThread(int threadNumber) async {
|
Future<List<MessageItem>> getThread(int threadNumber) async {
|
||||||
if (client!.currentGroup == "") return [];
|
if (client!.currentGroup == "") return [];
|
||||||
|
|
||||||
|
@ -12,24 +12,27 @@ class ThreadScreenArguments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ThreadScreen extends StatefulWidget {
|
class ThreadScreen extends StatefulWidget {
|
||||||
|
ThreadScreen({Key? key, required this.threadNumber}) : super(key: key);
|
||||||
|
|
||||||
|
late int threadNumber;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<StatefulWidget> createState() => ThreadScreenState();
|
State<StatefulWidget> createState() => ThreadScreenState(threadNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
class ThreadScreenState extends State<ThreadScreen> {
|
class ThreadScreenState extends State<ThreadScreen> {
|
||||||
|
ThreadScreenState(this.threadNumber);
|
||||||
|
|
||||||
late NNTPClient client;
|
late NNTPClient client;
|
||||||
late int threadNumber;
|
late int threadNumber;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var args =
|
|
||||||
ModalRoute.of(context)!.settings.arguments as ThreadScreenArguments;
|
|
||||||
client = context.read<NNTPClient>();
|
client = context.read<NNTPClient>();
|
||||||
threadNumber = args.item.number;
|
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text("Thread #${args.item.number}"),
|
title: Text("Thread #${this.threadNumber}"),
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
@ -39,10 +42,7 @@ class ThreadScreenState extends State<ThreadScreen> {
|
|||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
List<MessageItem> data = List.from(snapshot.data!);
|
List<MessageItem> data = List.from(snapshot.data!);
|
||||||
data.insert(
|
data.insert(1, MessageItem("reply", 0, "", "", "", "")); // reply
|
||||||
0,
|
|
||||||
MessageItem(args.item.id, args.item.number, args.item.subject,
|
|
||||||
args.item.author, args.item.date, args.item.body));
|
|
||||||
return _listView(data);
|
return _listView(data);
|
||||||
} else if (snapshot.hasError) {
|
} else if (snapshot.hasError) {
|
||||||
return Text("${snapshot.error}");
|
return Text("${snapshot.error}");
|
||||||
@ -56,14 +56,95 @@ class ThreadScreenState extends State<ThreadScreen> {
|
|||||||
|
|
||||||
Future<List<MessageItem>> _fetch(BuildContext context) async {
|
Future<List<MessageItem>> _fetch(BuildContext context) async {
|
||||||
var model = context.read<ThreadModel>();
|
var model = context.read<ThreadModel>();
|
||||||
return await model.getThread(threadNumber);
|
List<MessageItem> posts = [];
|
||||||
|
|
||||||
|
var threadPosts = await model.getThread(threadNumber);
|
||||||
|
posts.addAll(threadPosts);
|
||||||
|
var opPost = await model.getPost(threadNumber);
|
||||||
|
posts.insert(0, opPost);
|
||||||
|
|
||||||
|
return posts;
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _listView(List<MessageItem> data) {
|
Widget _listView(List<MessageItem> data) {
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
itemCount: data.length,
|
itemCount: data.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
|
if (index == 1) {
|
||||||
|
return SendMessageForm();
|
||||||
|
}
|
||||||
return MessageItemView(item: data[index], isOpPost: index == 0);
|
return MessageItemView(item: data[index], isOpPost: index == 0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class SendMessageForm extends StatefulWidget {
|
||||||
|
const SendMessageForm({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@override
|
||||||
|
SendMessageFormState createState() {
|
||||||
|
return SendMessageFormState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a corresponding State class.
|
||||||
|
// This class holds data related to the form.
|
||||||
|
class SendMessageFormState extends State<SendMessageForm> {
|
||||||
|
// 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,
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Center(
|
||||||
|
child: Column(children: [
|
||||||
|
TextFormField(
|
||||||
|
minLines: 5,
|
||||||
|
keyboardType: TextInputType.multiline,
|
||||||
|
maxLines: null,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: OutlineInputBorder(), labelText: "Comment"),
|
||||||
|
// The validator receives the text that the user has entered.
|
||||||
|
validator: (value) {
|
||||||
|
if (value == null || value.isEmpty) {
|
||||||
|
return 'Please enter some text';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
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()) {
|
||||||
|
// If the form is valid, display a snackbar. In the real world,
|
||||||
|
// you'd often call a server or save the information in a database.
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text('Processing Data')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: const Text('Send'),
|
||||||
|
)
|
||||||
|
]))
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user