基础组件库
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 11 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. Widget build(BuildContext context) {
  97. return GestureDetector(
  98. onTap: () {},
  99. child: Container(
  100. width: double.infinity,
  101. decoration: BoxDecoration(
  102. color: Colors.white,
  103. borderRadius: BorderRadius.only(
  104. topLeft: Radius.circular(12),
  105. topRight: Radius.circular(12),
  106. ),
  107. ),
  108. child: SafeArea(
  109. top: false,
  110. child: Column(
  111. children: <Widget>[
  112. Container(
  113. margin: EdgeInsets.only(top: 8, bottom: 8),
  114. width: 62,
  115. height: 4,
  116. decoration: BoxDecoration(color: Color(0xffd8d8d8), borderRadius: BorderRadius.circular(2)),
  117. ),
  118. Text(
  119. '分享至',
  120. style: TextStyle(fontSize: 15, color: Color(0xff333333), fontWeight: FontWeight.bold),
  121. ),
  122. Container(
  123. margin: EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
  124. child: _createIcons(),
  125. ),
  126. GestureDetector(
  127. child: Container(
  128. margin: EdgeInsets.only(left: 12, right: 12, bottom: 10),
  129. padding: EdgeInsets.all(12),
  130. decoration: BoxDecoration(color: Color(0xfff3f3f3), borderRadius: BorderRadius.circular(8)),
  131. child: Center(
  132. child: Text(
  133. '取消',
  134. style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Color(0xff999999)),
  135. ),
  136. ),
  137. ),
  138. onTap: () {
  139. Navigator.of(context).pop();
  140. },
  141. )
  142. ],
  143. ),
  144. ),
  145. ),
  146. );
  147. }
  148. Widget _createIcons() {
  149. return Wrap(
  150. spacing: 10,
  151. runSpacing: 10,
  152. children: widget.iconModel?.icons?.map((item) {
  153. return _createIcon(item);
  154. })?.toList() ??
  155. [],
  156. );
  157. }
  158. Widget _createIcon(ShareIconModel item) {
  159. return GestureDetector(
  160. child: Container(
  161. width: 60,
  162. child: Column(
  163. children: <Widget>[
  164. Container(
  165. width: 40,
  166. height: 40,
  167. child: CachedNetworkImage(
  168. imageUrl: item.icon,
  169. fit: BoxFit.contain,
  170. ),
  171. ),
  172. Padding(
  173. padding: const EdgeInsets.only(top: 2, bottom: 2),
  174. child: Text(
  175. item.name,
  176. style: TextStyle(fontSize: 12, color: Color(0xff333333), fontWeight: FontWeight.bold),
  177. ),
  178. ),
  179. ],
  180. ),
  181. ),
  182. onTap: () async {
  183. //检查是否有存储权限
  184. var status = await Permission.storage.status;
  185. if (!status.isGranted) {
  186. status = await Permission.storage.request();
  187. print(status);
  188. return;
  189. }
  190. int count = 0;
  191. if (widget.model.poster != null) {
  192. count++;
  193. }
  194. count += (widget.model?.image?.length ?? 0);
  195. // 多图分享
  196. if (count > 1) {
  197. _shareMultipleImages(item.type);
  198. return;
  199. }
  200. if (item.type == 'wx') {
  201. _shareByMob(ShareSDKPlatforms.wechatSession);
  202. } else if (item.type == 'pyq') {
  203. _shareByMob(ShareSDKPlatforms.wechatTimeline);
  204. } else if (item.type == 'qq') {
  205. _shareByMob(ShareSDKPlatforms.qq);
  206. } else if (item.type == 'qq_space') {
  207. _shareByMob(ShareSDKPlatforms.qZone);
  208. } else if (item.type == 'weibo') {
  209. _shareByMob(ShareSDKPlatforms.sina);
  210. } else if (item.type == 'more_setting') {
  211. _shareBySystem(item.type);
  212. }
  213. },
  214. );
  215. }
  216. // mob分享,只能单图分享,多图分享调用系统分享
  217. void _shareByMob(ShareSDKPlatform plateform) async {
  218. Loading.show(context);
  219. Timer(Duration(milliseconds: 2000),(){
  220. Loading.dismiss();
  221. });
  222. SSDKMap params;
  223. if (widget.model.poster != null) {
  224. String path = await _savePoster();
  225. if (path != null && path != '') {
  226. params = SSDKMap()
  227. ..setGeneral(
  228. widget.model?.title ?? '',
  229. widget.model?.content ?? '',
  230. Platform.isIOS ? path : null,
  231. null,
  232. Platform.isAndroid ? path : null,
  233. null,
  234. null,
  235. null,
  236. null,
  237. null,
  238. SSDKContentTypes.image,
  239. );
  240. }
  241. } else {
  242. var type=SSDKContentTypes.auto;
  243. if(widget?.model?.image?.first!=null&&widget.model?.url!=null){
  244. type=SSDKContentTypes.webpage;
  245. }else if(widget?.model?.image?.first!=null){
  246. type=SSDKContentTypes.image;
  247. }else if(widget?.model?.title!=null||widget.model?.content!=null){
  248. type=SSDKContentTypes.text;
  249. }
  250. if(plateform==ShareSDKPlatforms.qZone){
  251. widget?.model?.title=null;
  252. type=SSDKContentTypes.message;
  253. }
  254. params = SSDKMap()
  255. ..setGeneral(
  256. widget.model?.title ?? '',
  257. widget?.model?.content??'',
  258. Platform.isIOS ? widget.model.image : null,
  259. Platform.isAndroid ? widget?.model?.image?.first : null,
  260. null,
  261. widget.model.url,
  262. null,
  263. null,
  264. null,
  265. null,
  266. type,
  267. );
  268. }
  269. SharesdkPlugin.share(plateform, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {
  270. print(error);
  271. if (state == SSDKResponseState.Fail) {
  272. Fluttertoast.showToast(msg: '分享失败');
  273. } else if (state == SSDKResponseState.Success) {
  274. //Fluttertoast.showToast(msg: '分享成功');
  275. } else if (state == SSDKResponseState.Cancel) {
  276. Fluttertoast.showToast(msg: '取消分享');
  277. }
  278. Logger.debug('${state}, ${error.rawData}');
  279. Loading.dismiss();
  280. });
  281. }
  282. // 系统分享,只能分享图片或者文字,不能组合分享
  283. void _shareBySystem(String type) async {
  284. int count = 0;
  285. if (widget.model.poster != null) {
  286. count++;
  287. }
  288. count += (widget.model?.image?.length ?? 0);
  289. // 多图分享
  290. if (count > 1) {
  291. _shareMultipleImages(type);
  292. return;
  293. }
  294. if (widget.model.poster != null) {
  295. String path = await _savePoster();
  296. if (path != null && path != '') {
  297. ShareExtend.share(path, 'image');
  298. }
  299. } else {
  300. ShareExtend.share(widget.model.content, 'text');
  301. }
  302. }
  303. Future<String> _savePoster() async {
  304. String path;
  305. if (widget.model.poster != null) {
  306. // 检查并请求权限
  307. var status = await Permission.storage.status;
  308. if (status != PermissionStatus.granted) {
  309. status = await Permission.storage.request();
  310. }
  311. if (status == PermissionStatus.denied) {
  312. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  313. return null;
  314. }
  315. try {
  316. // 保存到本地路径
  317. final tempDir = await getTemporaryDirectory();
  318. final file = await File('${tempDir.path}/image.png').create();
  319. file.writeAsBytesSync(widget.model.poster);
  320. path = file.path;
  321. Logger.debug(file.path);
  322. } catch (err, s) {
  323. Logger.error(err.toString(), s.toString());
  324. Fluttertoast.showToast(msg: '分享失败');
  325. return null;
  326. }
  327. }
  328. return path;
  329. }
  330. // 多图分享,调用系统分享
  331. void _shareMultipleImages(String type) async {
  332. List<String> paths = List();
  333. String path = await _savePoster();
  334. if (path != null && path != '') {
  335. paths.add(path);
  336. }
  337. Loading.show(context);
  338. List<String> downPaths = await ImageDownloadUtil.download(widget.model.image);
  339. paths.addAll(downPaths);
  340. if(Platform.isAndroid&&type=='wx'){
  341. MorePictureShare.shareWeixinPics(paths);
  342. }else{
  343. ShareExtend.shareMultiple(paths, "image", subject: "");
  344. }
  345. Loading.dismiss();
  346. }
  347. }