基础组件库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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