user_detail_page.dart 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/rendering.dart';
  3. import 'package:flutter_easyrefresh/easy_refresh.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:sport/bean/forum.dart';
  6. import 'package:sport/bean/post.dart';
  7. import 'package:sport/bean/post_user.dart';
  8. import 'package:sport/bean/user_friend.dart';
  9. import 'package:sport/bean/user_info.dart';
  10. import 'package:sport/pages/social/block_user_list_page.dart';
  11. import 'package:sport/pages/social/chat_page.dart';
  12. import 'package:sport/pages/social/post_widget.dart';
  13. import 'package:sport/pages/social/private_message_page.dart';
  14. import 'package:sport/provider/lib/provider_widget.dart';
  15. import 'package:sport/provider/lib/view_state_lifecycle.dart';
  16. import 'package:sport/provider/user_detail_model.dart';
  17. import 'package:sport/provider/user_model.dart';
  18. import 'package:sport/router/navigator_util.dart';
  19. import 'package:sport/services/api/inject_api.dart';
  20. import 'package:sport/services/userid.dart';
  21. import 'package:sport/utils/toast.dart';
  22. import 'package:sport/widgets/appbar.dart';
  23. import 'package:sport/widgets/button_primary.dart';
  24. import 'package:sport/widgets/dialog/request_dialog.dart';
  25. import 'package:sport/widgets/error.dart';
  26. import 'package:sport/widgets/image.dart';
  27. import 'package:sport/widgets/loading.dart';
  28. import 'package:sport/widgets/misc.dart';
  29. import 'package:sport/widgets/persistent_header.dart';
  30. import 'package:sport/widgets/popmenu_bg.dart';
  31. import 'package:sport/widgets/space.dart';
  32. class UserDetailPage extends StatefulWidget {
  33. final PostUser user;
  34. final List<UserFriend> userFriends;
  35. UserDetailPage(this.user, {this.userFriends});
  36. @override
  37. State<StatefulWidget> createState() => _PageState();
  38. }
  39. class _PageState extends ViewStateLifecycle<UserDetailPage, UserDetailModel> with InjectApi, UserId {
  40. ScrollController _controller;
  41. double _expandedHeight = 0;
  42. int _brightness = 0;
  43. String _name = "";
  44. String _genderLabel = "他";
  45. static const List<String> _sort = ["时间倒序", "点赞最多", "评论最多"];
  46. String _sortType = _sort[0];
  47. ValueNotifier<UserInfo> _valueNotifierUserInfo;
  48. ValueNotifier<List<Forum>> _valueNotifierForums;
  49. @override
  50. void initState() {
  51. super.initState();
  52. _valueNotifierUserInfo = ValueNotifier(UserInfo.fromJson(widget.user.toJson()));
  53. _valueNotifierForums = ValueNotifier([]);
  54. _controller = ScrollController()
  55. ..addListener(() {
  56. if (_controller.position.pixels >= _expandedHeight - 70) {
  57. if (_brightness == 0) {
  58. setState(() {
  59. _brightness = 1;
  60. });
  61. }
  62. } else {
  63. if (_brightness == 1) {
  64. setState(() {
  65. _brightness = 0;
  66. });
  67. }
  68. }
  69. });
  70. _genderLabel = selfId == widget.user.id ? "我" : "她";
  71. api.getUserInfo(widget.user.id).then((value) {
  72. _valueNotifierUserInfo.value = value.data;
  73. _name = value.data.name;
  74. _genderLabel = selfId == widget.user.id ? "我" : value.data.gender == 1 ? "他" : "她";
  75. });
  76. api.getForumIndex().then((value) {
  77. _valueNotifierForums.value = [Forum(name: "全部")]..addAll(value.results ?? []);
  78. });
  79. }
  80. bool isSelf() {
  81. return selfId == widget.user.id;
  82. }
  83. @override
  84. void dispose() {
  85. super.dispose();
  86. _controller?.dispose();
  87. _valueNotifierUserInfo?.dispose();
  88. }
  89. @override
  90. Widget build(BuildContext context) {
  91. _expandedHeight = MediaQuery.of(context).size.width * (isSelf() ? 455.0 : 604.0) / 750.0 - MediaQuery.of(context).padding.top;
  92. // print(MediaQuery.of(context));
  93. return Scaffold(
  94. backgroundColor: Colors.white,
  95. body: ProviderWidget<UserDetailModel>(
  96. model: model,
  97. onModelReady: (model) => model.initData(),
  98. builder: (_, model, __) {
  99. return EasyRefresh.builder(
  100. controller: model.refreshController,
  101. enableControlFinishRefresh: true,
  102. enableControlFinishLoad: true,
  103. onRefresh: () => model.refresh(),
  104. onLoad: model.isIdle ? () => model.loadMore() : null,
  105. header: buildClassicalHeader(),
  106. footer: buildClassicalFooter(),
  107. builder: (context, physics, header, footer) {
  108. return CustomScrollView(
  109. controller: _controller,
  110. physics: physics,
  111. slivers: <Widget>[
  112. SliverAppBar(
  113. expandedHeight: _expandedHeight,
  114. pinned: true,
  115. elevation: 0,
  116. forceElevated: true,
  117. iconTheme: IconThemeData(color: _brightness == 0 ? Colors.white : Colors.black),
  118. brightness: _brightness == 0 ? Brightness.dark : Brightness.light,
  119. backgroundColor: _brightness == 0 ? Colors.transparent : Colors.white,
  120. leading: IconButton(
  121. icon: Image.asset("lib/assets/img/topbar_return${_brightness == 0 ? "_white" : ""}.png"),
  122. onPressed: () {
  123. Navigator.of(context).pop();
  124. },
  125. ),
  126. title: _brightness == 0
  127. ? Text("")
  128. : Text(
  129. "$_name",
  130. style: titleStyle,
  131. ),
  132. actions: <Widget>[
  133. PopupMenuTheme(
  134. data: PopupMenuThemeData(shape: PopmenuShape(borderRadius: BorderRadius.all(Radius.circular(10.0)))),
  135. child: PopupMenuButton(
  136. icon: _brightness == 0 ? iconMore() : iconMoreGray(),
  137. onSelected: (val) async {
  138. switch (val) {
  139. case 'block':
  140. await request(context, () async {
  141. var resp = await model.api.postForumBlockUser("${_valueNotifierUserInfo.value?.id}");
  142. if (resp?.code == 0) {
  143. ToastUtil.show("屏蔽成功");
  144. }
  145. });
  146. break;
  147. case 'list':
  148. NavigatorUtil.goPage(context, (context) => BlockUserListPage());
  149. break;
  150. }
  151. },
  152. offset: Offset(0, kToolbarHeight / 2 + 15),
  153. itemBuilder: (_) => [
  154. if (isSelf()) menuItemCenter('list', '屏蔽列表'),
  155. if (!isSelf()) menuItemCenter('block', '屏蔽此人'),
  156. ],
  157. ))
  158. ],
  159. flexibleSpace: FlexibleSpaceBar(
  160. collapseMode: CollapseMode.pin,
  161. stretchModes: [],
  162. background: Stack(
  163. alignment: AlignmentDirectional.bottomCenter,
  164. children: <Widget>[
  165. Image.asset(
  166. "lib/assets/img/bg_userprofile_bbs.png",
  167. fit: BoxFit.cover,
  168. width: MediaQuery.of(context).size.width,
  169. ),
  170. Positioned(
  171. left: 0,
  172. right: 0,
  173. bottom: -1,
  174. child: Container(
  175. height: 10,
  176. decoration: BoxDecoration(borderRadius: BorderRadius.vertical(top: Radius.circular(10)), color: Colors.white),
  177. ),
  178. ),
  179. ValueListenableBuilder(
  180. valueListenable: _valueNotifierUserInfo,
  181. builder: (BuildContext context, UserInfo user, Widget child) {
  182. if (user == null) {
  183. return Container();
  184. }
  185. return Container(
  186. child: Column(
  187. mainAxisSize: MainAxisSize.min,
  188. children: <Widget>[
  189. CircleAvatar(
  190. backgroundImage: userAvatarProvider(user.avatar),
  191. radius: 40,
  192. ),
  193. Space(
  194. height: 12,
  195. ),
  196. Text(user.name, style: TextStyle(color: Colors.white, fontSize: 18)),
  197. Space(
  198. height: 6,
  199. ),
  200. Row(
  201. mainAxisSize: MainAxisSize.min,
  202. children: <Widget>[
  203. Image.asset("lib/assets/img/mine_icon_${user.gender == 1 ? "manl" : "girl"}_white.png"),
  204. Space(
  205. width: 4,
  206. ),
  207. Text(
  208. user.gender == 1 ? "男" : "女",
  209. style: TextStyle(color: Colors.white, fontSize: 12),
  210. ),
  211. Space(
  212. width: 10,
  213. ),
  214. Text(
  215. user.age != null ? "${user.age}岁" : "",
  216. style: TextStyle(color: Colors.white, fontSize: 12),
  217. ),
  218. if ((user.districtId ?? 0) != 0)
  219. Padding(
  220. padding: const EdgeInsets.only(left: 10.0),
  221. child: Text(
  222. "${user.province}${user.province == user.city ? '' : user.city}",
  223. style: TextStyle(color: Colors.white, fontSize: 12),
  224. ),
  225. )
  226. ],
  227. ),
  228. if (selfId != widget.user.id)
  229. Padding(
  230. padding: const EdgeInsets.only(top: 20.0),
  231. child: selfId == '${user.id}'
  232. ? Container(height: 35)
  233. : Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
  234. PrimaryButton(
  235. width: 120,
  236. height: 35,
  237. callback: () {
  238. // 先尝试换一下
  239. NavigatorUtil.goPage(context, (context) => ChatPage(user));
  240. // NavigatorUtil.goPage(context, (context) => PrivateMessagePage(user));
  241. },
  242. content: '',
  243. shadow: false,
  244. child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
  245. Image.asset("lib/assets/img/mine_icon_message.png"),
  246. Space(
  247. width: 6,
  248. ),
  249. Text(
  250. "私信",
  251. strutStyle: fixedLine,
  252. style: Theme.of(context).textTheme.subtitle1.copyWith(color: Colors.white),
  253. )
  254. ]),
  255. ),
  256. Space(
  257. width: 12,
  258. ),
  259. user.isFriend()
  260. ? GestureDetector(
  261. child: Container(
  262. width: 120,
  263. height: 35,
  264. alignment: Alignment.center,
  265. child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
  266. Image.asset("lib/assets/img/mine_icon_followed.png"),
  267. Space(
  268. width: 6,
  269. ),
  270. Text(
  271. "已关注",
  272. strutStyle: fixedLine,
  273. style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor),
  274. )
  275. ]),
  276. decoration: BoxDecoration(
  277. borderRadius: BorderRadius.circular(20),
  278. border: Border.all(
  279. color: Theme.of(context).accentColor,
  280. width: .5,
  281. ),
  282. ),
  283. ),
  284. onTap: () async {
  285. await request(context, () async {
  286. var resp = await model.api.userUnFollow(uid: user.id).catchError((onError) {});
  287. if (resp?.code == 0) {
  288. user.followStatus = "none";
  289. widget.userFriends?.firstWhere((element) => element.uid == user.id)?.isFriends = "0";
  290. setState(() {});
  291. ToastUtil.show("取关成功");
  292. }
  293. });
  294. },
  295. )
  296. : PrimaryButton(
  297. width: 120,
  298. height: 35,
  299. callback: () async {
  300. await request(context, () async {
  301. var resp = await model.api.userFollow(uid: user.id).catchError((onError) {});
  302. if (resp?.code == 0) {
  303. user.followStatus = "followed";
  304. var db = widget.userFriends?.firstWhere((element) => element.uid == user.id);
  305. if (db != null) {
  306. db.isFriends = "1";
  307. db.isIgnore = 0;
  308. }
  309. setState(() {});
  310. ToastUtil.show("关注成功");
  311. }
  312. });
  313. },
  314. content: '',
  315. shadow: false,
  316. child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
  317. Image.asset("lib/assets/img/mine_icon_follow.png"),
  318. Space(
  319. width: 6,
  320. ),
  321. Text(
  322. "关注",
  323. strutStyle: fixedLine,
  324. style: Theme.of(context).textTheme.subtitle1.copyWith(color: Colors.white),
  325. )
  326. ]),
  327. ),
  328. ]),
  329. ),
  330. Space(
  331. height: 32,
  332. ),
  333. ],
  334. ),
  335. );
  336. },
  337. )
  338. ],
  339. ),
  340. ),
  341. ),
  342. header,
  343. SliverToBoxAdapter(
  344. child: Container(
  345. color: Colors.white,
  346. child: ValueListenableBuilder<UserInfo>(
  347. valueListenable: _valueNotifierUserInfo,
  348. builder: (_, data, ___) => (data == null || data.achievements == null || data.achievements.isEmpty)
  349. ? Container()
  350. : Column(
  351. children: <Widget>[
  352. Padding(
  353. padding: const EdgeInsets.all(12.0),
  354. child: Stack(
  355. alignment: Alignment.center,
  356. children: <Widget>[
  357. Image.asset(
  358. "lib/assets/img/mine_image_achievement.png",
  359. fit: BoxFit.fitWidth,
  360. width: 240,
  361. ),
  362. Text(
  363. "${widget.user.id == Provider.of<UserModel>(context).user.id.toString() ? '我' : '$_genderLabel'}的成就",
  364. style: Theme.of(context).textTheme.subtitle1,
  365. )
  366. ],
  367. ),
  368. ),
  369. Padding(
  370. padding: const EdgeInsets.all(12.0),
  371. child: achievementGroupWidget(data?.achievements),
  372. ),
  373. Divider()
  374. ],
  375. )),
  376. ),
  377. ),
  378. SliverPersistentHeader(
  379. delegate: PersistentHeader(
  380. min: 50,
  381. max: 50,
  382. child: Container(
  383. color: Colors.white,
  384. padding: EdgeInsets.only(bottom: 5),
  385. child: Row(
  386. children: <Widget>[
  387. Expanded(
  388. child: Padding(
  389. padding: const EdgeInsets.all(12.0),
  390. child: Text(
  391. "$_genderLabel的帖子",
  392. style: Theme.of(context).textTheme.headline3,
  393. ),
  394. ),
  395. ),
  396. ValueListenableBuilder<List<Forum>>(
  397. builder: (_, snapshot, __) {
  398. var list = snapshot;
  399. if (list.isEmpty) return Container();
  400. return PopupMenuButton(
  401. onSelected: (v) {
  402. setState(() {
  403. model.forumBy(v);
  404. });
  405. },
  406. itemBuilder: (BuildContext context) {
  407. return divideMenus(
  408. list.map((e) => menuItemSelected(e, e.name, model.forum == e || model.forum == null && e.name == "全部")).toList());
  409. },
  410. child: Row(
  411. mainAxisSize: MainAxisSize.min,
  412. children: <Widget>[
  413. Text(
  414. model.forum?.name ?? "全部",
  415. style: Theme.of(context).textTheme.bodyText1,
  416. ),
  417. Padding(
  418. padding: const EdgeInsets.fromLTRB(6, 6, 12.0, 6),
  419. child: arrowBottom(),
  420. )
  421. ],
  422. ),
  423. );
  424. },
  425. valueListenable: _valueNotifierForums,
  426. ),
  427. Space(
  428. width: 12,
  429. ),
  430. PopupMenuButton(
  431. onSelected: (v) {
  432. setState(() {
  433. _sortType = v;
  434. model.sortBy(_sortType);
  435. });
  436. },
  437. itemBuilder: (BuildContext context) {
  438. return divideMenus(_sort.map((e) => menuItemSelected(e, e, _sortType == e)).toList());
  439. },
  440. child: Row(
  441. mainAxisSize: MainAxisSize.min,
  442. children: <Widget>[
  443. Text(
  444. _sortType,
  445. style: Theme.of(context).textTheme.bodyText1,
  446. ),
  447. Padding(
  448. padding: const EdgeInsets.fromLTRB(6, 6, 12.0, 6),
  449. child: arrowBottom(),
  450. )
  451. ],
  452. ),
  453. ),
  454. ],
  455. ),
  456. )),
  457. pinned: true,
  458. ),
  459. if (model.isBusy) SliverToBoxAdapter(child: RequestLoadingWidget()),
  460. if (model.isEmpty)
  461. SliverFillRemaining(child: Center(child: RequestErrorWidget(null, msg: "暂无帖子~", assets: RequestErrorWidget.ASSETS_NO_INVITATION))),
  462. if (model.isIdle)
  463. SliverList(
  464. delegate: SliverChildBuilderDelegate(
  465. (context, index) {
  466. Post post = model.list[index];
  467. return PostWidget(
  468. post,
  469. model,
  470. selfId == post.userId,
  471. jump: false,
  472. showForum: true,
  473. showTopButton: true,
  474. callback: () {
  475. model?.initData();
  476. },
  477. );
  478. },
  479. childCount: model.list.length,
  480. ),
  481. ),
  482. ],
  483. );
  484. });
  485. }),
  486. );
  487. }
  488. @override
  489. createModel() => UserDetailModel(widget.user.id.toString());
  490. }