基础组件库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

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