基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

share_alert.dart 13 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:ui';
  5. import 'package:cached_network_image/cached_network_image.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:fluttertoast/fluttertoast.dart';
  8. import 'package:more_picture_share/more_picture_share.dart';
  9. import 'package:path_provider/path_provider.dart';
  10. import 'package:permission_handler/permission_handler.dart';
  11. import 'package:share_extend/share_extend.dart';
  12. import 'package:sharesdk_plugin/sharesdk_plugin.dart';
  13. import 'package:zhiying_base_widget/dialog/loading/loading.dart';
  14. import 'package:zhiying_base_widget/utils/image_download_util/image_download_util.dart';
  15. import 'package:zhiying_base_widget/widgets/share/models/share_alert_model.dart';
  16. import 'package:zhiying_base_widget/widgets/share/models/share_data_model.dart';
  17. import 'package:zhiying_base_widget/widgets/share/models/share_icon_model.dart';
  18. import 'package:zhiying_base_widget/widgets/share/share_alert_content.dart';
  19. import 'package:zhiying_comm/zhiying_comm.dart';
  20. import 'dart:io';
  21. class ShareAlert extends StatefulWidget {
  22. final String skipIdentifier;
  23. final bool isContentShow;
  24. final ShareDataModel model;
  25. const ShareAlert(this.model, this.skipIdentifier, {Key key, this.isContentShow = false}) : super(key: key); // 中间视图
  26. @override
  27. _ShareAlertState createState() => _ShareAlertState();
  28. }
  29. class _ShareAlertState extends State<ShareAlert> {
  30. ShareAlertModel _iconModel;
  31. @override
  32. void initState() {
  33. NetUtil.request('/api/v1/mod/${widget.skipIdentifier}', method: NetMethod.GET, onCache: (data) {
  34. // try{
  35. // _parseData(data);
  36. // }catch(e){
  37. // print(e);
  38. // }
  39. }, onSuccess: (data) {
  40. print(data);
  41. _parseData(data);
  42. }, onError: (err) {});
  43. super.initState();
  44. }
  45. void _parseData(Map<String, dynamic> data) {
  46. List modList = data['mod_list'];
  47. Map d = modList.first;
  48. if (d != null) {
  49. String dString = d['data'];
  50. _iconModel = ShareAlertModel.fromJson(Map<String, dynamic>.from(jsonDecode(dString)));
  51. setState(() {});
  52. }
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. return WillPopScope(
  57. onWillPop: () async {
  58. Loading.dismiss();
  59. Navigator.canPop(context);
  60. return true;
  61. },
  62. child: GestureDetector(
  63. child: Scaffold(
  64. backgroundColor: Colors.transparent,
  65. body: BackdropFilter(
  66. filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), //背景
  67. child: Container(
  68. child: Column(
  69. children: <Widget>[
  70. Expanded(
  71. child: Center(child: widget.isContentShow ? ShareAlertContent(_iconModel) : Container()),
  72. ),
  73. _ShareAlertContent(widget.model, widget.skipIdentifier, _iconModel),
  74. ],
  75. ),
  76. ), // 模糊化
  77. ),
  78. ),
  79. onTap: () {
  80. Navigator.of(context).pop();
  81. },
  82. ),
  83. );
  84. }
  85. }
  86. class _ShareAlertContent extends StatefulWidget {
  87. final ShareDataModel model;
  88. final String skipIdentifier;
  89. final ShareAlertModel iconModel;
  90. const _ShareAlertContent(this.model, this.skipIdentifier, this.iconModel, {Key key}) : super(key: key);
  91. @override
  92. _ShareAlertContentState createState() => _ShareAlertContentState();
  93. }
  94. class _ShareAlertContentState extends State<_ShareAlertContent> {
  95. @override
  96. void initState() {
  97. Future.delayed(Duration(milliseconds: 100), () async {
  98. var status = await Permission.storage.status;
  99. if (status != PermissionStatus.granted) {
  100. status = await Permission.storage.request();
  101. }
  102. });
  103. super.initState();
  104. }
  105. @override
  106. Widget build(BuildContext context) {
  107. return GestureDetector(
  108. onTap: () {},
  109. child: Container(
  110. width: double.infinity,
  111. decoration: BoxDecoration(
  112. color: Colors.white,
  113. borderRadius: BorderRadius.only(
  114. topLeft: Radius.circular(12),
  115. topRight: Radius.circular(12),
  116. ),
  117. ),
  118. child: SafeArea(
  119. top: false,
  120. child: Column(
  121. children: <Widget>[
  122. Container(
  123. margin: EdgeInsets.only(top: 8, bottom: 8),
  124. width: 62,
  125. height: 4,
  126. decoration: BoxDecoration(color: Color(0xffd8d8d8), borderRadius: BorderRadius.circular(2)),
  127. ),
  128. Text(
  129. '分享至',
  130. style: TextStyle(fontSize: 15, color: Color(0xff333333), fontWeight: FontWeight.bold),
  131. ),
  132. Container(
  133. margin: EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
  134. child: _createIcons(),
  135. ),
  136. GestureDetector(
  137. child: Container(
  138. margin: EdgeInsets.only(left: 12, right: 12, bottom: 10),
  139. padding: EdgeInsets.all(12),
  140. decoration: BoxDecoration(color: Color(0xfff3f3f3), borderRadius: BorderRadius.circular(8)),
  141. child: Center(
  142. child: Text(
  143. '取消',
  144. style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Color(0xff999999)),
  145. ),
  146. ),
  147. ),
  148. onTap: () {
  149. Navigator.of(context).pop();
  150. },
  151. )
  152. ],
  153. ),
  154. ),
  155. ),
  156. );
  157. }
  158. Widget _createIcons() {
  159. return Wrap(
  160. spacing: 10,
  161. runSpacing: 10,
  162. children: widget.iconModel?.icons?.map((item) {
  163. return _createIcon(item);
  164. })?.toList() ??
  165. [],
  166. );
  167. }
  168. Widget _createIcon(ShareIconModel item) {
  169. return GestureDetector(
  170. child: Container(
  171. width: 60,
  172. child: Column(
  173. children: <Widget>[
  174. Container(
  175. width: 40,
  176. height: 40,
  177. child: CachedNetworkImage(
  178. imageUrl: item.icon,
  179. fit: BoxFit.contain,
  180. ),
  181. ),
  182. Padding(
  183. padding: const EdgeInsets.only(top: 2, bottom: 2),
  184. child: Text(
  185. item.name,
  186. style: TextStyle(fontSize: 12, color: Color(0xff333333), fontWeight: FontWeight.bold),
  187. ),
  188. ),
  189. ],
  190. ),
  191. ),
  192. onTap: () async {
  193. //检查是否有存储权限
  194. var status = await Permission.storage.status;
  195. if (!status.isGranted) {
  196. status = await Permission.storage.request();
  197. print(status);
  198. return;
  199. }
  200. int count = 0;
  201. if (widget.model.poster != null) {
  202. count++;
  203. }
  204. count += (widget.model?.image?.length ?? 0);
  205. // 多图分享
  206. if (count > 1) {
  207. _shareMultipleImages(item.type);
  208. return;
  209. }
  210. if (widget?.model?.poster != null) {
  211. Loading.show(context);
  212. File file = await EncodeUtil.compressImage(context, images: widget?.model?.poster, size: 600);
  213. widget?.model?.poster = await file.readAsBytes();
  214. Loading.dismiss();
  215. }
  216. if (item.type == 'wx') {
  217. _shareByMob(ShareSDKPlatforms.wechatSession);
  218. } else if (item.type == 'pyq') {
  219. _shareByMob(ShareSDKPlatforms.wechatTimeline);
  220. } else if (item.type == 'qq') {
  221. _shareByMob(ShareSDKPlatforms.qq);
  222. } else if (item.type == 'qq_space') {
  223. _shareByMob(ShareSDKPlatforms.qZone);
  224. } else if (item.type == 'weibo') {
  225. _shareByMob(ShareSDKPlatforms.sina);
  226. } else if (item.type == 'more_setting') {
  227. _shareBySystem(item.type);
  228. }
  229. },
  230. );
  231. }
  232. // mob分享,只能单图分享,多图分享调用系统分享
  233. void _shareByMob(ShareSDKPlatform plateform) async {
  234. Loading.show(context);
  235. Timer(Duration(milliseconds: 2000), () {
  236. Loading.dismiss();
  237. });
  238. SSDKMap params;
  239. if (widget.model.poster != null) {
  240. String path = await _savePoster();
  241. if (path != null && path != '') {
  242. params = SSDKMap()
  243. ..setGeneral(
  244. widget.model?.title ?? '',
  245. widget.model?.content ?? '',
  246. Platform.isIOS ? path : null,
  247. null,
  248. Platform.isAndroid ? path : null,
  249. null,
  250. null,
  251. null,
  252. null,
  253. null,
  254. SSDKContentTypes.image,
  255. );
  256. }
  257. } else {
  258. var type = SSDKContentTypes.auto;
  259. if ((widget?.model?.image?.length ?? 0) != 0 && widget.model?.url != null) {
  260. type = SSDKContentTypes.webpage;
  261. } else if ((widget?.model?.image?.length ?? 0) != 0) {
  262. type = SSDKContentTypes.image;
  263. } else if (widget?.model?.title != null || widget.model?.content != null) {
  264. type = SSDKContentTypes.text;
  265. }
  266. if (plateform == ShareSDKPlatforms.qZone && widget?.model?.content != null) {
  267. widget?.model?.title = null;
  268. widget?.model?.image=[];
  269. widget?.model?.url=null;
  270. type = SSDKContentTypes.message;
  271. }
  272. var isExistImage = false;
  273. if (widget?.model?.image != null && widget?.model?.image?.length > 0) {
  274. isExistImage = true;
  275. }
  276. if (plateform == ShareSDKPlatforms.qq && widget?.model?.url != null) {
  277. SSDKMap params = SSDKMap()
  278. ..setQQ(
  279. widget?.model?.content ?? "",
  280. widget?.model?.title ?? "",
  281. null,
  282. null,
  283. null,
  284. null,
  285. null,
  286. null,
  287. null,
  288. (isExistImage ? widget?.model?.image?.first : null),
  289. null,
  290. widget?.model?.content ?? "",
  291. null,
  292. null,
  293. SSDKContentTypes.image,
  294. ShareSDKPlatforms.qq,
  295. );
  296. SharesdkPlugin.share(ShareSDKPlatforms.qq, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {});
  297. return;
  298. }
  299. params = SSDKMap()
  300. ..setGeneral(
  301. widget.model?.title ?? '',
  302. widget?.model?.content ?? '',
  303. Platform.isIOS ? widget.model.image : null,
  304. Platform.isAndroid ? (isExistImage ? widget?.model?.image?.first : null) : null,
  305. null,
  306. widget.model.url,
  307. null,
  308. null,
  309. null,
  310. null,
  311. type,
  312. );
  313. }
  314. SharesdkPlugin.share(plateform, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {
  315. print(error);
  316. if (state == SSDKResponseState.Fail) {
  317. Fluttertoast.showToast(msg: '分享失败');
  318. } else if (state == SSDKResponseState.Success) {
  319. //Fluttertoast.showToast(msg: '分享成功');
  320. } else if (state == SSDKResponseState.Cancel) {
  321. Fluttertoast.showToast(msg: '取消分享');
  322. }
  323. Logger.debug('${state}, ${error.rawData}');
  324. Loading.dismiss();
  325. });
  326. }
  327. // 系统分享,只能分享图片或者文字,不能组合分享
  328. void _shareBySystem(String type) async {
  329. int count = 0;
  330. if (widget.model.poster != null) {
  331. count++;
  332. }
  333. count += (widget.model?.image?.length ?? 0);
  334. // 多图分享
  335. if (count > 1) {
  336. _shareMultipleImages(type);
  337. return;
  338. }
  339. if (widget.model.poster != null) {
  340. String path = await _savePoster();
  341. if (path != null && path != '') {
  342. ShareExtend.share(path, 'image');
  343. }
  344. } else {
  345. ShareExtend.share(widget.model.content, 'text');
  346. }
  347. }
  348. Future<String> _savePoster() async {
  349. String path;
  350. if (widget.model.poster != null) {
  351. // 检查并请求权限
  352. var status = await Permission.storage.status;
  353. if (status != PermissionStatus.granted) {
  354. status = await Permission.storage.request();
  355. }
  356. if (status != PermissionStatus.granted) {
  357. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  358. return null;
  359. }
  360. try {
  361. // 保存到本地路径
  362. // final tempDir = await getTemporaryDirectory();
  363. final tempDir = await getApplicationSupportDirectory();
  364. final file = await File('${tempDir.path}/image.png').create();
  365. file.writeAsBytesSync(widget.model.poster);
  366. path = file.path;
  367. Logger.debug(file.path);
  368. } catch (err, s) {
  369. Logger.error(err.toString(), s.toString());
  370. Fluttertoast.showToast(msg: '分享失败');
  371. return null;
  372. }
  373. }
  374. return path;
  375. }
  376. // 多图分享,调用系统分享
  377. void _shareMultipleImages(String type) async {
  378. List<String> paths = List();
  379. String path = await _savePoster();
  380. if (path != null && path != '') {
  381. paths.add(path);
  382. }
  383. Loading.show(context);
  384. List<String> downPaths = await ImageDownloadUtil.download(widget.model.image);
  385. paths.addAll(downPaths);
  386. if (Platform.isAndroid && type == 'wx') {
  387. MorePictureShare.shareWeixinPics(paths);
  388. } else if (Platform.isAndroid && type == 'pyq') {
  389. MorePictureShare.shareWeixinPicsCirlc(paths);
  390. } else {
  391. ShareExtend.shareMultiple(paths, "image", subject: "");
  392. }
  393. Loading.dismiss();
  394. }
  395. }