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

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