基础组件库
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

share_alert.dart 9.9 KiB

hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. if (state == SSDKResponseState.Fail) {
  252. Fluttertoast.showToast(msg: '分享失败');
  253. } else if (state == SSDKResponseState.Success) {
  254. Fluttertoast.showToast(msg: '分享成功');
  255. } else if (state == SSDKResponseState.Cancel) {
  256. Fluttertoast.showToast(msg: '取消分享');
  257. }
  258. Logger.debug('${state}, ${error.rawData}');
  259. });
  260. }
  261. // 系统分享,只能分享图片或者文字,不能组合分享
  262. void _shareBySystem() async {
  263. int count = 0;
  264. if (widget.model.poster != null) {
  265. count++;
  266. }
  267. count += (widget.model?.image?.length ?? 0);
  268. // 多图分享
  269. if (count > 1) {
  270. _shareMultipleImages();
  271. return;
  272. }
  273. if (widget.model.poster != null) {
  274. String path = await _savePoster();
  275. if (path != null && path != '') {
  276. ShareExtend.share(path, 'image');
  277. }
  278. } else {
  279. ShareExtend.share(widget.model.content, 'text');
  280. }
  281. }
  282. Future<String> _savePoster() async {
  283. String path;
  284. if (widget.model.poster != null) {
  285. // 检查并请求权限
  286. var status = await Permission.storage.status;
  287. if (status != PermissionStatus.granted) {
  288. status = await Permission.storage.request();
  289. }
  290. if (status == PermissionStatus.denied) {
  291. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  292. return null;
  293. }
  294. try {
  295. // 保存到本地路径
  296. final tempDir = await getTemporaryDirectory();
  297. final file = await File('${tempDir.path}/image.jpg').create();
  298. file.writeAsBytesSync(widget.model.poster);
  299. path = file.path;
  300. Logger.debug(file.path);
  301. } catch (err, s) {
  302. Logger.error(err.toString(), s.toString());
  303. Fluttertoast.showToast(msg: '分享失败');
  304. return null;
  305. }
  306. }
  307. return path;
  308. }
  309. // 多图分享,调用系统分享
  310. void _shareMultipleImages() async {
  311. List<String> paths = List();
  312. String path = await _savePoster();
  313. if (path != null && path != '') {
  314. paths.add(path);
  315. }
  316. List<String> downPaths =
  317. await ImageDownloadUtil.download(widget.model.image);
  318. paths.addAll(downPaths);
  319. ShareExtend.shareMultiple(paths, "image", subject: "");
  320. }
  321. }