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