pikapika/lib/screens/CommentScreen.dart

195 lines
5.3 KiB
Dart
Raw Normal View History

2021-09-29 23:57:09 +00:00
import 'package:flutter/material.dart';
2021-11-11 03:00:38 +00:00
import 'package:pikapika/basic/Common.dart';
import 'package:pikapika/basic/Entities.dart';
import 'package:pikapika/basic/Entities.dart' as e;
import 'package:pikapika/basic/Method.dart';
import 'package:pikapika/screens/components/CommentItem.dart';
import 'package:pikapika/screens/components/CommentMainType.dart';
import 'package:pikapika/screens/components/ContentBuilder.dart';
2021-09-29 23:57:09 +00:00
2022-03-19 04:12:27 +00:00
import 'components/RightClickPop.dart';
2021-11-09 21:57:44 +00:00
class _CommentChildPage extends e.Page {
late List<ChildOfComment> docs;
_CommentChildPage.ofComic(CommentChildrenPage commentPage)
: super.of(commentPage.total, commentPage.limit, commentPage.page,
commentPage.pages) {
this.docs = commentPage.docs;
}
_CommentChildPage.ofGame(GameCommentChildrenPage commentPage)
: super.of(commentPage.total, commentPage.limit, commentPage.page,
commentPage.pages) {
this.docs = commentPage.docs;
}
}
2021-09-29 23:57:09 +00:00
class CommentScreen extends StatefulWidget {
2021-11-09 21:57:44 +00:00
final CommentMainType mainType;
final String mainId;
final CommentBase comment;
2021-09-29 23:57:09 +00:00
2022-03-17 03:31:25 +00:00
const CommentScreen(this.mainType, this.mainId, this.comment, {Key? key})
: super(key: key);
2021-09-29 23:57:09 +00:00
@override
State<StatefulWidget> createState() => _CommentScreenState();
}
class _CommentScreenState extends State<CommentScreen> {
late int _currentPage = 1;
2021-11-09 21:57:44 +00:00
late Future<_CommentChildPage> _future = _loadPage();
2021-09-29 23:57:09 +00:00
2021-11-09 21:57:44 +00:00
Future<_CommentChildPage> _loadPage() async {
switch (widget.mainType) {
case CommentMainType.COMIC:
return _CommentChildPage.ofComic(await method.commentChildren(
widget.mainId,
widget.comment.id,
_currentPage,
));
case CommentMainType.GAME:
return _CommentChildPage.ofGame(await method.gameCommentChildren(
widget.mainId,
widget.comment.id,
_currentPage,
));
}
2021-09-29 23:57:09 +00:00
}
Widget _buildChildrenPager() {
return ContentBuilder(
future: _future,
onRefresh: _loadPage,
successBuilder:
2021-11-09 21:57:44 +00:00
(BuildContext context, AsyncSnapshot<_CommentChildPage> snapshot) {
2021-09-29 23:57:09 +00:00
var page = snapshot.data!;
return ListView(
children: [
_buildPrePage(page),
...page.docs.map((e) => _buildComment(e)),
_buildNextPage(page),
2022-01-13 06:19:57 +00:00
_buildPostComment(),
2021-09-29 23:57:09 +00:00
],
);
},
);
}
@override
2022-03-19 04:12:27 +00:00
Widget build(BuildContext context){
return RightClickPop(buildScreen(context));
}
Widget buildScreen(BuildContext context) {
2021-09-29 23:57:09 +00:00
return Scaffold(
appBar: AppBar(
2022-03-19 04:12:27 +00:00
title: const Text('评论'),
2021-09-29 23:57:09 +00:00
),
body: Column(
children: [
2021-11-09 21:57:44 +00:00
ComicCommentItem(widget.mainType, widget.mainId, widget.comment),
2021-09-29 23:57:09 +00:00
Container(
height: 3,
color:
(Theme.of(context).textTheme.bodyText1?.color ?? Colors.black)
.withOpacity(.05),
),
Expanded(child: _buildChildrenPager())
],
),
);
}
2021-11-09 21:57:44 +00:00
Widget _buildComment(CommentBase e) {
return ComicCommentItem(widget.mainType, widget.mainId, e);
2021-09-29 23:57:09 +00:00
}
Widget _buildPostComment() {
return InkWell(
onTap: () async {
String? text = await inputString(context, '请输入评论内容');
if (text != null && text.isNotEmpty) {
try {
2021-11-09 21:57:44 +00:00
switch (widget.mainType) {
case CommentMainType.COMIC:
await method.postChildComment(widget.comment.id, text);
break;
case CommentMainType.GAME:
await method.postGameChildComment(widget.comment.id, text);
break;
}
2021-09-29 23:57:09 +00:00
setState(() {
_future = _loadPage();
widget.comment.commentsCount++;
});
} catch (e) {
defaultToast(context, "评论失败");
}
}
},
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
width: .25,
style: BorderStyle.solid,
color: Colors.grey.shade500.withOpacity(.5),
),
bottom: BorderSide(
width: .25,
style: BorderStyle.solid,
color: Colors.grey.shade500.withOpacity(.5),
),
),
),
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.all(30),
child: const Center(
2021-09-29 23:57:09 +00:00
child: Text('我有话要讲'),
),
),
);
}
2021-11-09 21:57:44 +00:00
Widget _buildPrePage(_CommentChildPage page) {
2021-09-29 23:57:09 +00:00
if (page.page > 1) {
return InkWell(
onTap: () {
setState(() {
_currentPage = page.page - 1;
_future = _loadPage();
});
},
child: Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.all(30),
child: const Center(
2021-09-29 23:57:09 +00:00
child: Text('上一页'),
),
),
);
}
return Container();
}
2021-11-09 21:57:44 +00:00
Widget _buildNextPage(_CommentChildPage page) {
2021-09-29 23:57:09 +00:00
if (page.page < page.pages) {
return InkWell(
onTap: () {
setState(() {
_currentPage = page.page + 1;
_future = _loadPage();
});
},
child: Container(
2022-03-19 04:12:27 +00:00
padding: const EdgeInsets.all(30),
child: const Center(
2021-09-29 23:57:09 +00:00
child: Text('下一页'),
),
),
);
}
return Container();
}
}