pikapika/lib/screens/components/CommentList.dart

181 lines
4.9 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/screens/CommentScreen.dart';
import 'package:pikapika/screens/components/ItemBuilder.dart';
import 'package:pikapika/basic/Method.dart';
2023-01-31 10:50:51 +00:00
import '../../basic/config/IconLoading.dart';
2021-11-09 21:57:44 +00:00
import 'CommentItem.dart';
import 'CommentMainType.dart';
class _CommentBasePage extends e.Page {
late List<CommentBase> docs;
_CommentBasePage.ofComic(CommentPage commentPage)
: super.of(commentPage.total, commentPage.limit, commentPage.page,
commentPage.pages) {
this.docs = commentPage.docs;
}
_CommentBasePage.ofGame(GameCommentPage commentPage)
: super.of(commentPage.total, commentPage.limit, commentPage.page,
commentPage.pages) {
this.docs = commentPage.docs;
}
}
2021-09-29 23:57:09 +00:00
// 漫画的评论列表
2021-11-09 21:57:44 +00:00
class CommentList extends StatefulWidget {
final CommentMainType mainType;
final String mainId;
2021-09-29 23:57:09 +00:00
2022-03-25 14:57:30 +00:00
const CommentList(this.mainType, this.mainId, {Key? key}) : super(key: key);
2021-09-29 23:57:09 +00:00
@override
2021-11-09 21:57:44 +00:00
State<StatefulWidget> createState() => _CommentListState();
2021-09-29 23:57:09 +00:00
}
2021-11-09 21:57:44 +00:00
class _CommentListState extends State<CommentList> {
2021-09-29 23:57:09 +00:00
late int _currentPage = 1;
2021-11-09 21:57:44 +00:00
late Future<_CommentBasePage> _future = _loadPage();
2021-09-29 23:57:09 +00:00
2021-11-09 21:57:44 +00:00
Future<_CommentBasePage> _loadPage() async {
switch (widget.mainType) {
case CommentMainType.COMIC:
return _CommentBasePage.ofComic(
await method.comments(widget.mainId, _currentPage),
);
case CommentMainType.GAME:
return _CommentBasePage.ofGame(
await method.gameComments(widget.mainId, _currentPage),
);
}
2021-09-29 23:57:09 +00:00
}
@override
Widget build(BuildContext context) {
return ItemBuilder(
future: _future,
successBuilder:
2021-11-09 21:57:44 +00:00
(BuildContext context, AsyncSnapshot<_CommentBasePage> snapshot) {
2021-09-29 23:57:09 +00:00
var page = snapshot.data!;
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
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
],
);
},
onRefresh: () async => {
setState(() {
_future = _loadPage();
})
},
);
}
2021-11-09 21:57:44 +00:00
Widget _buildComment(CommentBase comment) {
2021-09-29 23:57:09 +00:00
return InkWell(
onTap: () {
Navigator.of(context).push(
2023-01-31 10:50:51 +00:00
mixRoute(
2021-11-09 21:57:44 +00:00
builder: (context) =>
CommentScreen(widget.mainType, widget.mainId, comment),
2021-09-29 23:57:09 +00:00
),
);
},
2021-11-09 21:57:44 +00:00
child: ComicCommentItem(widget.mainType, widget.mainId, comment),
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.postComment(widget.mainId, text);
break;
case CommentMainType.GAME:
await method.postGameComment(widget.mainId, text);
break;
}
2021-09-29 23:57:09 +00:00
setState(() {
_future = _loadPage();
});
} 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),
2022-03-25 14:57:30 +00:00
child: const Center(
2021-09-29 23:57:09 +00:00
child: Text('我有话要讲'),
),
),
);
}
2021-11-09 21:57:44 +00:00
Widget _buildPrePage(_CommentBasePage 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),
2022-03-25 14:57:30 +00:00
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(_CommentBasePage 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),
2022-03-25 14:57:30 +00:00
child: const Center(
2021-09-29 23:57:09 +00:00
child: Text('下一页'),
),
),
);
}
return Container();
}
}