基础组件库
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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