基础组件库
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 12 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
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 (item.type == 'wx') {
  211. _shareByMob(ShareSDKPlatforms.wechatSession);
  212. } else if (item.type == 'pyq') {
  213. _shareByMob(ShareSDKPlatforms.wechatTimeline);
  214. } else if (item.type == 'qq') {
  215. _shareByMob(ShareSDKPlatforms.qq);
  216. } else if (item.type == 'qq_space') {
  217. _shareByMob(ShareSDKPlatforms.qZone);
  218. } else if (item.type == 'weibo') {
  219. _shareByMob(ShareSDKPlatforms.sina);
  220. } else if (item.type == 'more_setting') {
  221. _shareBySystem(item.type);
  222. }
  223. },
  224. );
  225. }
  226. // mob分享,只能单图分享,多图分享调用系统分享
  227. void _shareByMob(ShareSDKPlatform plateform) async {
  228. Loading.show(context);
  229. Timer(Duration(milliseconds: 2000), () {
  230. Loading.dismiss();
  231. });
  232. SSDKMap params;
  233. if (widget.model.poster != null) {
  234. String path = await _savePoster();
  235. if (path != null && path != '') {
  236. params = SSDKMap()
  237. ..setGeneral(
  238. widget.model?.title ?? '',
  239. widget.model?.content ?? '',
  240. Platform.isIOS ? path : null,
  241. null,
  242. Platform.isAndroid ? path : null,
  243. null,
  244. null,
  245. null,
  246. null,
  247. null,
  248. SSDKContentTypes.image,
  249. );
  250. }
  251. } else {
  252. var type = SSDKContentTypes.auto;
  253. if (widget?.model?.image?.first != null && widget.model?.url != null) {
  254. type = SSDKContentTypes.webpage;
  255. } else if (widget?.model?.image?.first != null) {
  256. type = SSDKContentTypes.image;
  257. } else if (widget?.model?.title != null || widget.model?.content != null) {
  258. type = SSDKContentTypes.text;
  259. }
  260. if (plateform == ShareSDKPlatforms.qZone && type == SSDKContentTypes.text) {
  261. widget?.model?.title = null;
  262. type = SSDKContentTypes.message;
  263. }
  264. params = SSDKMap()
  265. ..setGeneral(
  266. widget.model?.title ?? '',
  267. widget?.model?.content ?? '',
  268. Platform.isIOS ? widget.model.image : null,
  269. Platform.isAndroid ? widget?.model?.image?.first : null,
  270. null,
  271. widget.model.url,
  272. null,
  273. null,
  274. null,
  275. null,
  276. type,
  277. );
  278. }
  279. SharesdkPlugin.share(plateform, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {
  280. print(error);
  281. if (state == SSDKResponseState.Fail) {
  282. Fluttertoast.showToast(msg: '分享失败');
  283. } else if (state == SSDKResponseState.Success) {
  284. //Fluttertoast.showToast(msg: '分享成功');
  285. } else if (state == SSDKResponseState.Cancel) {
  286. Fluttertoast.showToast(msg: '取消分享');
  287. }
  288. Logger.debug('${state}, ${error.rawData}');
  289. Loading.dismiss();
  290. });
  291. }
  292. // 系统分享,只能分享图片或者文字,不能组合分享
  293. void _shareBySystem(String type) async {
  294. int count = 0;
  295. if (widget.model.poster != null) {
  296. count++;
  297. }
  298. count += (widget.model?.image?.length ?? 0);
  299. // 多图分享
  300. if (count > 1) {
  301. _shareMultipleImages(type);
  302. return;
  303. }
  304. if (widget.model.poster != null) {
  305. String path = await _savePoster();
  306. if (path != null && path != '') {
  307. ShareExtend.share(path, 'image');
  308. }
  309. } else {
  310. ShareExtend.share(widget.model.content, 'text');
  311. }
  312. }
  313. Future<String> _savePoster() async {
  314. String path;
  315. if (widget.model.poster != null) {
  316. // 检查并请求权限
  317. var status = await Permission.storage.status;
  318. if (status != PermissionStatus.granted) {
  319. status = await Permission.storage.request();
  320. }
  321. if (status != PermissionStatus.granted) {
  322. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  323. return null;
  324. }
  325. try {
  326. // 保存到本地路径
  327. final tempDir = await getTemporaryDirectory();
  328. final file = await File('${tempDir.path}/image.png').create();
  329. file.writeAsBytesSync(widget.model.poster);
  330. path = file.path;
  331. Logger.debug(file.path);
  332. } catch (err, s) {
  333. Logger.error(err.toString(), s.toString());
  334. Fluttertoast.showToast(msg: '分享失败');
  335. return null;
  336. }
  337. }
  338. return path;
  339. }
  340. // 多图分享,调用系统分享
  341. void _shareMultipleImages(String type) async {
  342. List<String> paths = List();
  343. String path = await _savePoster();
  344. if (path != null && path != '') {
  345. paths.add(path);
  346. }
  347. Loading.show(context);
  348. List<String> downPaths = await ImageDownloadUtil.download(widget.model.image);
  349. paths.addAll(downPaths);
  350. if (Platform.isAndroid && type == 'wx') {
  351. MorePictureShare.shareWeixinPics(paths);
  352. } else if (Platform.isAndroid && type == 'pyq') {
  353. MorePictureShare.shareWeixinPicsCirlc(paths);
  354. } else {
  355. ShareExtend.shareMultiple(paths, "image", subject: "");
  356. }
  357. Loading.dismiss();
  358. }
  359. }