基础组件库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

share_alert.dart 13 KiB

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