post_share_page.dart 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import 'dart:io';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:flutter/material.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/user_friend.dart';
  8. import 'package:sport/pages/social/chat_page.dart';
  9. import 'package:sport/pages/social/post_page.dart';
  10. import 'package:sport/provider/user_model.dart';
  11. import 'package:sport/router/navigator_util.dart';
  12. import 'package:sport/services/api/inject_api.dart';
  13. import 'package:sport/services/api/resp.dart';
  14. import 'package:sport/utils/toast.dart';
  15. import 'package:sport/widgets/appbar.dart';
  16. import 'package:sport/widgets/dialog/alert_dialog.dart';
  17. import 'package:sport/widgets/image.dart';
  18. import 'package:sport/widgets/loading.dart';
  19. import 'package:sport/widgets/space.dart';
  20. // 简单判断吧 post 就是转发 帖子类型 link 就是 链接类型 ...
  21. class PostSharePage extends StatefulWidget {
  22. final Post post;
  23. // link 需要hash 和 url 要分开操作的是...
  24. final String url;
  25. final String hash;
  26. // Img
  27. final String image;
  28. const PostSharePage({this.post, this.url, this.hash, this.image});
  29. @override
  30. State<StatefulWidget> createState() => _PageState();
  31. }
  32. class _PageState extends State<PostSharePage> with InjectApi {
  33. @override
  34. void initState() {
  35. super.initState();
  36. }
  37. @override
  38. void dispose() {
  39. super.dispose();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Scaffold(
  44. backgroundColor: Colors.white,
  45. appBar: AppBar(
  46. leading: buildBackButton(context),
  47. title: Text(
  48. "选择分享到的吧",
  49. style: titleStyle,
  50. ),
  51. centerTitle: false,
  52. titleSpacing: 0,
  53. ),
  54. body: FutureBuilder<RespList<Forum>>(
  55. future: api.getForumIndex(),
  56. builder: (_, snapshot) {
  57. if (snapshot.connectionState != ConnectionState.done)
  58. return RequestLoadingWidget();
  59. var list = snapshot.data?.results ?? [];
  60. return ListView.separated(
  61. itemBuilder: (BuildContext context, int index) => ListTile(
  62. leading: CircleAvatar(
  63. backgroundImage:
  64. CachedNetworkImageProvider(list[index].cover),
  65. radius: 22.0),
  66. title: Text(
  67. "${list[index].name}",
  68. style: Theme.of(context)
  69. .textTheme
  70. .subtitle1
  71. .copyWith(fontSize: 16),
  72. maxLines: 1,
  73. overflow: TextOverflow.ellipsis,
  74. ),
  75. onTap: () async {
  76. bool result = await NavigatorUtil.goPage(
  77. context,
  78. (context) => PostPage(
  79. list[index].forumId,
  80. post: widget.post,
  81. forum: list[index],
  82. url: widget.url,
  83. hash: widget.hash,
  84. image: widget.image,
  85. ));
  86. if (result == true) {
  87. Navigator.of(context).pop(true);
  88. }
  89. },
  90. trailing: arrowRight(),
  91. ),
  92. itemCount: list.length,
  93. separatorBuilder: (BuildContext context, int index) => Divider(),
  94. );
  95. },
  96. ),
  97. );
  98. }
  99. }
  100. class PostShareFriendsPage extends StatefulWidget {
  101. final Post post; // 应该是二选一...
  102. final String hash; // 二选一 ...
  103. final String image;
  104. const PostShareFriendsPage({this.post, this.hash, this.image});
  105. @override
  106. State<StatefulWidget> createState() => _PostShareFriendsPageState();
  107. }
  108. class _PostShareFriendsPageState extends State<PostShareFriendsPage>
  109. with InjectApi {
  110. @override
  111. void initState() {
  112. super.initState();
  113. }
  114. @override
  115. void dispose() {
  116. super.dispose();
  117. }
  118. @override
  119. Widget build(BuildContext context) {
  120. return Scaffold(
  121. backgroundColor: Colors.white,
  122. appBar: AppBar(
  123. leading: buildBackButton(context),
  124. title: Text(
  125. "选择分享到的朋友",
  126. style: titleStyle,
  127. ),
  128. centerTitle: false,
  129. titleSpacing: 0,
  130. ),
  131. body: FutureBuilder<RespList<UserFriend>>(
  132. future: api.userFriends(),
  133. builder: (_, snapshot) {
  134. if (snapshot.connectionState != ConnectionState.done)
  135. return RequestLoadingWidget();
  136. var list = snapshot.data?.results ?? [];
  137. return ListView.separated(
  138. itemBuilder: (BuildContext context, int index) => ListTile(
  139. leading: CircleAvatar(
  140. backgroundImage:
  141. CachedNetworkImageProvider(list[index].socialInfo.avatar),
  142. radius: 22.0),
  143. title: Text(
  144. "${list[index].socialInfo.name}",
  145. style: Theme.of(context)
  146. .textTheme
  147. .subtitle1
  148. .copyWith(fontSize: 16),
  149. maxLines: 1,
  150. overflow: TextOverflow.ellipsis,
  151. ),
  152. onTap: () async {
  153. await showDialog(
  154. context: context,
  155. builder: (context) => CustomAlertDialog(
  156. child: Column(
  157. children: <Widget>[
  158. Space(
  159. height: 37.0,
  160. ),
  161. CircleAvatar(
  162. backgroundImage: CachedNetworkImageProvider(
  163. list[index].socialInfo?.avatar,
  164. ),
  165. radius: 35),
  166. Space(
  167. height: 8.0,
  168. ),
  169. Center(
  170. child: Text(
  171. "${list[index].socialInfo?.name}",
  172. style: Theme.of(context).textTheme.headline3,
  173. ),
  174. ),
  175. Space(
  176. height: 8.0,
  177. ),
  178. _buildPostWidget(
  179. context, widget.post, widget.hash, widget.image)
  180. ],
  181. ),
  182. textOk: "分享",
  183. ok: () async {
  184. bool result = await NavigatorUtil.goPage(
  185. context,
  186. (context) => ChatPage(list[index].socialInfo,
  187. post: widget.post,
  188. hash: widget.hash,
  189. image: widget.image));
  190. if (result == true) {
  191. Navigator.of(context).pop(true);
  192. Navigator.of(context).pop(true);
  193. }
  194. }));
  195. },
  196. trailing: arrowRight(),
  197. ),
  198. itemCount: list.length,
  199. separatorBuilder: (BuildContext context, int index) => Divider(),
  200. );
  201. },
  202. ),
  203. );
  204. }
  205. }
  206. Widget _buildPostWidget(
  207. BuildContext context, Post post, String hash, String image) {
  208. Widget imageWidget(String image) {
  209. return ClipRRect(
  210. child: CachedNetworkImage(
  211. imageUrl: image,
  212. fit: BoxFit.cover,
  213. width: 50,
  214. height: 50,
  215. ),
  216. // 也可控件一边圆角大小
  217. borderRadius: new BorderRadius.all(Radius.circular(6.0)));
  218. }
  219. Widget contentWidget(String content) {
  220. return Expanded(
  221. child: Container(
  222. margin: const EdgeInsets.all(5.0),
  223. child: Text(content,
  224. maxLines: 1,
  225. overflow: TextOverflow.ellipsis,
  226. style: Theme.of(context).textTheme.subtitle1),
  227. ),
  228. );
  229. }
  230. Widget contentMoreWidget() {
  231. return Expanded(
  232. child: Container(
  233. margin: const EdgeInsets.all(5.0),
  234. child: RichText(
  235. text: TextSpan(children: [
  236. TextSpan(
  237. text: Provider.of<UserModel>(context, listen: false).user.name,
  238. style: Theme.of(context)
  239. .textTheme
  240. .headline6
  241. .copyWith(color: Color(0xffffc400))),
  242. TextSpan(
  243. text: "分享了他的运动记录,快来围观吧~",
  244. style: Theme.of(context).textTheme.subtitle1)
  245. ]),
  246. ),
  247. ),
  248. );
  249. }
  250. Widget sharePostWidget() {
  251. print("$image----------------------------");
  252. if (post != null) {
  253. return Row(
  254. children: <Widget>[
  255. if (post?.images != null && post.images.isNotEmpty)
  256. imageWidget(post?.images[0].src),
  257. contentWidget(post.content),
  258. ],
  259. );
  260. } else if (hash != null) {
  261. return Row(
  262. children: <Widget>[
  263. imageWidget(avatarList[4]),
  264. contentMoreWidget(),
  265. ],
  266. );
  267. } else if (image != null) {
  268. return Container(
  269. decoration: BoxDecoration(
  270. image: DecorationImage(
  271. image: FileImage(File(image)),
  272. fit: BoxFit.cover,
  273. )),
  274. constraints: BoxConstraints(maxHeight: 300, maxWidth: 200),
  275. child: CachedNetworkImage(
  276. imageUrl:image,
  277. ),
  278. );
  279. } else {
  280. return Container();
  281. }
  282. }
  283. // file unload asset
  284. return Container(
  285. margin: const EdgeInsets.symmetric(horizontal: 8.0),
  286. padding: const EdgeInsets.symmetric(vertical: 7.0, horizontal: 8.0),
  287. decoration: new BoxDecoration(
  288. color: Color(0xfff1f1f1), // 底色
  289. borderRadius: new BorderRadius.all(Radius.circular(10.0)),
  290. ),
  291. child: sharePostWidget());
  292. }