基础组件库
 
 
 
 
 

311 linhas
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:fluttertoast/fluttertoast.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import 'package:share_extend/share_extend.dart';
  8. import 'package:sharesdk_plugin/sharesdk_plugin.dart';
  9. import 'package:zhiying_base_widget/utils/image_download_util/image_download_util.dart';
  10. import 'package:zhiying_base_widget/widgets/share/models/share_data_model.dart';
  11. import 'package:zhiying_base_widget/widgets/share/models/share_icon_model.dart';
  12. import 'package:zhiying_comm/zhiying_comm.dart';
  13. // import 'package:image_gallery_saver/image_gallery_saver.dart';
  14. import 'package:path_provider/path_provider.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. void _shareByMob(ShareSDKPlatform plateform) async {
  203. int count = 0;
  204. if (widget.model.poster != null) {
  205. count++;
  206. }
  207. count += (widget.model?.image?.length ?? 0);
  208. // 多图分享
  209. if (count > 1) {
  210. _shareMultipleImages();
  211. return;
  212. }
  213. SSDKMap params;
  214. if (widget.model.poster != null) {
  215. // 检查并请求权限
  216. var status = await Permission.storage.status;
  217. if (status != PermissionStatus.granted) {
  218. status = await Permission.storage.request();
  219. }
  220. if (status == PermissionStatus.denied) {
  221. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  222. return;
  223. }
  224. // PermissionStatus status = await PermissionHandler().checkPermissionStatus(
  225. // PermissionGroup.storage);
  226. // if (PermissionStatus.granted != status) {
  227. // PermissionHandler().requestPermissions(<PermissionGroup>[
  228. // PermissionGroup.storage,
  229. // ]);
  230. // }
  231. // final result = await ImageGallerySaver.saveImage(widget.model.poster);
  232. // var appDocDir = await getTemporaryDirectory();
  233. // final result = await ImageGallerySaver.saveImage(
  234. // widget.model.poster,
  235. // quality: 60,
  236. // name: "hello");
  237. // Logger.debug(result);
  238. // params = SSDKMap()
  239. // ..setGeneral(
  240. // widget.model.title,
  241. // widget.model.content,
  242. // widget.model.poster,
  243. // null,
  244. // null,
  245. // widget.model.url,
  246. // null,
  247. // null,
  248. // null,
  249. // null,
  250. // SSDKContentTypes.auto,
  251. // );
  252. } else {
  253. params = SSDKMap()
  254. ..setGeneral(
  255. widget.model.title,
  256. widget.model.content,
  257. widget.model.image,
  258. null,
  259. null,
  260. widget.model.url,
  261. null,
  262. null,
  263. null,
  264. null,
  265. SSDKContentTypes.auto,
  266. );
  267. }
  268. SharesdkPlugin.share(plateform, params, (SSDKResponseState state,
  269. Map userdata, Map contentEntity, SSDKError error) {
  270. Logger.debug('${state}, ${error.rawData}');
  271. });
  272. }
  273. // 系统分享,只能分享图片或者文字,不能组合分享
  274. void _shareBySystem() async {
  275. if (widget.model.image.length >= 1) {
  276. _shareMultipleImages();
  277. return;
  278. } else {
  279. ShareExtend.share(widget.model.content, 'text');
  280. }
  281. }
  282. // 多图分享,调用系统分享
  283. void _shareMultipleImages() async {
  284. List<String> paths = await ImageDownloadUtil.download(widget.model.image);
  285. ShareExtend.shareMultiple(paths, "image", subject: "");
  286. }
  287. }