user_post_page.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/rendering.dart';
  3. import 'package:flutter_easyrefresh/easy_refresh.dart';
  4. import 'package:sport/bean/forum.dart';
  5. import 'package:sport/bean/post.dart';
  6. import 'package:sport/bean/post_user.dart';
  7. import 'package:sport/bean/user_friend.dart';
  8. import 'package:sport/pages/social/post_widget.dart';
  9. import 'package:sport/provider/lib/provider_widget.dart';
  10. import 'package:sport/provider/lib/view_state_lifecycle.dart';
  11. import 'package:sport/provider/user_detail_model.dart';
  12. import 'package:sport/services/api/inject_api.dart';
  13. import 'package:sport/services/userid.dart';
  14. import 'package:sport/widgets/appbar.dart';
  15. import 'package:sport/widgets/error.dart';
  16. import 'package:sport/widgets/image.dart';
  17. import 'package:sport/widgets/loading.dart';
  18. import 'package:sport/widgets/misc.dart';
  19. import 'package:sport/widgets/space.dart';
  20. class UserPostPage extends StatefulWidget {
  21. final PostUser user;
  22. final List<UserFriend>? userFriends;
  23. UserPostPage(this.user, {this.userFriends});
  24. @override
  25. State<StatefulWidget> createState() => _PageState();
  26. }
  27. class _PageState extends ViewStateLifecycle<UserPostPage, UserDetailModel> with InjectApi, UserId {
  28. static const List<String> _sort = ["时间倒序", "点赞最多", "评论最多"];
  29. String _sortType = _sort[0];
  30. late ValueNotifier<List<Forum>> _valueNotifierForums;
  31. @override
  32. void initState() {
  33. super.initState();
  34. _valueNotifierForums = ValueNotifier([]);
  35. api.getForumIndex().then((value) {
  36. _valueNotifierForums.value = [Forum(name: "全部")]..addAll(value.results);
  37. });
  38. }
  39. bool isSelf() {
  40. return selfId == widget.user.id;
  41. }
  42. @override
  43. void dispose() {
  44. super.dispose();
  45. }
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. backgroundColor: Colors.white,
  50. appBar: buildAppBar(context,
  51. title: "${selfId == widget.user.id ? "我" : widget.user.gender == 1 ? "他" : "她"}的帖子",
  52. actions: [
  53. ValueListenableBuilder<List<Forum>>(
  54. builder: (_, snapshot, __) {
  55. var list = snapshot;
  56. if (list.isEmpty) return Container();
  57. return PopupMenuButton(
  58. onSelected: (v) {
  59. setState(() {
  60. if (v is Forum) model.forumBy(v);
  61. });
  62. },
  63. itemBuilder: (BuildContext context) {
  64. return divideMenus(list.map((e) => menuItemSelected(e, e.gameName ?? "全部", model.forum == e || model.forum == null && e.gameName == "全部")).toList());
  65. },
  66. child: Row(
  67. mainAxisSize: MainAxisSize.min,
  68. children: <Widget>[
  69. Text(
  70. model.forum?.gameName ?? "全部",
  71. style: Theme.of(context).textTheme.bodyText1!,
  72. ),
  73. Padding(
  74. padding: const EdgeInsets.fromLTRB(6, 6, 12.0, 6),
  75. child: arrowBottom(),
  76. )
  77. ],
  78. ),
  79. );
  80. },
  81. valueListenable: _valueNotifierForums,
  82. ),
  83. Space(
  84. width: 12,
  85. ),
  86. PopupMenuButton(
  87. onSelected: (v) {
  88. setState(() {
  89. if (v is String) {
  90. _sortType = v;
  91. model.sortBy(_sortType);
  92. }
  93. });
  94. },
  95. itemBuilder: (BuildContext context) {
  96. return divideMenus(_sort.map((e) => menuItemSelected(e, e, _sortType == e)).toList());
  97. },
  98. child: Row(
  99. mainAxisSize: MainAxisSize.min,
  100. children: <Widget>[
  101. Text(
  102. _sortType,
  103. style: Theme.of(context).textTheme.bodyText1!,
  104. ),
  105. Padding(
  106. padding: const EdgeInsets.fromLTRB(6, 6, 12.0, 6),
  107. child: arrowBottom(),
  108. )
  109. ],
  110. ),
  111. )
  112. ]),
  113. body: ProviderWidget<UserDetailModel>(
  114. model: model,
  115. onModelReady: (model) => model.initData(),
  116. builder: (_, model, __) {
  117. return EasyRefresh.builder(
  118. controller: model.refreshController,
  119. enableControlFinishRefresh: true,
  120. enableControlFinishLoad: true,
  121. onRefresh: () => model.refresh(),
  122. onLoad: model.isIdle ? () => model.loadMore() : null,
  123. header: buildClassicalHeader(),
  124. footer: buildClassicalFooter(),
  125. builder: (context, physics, header, footer) {
  126. return CustomScrollView(
  127. physics: physics,
  128. slivers: <Widget>[
  129. if (header != null) header,
  130. if (model.isBusy) SliverToBoxAdapter(child: RequestLoadingWidget()),
  131. if (model.isEmpty) SliverToBoxAdapter(child: Center(child: RequestErrorWidget(null, msg: "暂无帖子~", assets: RequestErrorWidget.ASSETS_NO_INVITATION))),
  132. if (model.isIdle)
  133. SliverList(
  134. delegate: SliverChildBuilderDelegate(
  135. (context, index) {
  136. Post post = model.list[index];
  137. return PostWidget(
  138. post,
  139. model,
  140. selfId == post.userId,
  141. jump: false,
  142. showForum: true,
  143. showTopButton: true,
  144. callback: () {
  145. model.initData();
  146. },
  147. );
  148. // return Text("A");
  149. },
  150. childCount: model.list.length,
  151. ),
  152. ),
  153. if (model.isIdle && model.list.isNotEmpty)
  154. SliverToBoxAdapter(
  155. child: Center(
  156. child: Padding(
  157. padding: const EdgeInsets.all(32.0),
  158. child: Text("没有更多数据了"),
  159. )),
  160. ),
  161. ],
  162. );
  163. });
  164. }),
  165. );
  166. }
  167. @override
  168. createModel() => UserDetailModel(widget.user.id.toString());
  169. }