基础组件库
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.
 
 
 
 
 

393 lines
12 KiB

  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?.length != 0 && widget.model?.url != null) {
  254. type = SSDKContentTypes.webpage;
  255. } else if (widget?.model?.image?.length != 0) {
  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. var isExistImage = false;
  265. if (widget?.model?.image != null && widget?.model?.image?.length > 0) {
  266. isExistImage = true;
  267. }
  268. params = SSDKMap()
  269. ..setGeneral(
  270. widget.model?.title ?? '',
  271. widget?.model?.content ?? '',
  272. Platform.isIOS ? widget.model.image : null,
  273. Platform.isAndroid ? (isExistImage ? widget?.model?.image?.first : null) : null,
  274. null,
  275. widget.model.url,
  276. null,
  277. null,
  278. null,
  279. null,
  280. type,
  281. );
  282. }
  283. SharesdkPlugin.share(plateform, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {
  284. print(error);
  285. if (state == SSDKResponseState.Fail) {
  286. Fluttertoast.showToast(msg: '分享失败');
  287. } else if (state == SSDKResponseState.Success) {
  288. //Fluttertoast.showToast(msg: '分享成功');
  289. } else if (state == SSDKResponseState.Cancel) {
  290. Fluttertoast.showToast(msg: '取消分享');
  291. }
  292. Logger.debug('${state}, ${error.rawData}');
  293. Loading.dismiss();
  294. });
  295. }
  296. // 系统分享,只能分享图片或者文字,不能组合分享
  297. void _shareBySystem(String type) async {
  298. int count = 0;
  299. if (widget.model.poster != null) {
  300. count++;
  301. }
  302. count += (widget.model?.image?.length ?? 0);
  303. // 多图分享
  304. if (count > 1) {
  305. _shareMultipleImages(type);
  306. return;
  307. }
  308. if (widget.model.poster != null) {
  309. String path = await _savePoster();
  310. if (path != null && path != '') {
  311. ShareExtend.share(path, 'image');
  312. }
  313. } else {
  314. ShareExtend.share(widget.model.content, 'text');
  315. }
  316. }
  317. Future<String> _savePoster() async {
  318. String path;
  319. if (widget.model.poster != null) {
  320. // 检查并请求权限
  321. var status = await Permission.storage.status;
  322. if (status != PermissionStatus.granted) {
  323. status = await Permission.storage.request();
  324. }
  325. if (status != PermissionStatus.granted) {
  326. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  327. return null;
  328. }
  329. try {
  330. // 保存到本地路径
  331. final tempDir = await getTemporaryDirectory();
  332. final file = await File('${tempDir.path}/image.png').create();
  333. file.writeAsBytesSync(widget.model.poster);
  334. path = file.path;
  335. Logger.debug(file.path);
  336. } catch (err, s) {
  337. Logger.error(err.toString(), s.toString());
  338. Fluttertoast.showToast(msg: '分享失败');
  339. return null;
  340. }
  341. }
  342. return path;
  343. }
  344. // 多图分享,调用系统分享
  345. void _shareMultipleImages(String type) async {
  346. List<String> paths = List();
  347. String path = await _savePoster();
  348. if (path != null && path != '') {
  349. paths.add(path);
  350. }
  351. Loading.show(context);
  352. List<String> downPaths = await ImageDownloadUtil.download(widget.model.image);
  353. paths.addAll(downPaths);
  354. if (Platform.isAndroid && type == 'wx') {
  355. MorePictureShare.shareWeixinPics(paths);
  356. } else if (Platform.isAndroid && type == 'pyq') {
  357. MorePictureShare.shareWeixinPicsCirlc(paths);
  358. } else {
  359. ShareExtend.shareMultiple(paths, "image", subject: "");
  360. }
  361. Loading.dismiss();
  362. }
  363. }