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

364 lines
11 KiB

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