post_share_page.dart 9.5 KB

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