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

303 lines
9.0 KiB

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