novel_detail_scene.dart 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:shuqi/public.dart';
  4. import 'novel_detail_header.dart';
  5. import 'novel_summary_view.dart';
  6. import 'novel_detail_toolbar.dart';
  7. import 'novel_detail_recommend_view.dart';
  8. import 'novel_detail_cell.dart';
  9. import 'novel_comment_cell.dart';
  10. class NovelDetailScene extends StatefulWidget {
  11. final String novelId;
  12. NovelDetailScene(this.novelId);
  13. @override
  14. NovelDetailSceneState createState() => NovelDetailSceneState();
  15. }
  16. class NovelDetailSceneState extends State<NovelDetailScene> with RouteAware {
  17. Novel? novel;
  18. List<Novel> recommendNovels = [];
  19. List<NovelComment> comments = [];
  20. ScrollController scrollController = ScrollController();
  21. double navAlpha = 0;
  22. bool isSummaryUnfold = false;
  23. int commentCount = 0;
  24. int commentMemberCount = 0;
  25. @override
  26. void initState() {
  27. super.initState();
  28. fetchData();
  29. scrollController.addListener(() {
  30. var offset = scrollController.offset;
  31. if (offset < 0) {
  32. if (navAlpha != 0) {
  33. setState(() {
  34. navAlpha = 0;
  35. });
  36. }
  37. } else if (offset < 50) {
  38. setState(() {
  39. navAlpha = 1 - (50 - offset) / 50;
  40. });
  41. } else if (navAlpha != 1) {
  42. setState(() {
  43. navAlpha = 1;
  44. });
  45. }
  46. });
  47. }
  48. @override
  49. void dispose() {
  50. scrollController.dispose();
  51. super.dispose();
  52. }
  53. changeSummaryMaxLines() {
  54. setState(() {
  55. isSummaryUnfold = !isSummaryUnfold;
  56. });
  57. }
  58. back() {
  59. Navigator.pop(context);
  60. }
  61. fetchData() async {
  62. // try {
  63. var novelId = this.widget.novelId;
  64. var novelResponse = await Request.post(action: 'novel_detail', params: {'id': novelId});
  65. var commentsResponse = await Request.post(action: 'novel_comment', params: {'id': novelId});
  66. List<NovelComment> comments = [];
  67. commentsResponse.forEach((data) {
  68. comments.add(NovelComment.fromJson(data));
  69. });
  70. var recommendResponse = await Request.post(action: 'novel_recommend', params: {'id': novelId});
  71. List<Novel> recommendNovels = [];
  72. recommendResponse.forEach((data) {
  73. recommendNovels.add(Novel.fromJson(data));
  74. });
  75. setState(() {
  76. this.novel = Novel.fromJson(novelResponse);
  77. this.comments = comments;
  78. this.recommendNovels = recommendNovels;
  79. });
  80. // } catch (e) {
  81. // Toast.show(e.toString());
  82. // }
  83. }
  84. Widget buildNavigationBar() {
  85. return Stack(
  86. children: <Widget>[
  87. Container(
  88. width: 44,
  89. height: Screen.navigationBarHeight,
  90. padding: EdgeInsets.fromLTRB(5, Screen.topSafeHeight, 0, 0),
  91. child: GestureDetector(
  92. onTap: back, child: Image.asset('assets/img/pub_back_white.png')),
  93. ),
  94. Opacity(
  95. opacity: navAlpha,
  96. child: Container(
  97. decoration: BoxDecoration(color: SQColor.white, boxShadow: Styles.borderShadow),
  98. padding: EdgeInsets.fromLTRB(5, Screen.topSafeHeight, 0, 0),
  99. height: Screen.navigationBarHeight,
  100. child: Row(
  101. children: <Widget>[
  102. Container(
  103. width: 44,
  104. child: GestureDetector(
  105. onTap: back,
  106. child: Image.asset('assets/img/pub_back_gray.png')),
  107. ),
  108. Expanded(
  109. child: Text(
  110. novel!.name,
  111. style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
  112. textAlign: TextAlign.center,
  113. ),
  114. ),
  115. Container(width: 44),
  116. ],
  117. ),
  118. ),
  119. )
  120. ],
  121. );
  122. }
  123. Widget buildComment() {
  124. return Container(
  125. color: Colors.white,
  126. child: Column(
  127. crossAxisAlignment: CrossAxisAlignment.start,
  128. children: <Widget>[
  129. Container(
  130. padding: EdgeInsets.symmetric(vertical: 15),
  131. child: Row(
  132. children: <Widget>[
  133. Image.asset('assets/img/home_tip.png'),
  134. SizedBox(width: 13),
  135. Text('书友评价', style: TextStyle(fontSize: 16)),
  136. Expanded(child: Container()),
  137. Image.asset('assets/img/detail_write_comment.png'),
  138. Text(' 写书评', style: TextStyle(fontSize: 14, color: SQColor.primary)),
  139. SizedBox(width: 15),
  140. ],
  141. ),
  142. ),
  143. Divider(height: 1),
  144. Column(
  145. children: comments.map((comment) => NovelCommentCell(comment)).toList(),
  146. ),
  147. Divider(height: 1),
  148. Container(
  149. padding: EdgeInsets.symmetric(vertical: 15),
  150. child: Center(
  151. child: Text(
  152. '查看全部评论(${novel!.commentCount}条)',
  153. style: TextStyle(fontSize: 14, color: SQColor.gray),
  154. ),
  155. ),
  156. )
  157. ],
  158. ),
  159. );
  160. }
  161. Widget buildTags() {
  162. var colors = [Color(0xFFF9A19F), Color(0xFF59DDB9), Color(0xFF7EB3E7)];
  163. var i = 0;
  164. var tagWidgets = novel!.tags.map((tag) {
  165. var color = colors[i % 3];
  166. var tagWidget = Container(
  167. decoration: BoxDecoration(
  168. border: Border.all(color: Color.fromARGB(99, color.red, color.green, color.blue), width: 0.5),
  169. borderRadius: BorderRadius.circular(3),
  170. ),
  171. padding: EdgeInsets.fromLTRB(6, 3, 6, 3),
  172. child: Text(tag, style: TextStyle(fontSize: 14, color: colors[i % 3])),
  173. );
  174. i++;
  175. return tagWidget;
  176. }).toList();
  177. return Container(
  178. padding: EdgeInsets.fromLTRB(15, 15, 15, 15),
  179. color: SQColor.white,
  180. child: Wrap(runSpacing: 10, spacing: 10, children: tagWidgets),
  181. );
  182. }
  183. @override
  184. Widget build(BuildContext context) {
  185. if (this.novel == null) {
  186. return Scaffold(appBar: AppBar(elevation: 0));
  187. }
  188. var novel = this.novel!;
  189. return Scaffold(
  190. body: AnnotatedRegion(
  191. value: navAlpha > 0.5 ? SystemUiOverlayStyle.dark : SystemUiOverlayStyle.light,
  192. child: Stack(
  193. children: <Widget>[
  194. Column(
  195. children: <Widget>[
  196. Expanded(
  197. child: ListView(
  198. controller: scrollController,
  199. padding: EdgeInsets.only(top: 0),
  200. children: <Widget>[
  201. NovelDetailHeader(novel),
  202. NovelSummaryView(novel.introduction, isSummaryUnfold, changeSummaryMaxLines),
  203. NovelDetailCell(
  204. iconName: 'assets/img/detail_latest.png',
  205. title: '最新',
  206. subtitle: novel.lastChapter.title,
  207. attachedWidget: Text(novel.status, style: TextStyle(fontSize: 14, color: novel.statusColor())),
  208. ),
  209. NovelDetailCell(
  210. iconName: 'assets/img/detail_chapter.png',
  211. title: '目录',
  212. subtitle: '共${novel.chapterCount}章',
  213. ),
  214. buildTags(),
  215. SizedBox(height: 10),
  216. buildComment(),
  217. SizedBox(height: 10),
  218. NovelDetailRecommendView(recommendNovels),
  219. ],
  220. ),
  221. ),
  222. NovelDetailToolbar(novel),
  223. ],
  224. ),
  225. buildNavigationBar(),
  226. ],
  227. ),
  228. ),
  229. );
  230. }
  231. }