menu_share_bottom.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'dart:ui';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:image_gallery_saver/image_gallery_saver.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. import 'package:permission_handler/permission_handler.dart';
  10. import 'package:provider/provider.dart';
  11. import 'package:sport/application.dart';
  12. import 'package:sport/bean/forum.dart';
  13. import 'package:sport/bean/post.dart';
  14. import 'package:sport/pages/social/post_page.dart';
  15. import 'package:sport/pages/social/post_share_page.dart';
  16. import 'package:sport/pages/social/share_webview.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/api/resp.dart';
  21. import 'package:sport/sharesdk/tencent.dart';
  22. import 'package:sport/sharesdk/wechat.dart';
  23. import 'package:sport/utils/toast.dart';
  24. import 'package:sport/widgets/space.dart';
  25. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  26. import 'package:wechat_kit/wechat_kit.dart';
  27. class MenuShareBottomContent extends StatefulWidget {
  28. final String? url; // 复制的那个链接
  29. final bool hasDownload; // 有些又download 有些没download
  30. final GlobalKey? poster; // 下载海报...
  31. final String shareType; // 什么类型 Img social Link
  32. final Post? post; // social 连带的 post
  33. final String? hash; // 分享 link 连带的hash
  34. final String? file; // 分享 link 连带的hash
  35. final bool app; // 分享 是否显示app内功能
  36. MenuShareBottomContent(this.shareType, {this.post, this.url = "", this.hasDownload = false, this.poster, this.hash, this.file, this.app = true});
  37. @override
  38. State<StatefulWidget> createState() {
  39. return _MenuShareBottomContentState();
  40. }
  41. }
  42. // shareType : Img / Link / Social
  43. class _MenuShareBottomContentState extends State<MenuShareBottomContent> with TencentMixin, WechatMixin, InjectApi {
  44. late List<Map<String, dynamic>> map;
  45. Uint8List? postInstance;
  46. @override
  47. listenWechatShareMsg(ShareMsgResp resp) {
  48. Navigator.pop(context);
  49. ToastUtil.show("分享成功");
  50. }
  51. @override
  52. void initState() {
  53. super.initState();
  54. map = [
  55. {
  56. "value": "微信",
  57. "url": "share_icon_wechat",
  58. "callBack": () async {
  59. if (widget.shareType == "Img") {
  60. String path = await initFile();
  61. wechatShareImage(path, "chat");
  62. } else if (widget.shareType == "Link") {
  63. wechatShareLink(widget.url ?? "" + "?h=${widget.hash}", "chat", Provider.of<UserModel>(context, listen: false).user.name);
  64. } else if (widget.shareType == "Video") {
  65. String path = await initFile();
  66. wechatShareVideo(path, "chat");
  67. }
  68. UmengCommonSdk.onEvent("share_wechat", {});
  69. }
  70. },
  71. {
  72. "value": "朋友圈",
  73. "url": "share_icon_wechatmonents",
  74. "callBack": () async {
  75. if (widget.shareType == "Img") {
  76. String path = await initFile();
  77. wechatShareImage(path, "friend");
  78. } else if (widget.shareType == "Link") {
  79. wechatShareLink(widget.url ?? "" + "?h=${widget.hash}", "friend", Provider.of<UserModel>(context, listen: false).user.name);
  80. }
  81. UmengCommonSdk.onEvent("share_wechatmonents", {});
  82. }
  83. },
  84. {
  85. "value": "QQ",
  86. "url": "share_icon_qq",
  87. "callBack": () async {
  88. if (widget.shareType == "Img") {
  89. String path = await initFile();
  90. tencentShare(path);
  91. } else if (widget.shareType == "Link") {
  92. tencentShareLink(widget.url ?? "");
  93. }
  94. UmengCommonSdk.onEvent("share_tencent", {});
  95. }
  96. },
  97. if(widget.app && openSocial())
  98. {
  99. "value": "社区",
  100. "url": "share_icon_community",
  101. "callBack": () async {
  102. UmengCommonSdk.onEvent("share_community", {});
  103. // if(widget.post == null)
  104. // return;
  105. List<Forum> buttonList = [];
  106. RespList<Forum> data = await api.getForumIndex();
  107. buttonList = data.results;
  108. if (data.results.length > 0) {
  109. buttonList.insert(0, new Forum(gameName: "全部"));
  110. }
  111. if (widget.shareType == "social") {
  112. await NavigatorUtil.goPage(
  113. context,
  114. (contxt) => PostPage(
  115. "",
  116. post: widget.post,
  117. forums: buttonList,
  118. ));
  119. } else if (widget.shareType == "Link") {
  120. await NavigatorUtil.goPage(
  121. context,
  122. (contxt) => PostPage(
  123. "",
  124. post: widget.post,
  125. url: widget.url,
  126. hash: widget.hash,
  127. forums: buttonList,
  128. ));
  129. } else if (widget.shareType == "Img") {
  130. String path = await initFile();
  131. print("$path---------------------------------");
  132. await NavigatorUtil.goPage(
  133. context,
  134. (context) => PostPage(
  135. "",
  136. image: path,
  137. forums: buttonList,
  138. ));
  139. }
  140. Navigator.pop(context, true);
  141. }
  142. },
  143. if(widget.app && openSocial())
  144. {
  145. "value": "社区好友",
  146. "url": "share_icon_friends",
  147. "callBack": () async {
  148. UmengCommonSdk.onEvent("share_friend", {});
  149. if (widget.shareType == "social") {
  150. await NavigatorUtil.goPage(
  151. context,
  152. (context) => PostShareFriendsPage(
  153. post: widget.post,
  154. ));
  155. } else if (widget.shareType == "Link") {
  156. // 这里可能得组装一手 link 的 数据 ...
  157. await NavigatorUtil.goPage(context, (context) => PostShareFriendsPage(hash: widget.hash));
  158. } else if (widget.shareType == "Img") {
  159. String path = await initFile();
  160. String url = (await api.postChatUpload(File(path))).data["url"];
  161. await NavigatorUtil.goPage(
  162. context,
  163. (context) => PostShareFriendsPage(
  164. image: url,
  165. ));
  166. }
  167. Navigator.pop(context, true);
  168. }
  169. },
  170. if (widget.url?.isNotEmpty == true)
  171. {
  172. "value": "复制链接",
  173. "url": "share_icon_link",
  174. "callBack": () {
  175. UmengCommonSdk.onEvent("share_copy", {});
  176. Clipboard.setData(ClipboardData(text: widget.url));
  177. ToastUtil.show("复制成功");
  178. }
  179. },
  180. if (widget.hasDownload == true)
  181. {
  182. "value": "下载图片",
  183. "url": "share_icon_download",
  184. "callBack": () {
  185. UmengCommonSdk.onEvent("share_download", {});
  186. _capture().then((file) async {}).whenComplete(() {
  187. print("Complete-------------------------------------------------");
  188. });
  189. }
  190. },
  191. ];
  192. // 社区暂时先把前面的隐藏掉
  193. if (widget.shareType == "social") {
  194. map = map.sublist(map.length - 2, map.length);
  195. }
  196. }
  197. // 封装成灵活的更通用一点...
  198. Future<String> initFile() async {
  199. if (widget.file?.isNotEmpty == true) return widget.file!;
  200. Uint8List pngBytes = await initFileUint8List();
  201. print("$pngBytes------------------------------");
  202. String sTempDir = (await getTemporaryDirectory()).path;
  203. bool isDirExist = await Directory(sTempDir).exists();
  204. if (!isDirExist) {
  205. Directory(sTempDir).create();
  206. }
  207. // QQ分享需要一个确实存在的图片
  208. File file = await File(sTempDir + "/poster-temp-${DateTime.now().millisecondsSinceEpoch}.png").writeAsBytes(pngBytes);
  209. Future.delayed(new Duration(milliseconds: 200));
  210. print("[file]:${file.readAsBytesSync().length}---------------${pngBytes.length}----------------");
  211. return file.path;
  212. }
  213. Future<Uint8List> initFileUint8List() async {
  214. if(widget.poster == null) return Uint8List.fromList([0]);
  215. try {
  216. RenderObject? renderObject = widget.poster!.currentContext?.findRenderObject();
  217. //boundary.toImage()转化为ui.Image对象,不会自动为包裹的组件添加背景,不设置可能会缺失背景
  218. if(renderObject is RenderRepaintBoundary) {
  219. RenderRepaintBoundary boundary = renderObject;
  220. var image = await boundary.toImage(pixelRatio: window.devicePixelRatio);
  221. //将image转化为byteData
  222. ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
  223. if (byteData == null) return Uint8List.fromList([0]);
  224. //这个对象就是图片数据
  225. Uint8List pngBytes = byteData.buffer.asUint8List();
  226. print("成功生成图片 ----------------------------------------");
  227. return pngBytes;
  228. }
  229. } catch (e) {
  230. print(e);
  231. }
  232. return Uint8List.fromList([0]);
  233. }
  234. Future<File?> _capture() async {
  235. var permission = await Permission.storage.request();
  236. if (!permission.isGranted) {
  237. ToastUtil.show("没有保存文件的权限");
  238. return null;
  239. }
  240. var result ;
  241. if (widget.file?.isNotEmpty == true) {
  242. result = await ImageGallerySaver.saveFile(widget.file!);
  243. }else {
  244. Uint8List pngBytes = await initFileUint8List();
  245. result = await ImageGallerySaver.saveImage(pngBytes);
  246. }
  247. if (result != null && result["isSuccess"] == true) ToastUtil.show("下载成功");
  248. }
  249. @override
  250. Widget build(BuildContext context) {
  251. return Container(
  252. margin: EdgeInsets.fromLTRB(12, 23, 24, 17),
  253. child: Column(
  254. mainAxisSize: MainAxisSize.min,
  255. children: <Widget>[
  256. Row(
  257. children: <Widget>[
  258. Expanded(
  259. child: Divider(
  260. endIndent: 12.0,
  261. ),
  262. ),
  263. Text(
  264. "分享至",
  265. style: Theme.of(context).textTheme.bodyText2!,
  266. ),
  267. Expanded(
  268. child: Divider(
  269. indent: 12.0,
  270. ),
  271. )
  272. ],
  273. ),
  274. SizedBox(
  275. height: 20.0,
  276. ),
  277. Wrap(
  278. children: map
  279. .map((e) => InkWell(
  280. child: FractionallySizedBox(
  281. widthFactor: .25,
  282. child: Container(
  283. padding: EdgeInsets.symmetric(horizontal: 5, vertical: 6),
  284. child: Center(
  285. child: Column(
  286. mainAxisSize: MainAxisSize.min,
  287. crossAxisAlignment: CrossAxisAlignment.center,
  288. children: <Widget>[
  289. Image.asset(
  290. "lib/assets/img/${e["url"]}.png",
  291. width: 44.0,
  292. height: 44.0,
  293. ),
  294. Space(
  295. height: 4.0,
  296. ),
  297. Text(
  298. "${e["value"]}",
  299. style: Theme.of(context).textTheme.subtitle2,
  300. ),
  301. // Space(height: 8.0,),
  302. ],
  303. ),
  304. ),
  305. )),
  306. onTap: () {
  307. e["callBack"]();
  308. },
  309. ))
  310. .toList(),
  311. )
  312. ],
  313. ),
  314. );
  315. }
  316. }
  317. Future<bool?> menuShareBottom(BuildContext context, String shareType, {Post? post, String? url, bool hasDownload = true, GlobalKey? poster, String? hash}) {
  318. return showModalBottomSheet(
  319. context: context,
  320. builder: (context) {
  321. return MenuShareBottomContent(shareType, post: post, url: url, hasDownload: hasDownload, poster: poster, hash: hash);
  322. },
  323. backgroundColor: Colors.white,
  324. elevation: 10,
  325. shape: RoundedRectangleBorder(
  326. borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
  327. ),
  328. );
  329. }