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

261 lines
7.4 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:share_extend/share_extend.dart';
  6. import 'package:sharesdk_plugin/sharesdk_plugin.dart';
  7. import 'package:zhiying_base_widget/utils/image_download_util/image_download_util.dart';
  8. import 'package:zhiying_base_widget/widgets/share/models/share_data_model.dart';
  9. import 'package:zhiying_base_widget/widgets/share/models/share_icon_model.dart';
  10. import 'package:zhiying_comm/zhiying_comm.dart';
  11. import 'package:permission_handler/permission_handler.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. // dynamic image;
  201. if (widget.model.image != null && widget.model.image.length > 1) {
  202. _shareMultipleImages();
  203. return;
  204. }
  205. //单独公共分享
  206. SSDKMap params = SSDKMap()
  207. ..setGeneral(
  208. widget.model.title,
  209. widget.model.content,
  210. widget.model.image,
  211. null,
  212. null,
  213. widget.model.url,
  214. null,
  215. null,
  216. null,
  217. null,
  218. SSDKContentTypes.auto,
  219. );
  220. SharesdkPlugin.share(plateform, params, (SSDKResponseState state,
  221. Map userdata, Map contentEntity, SSDKError error) {
  222. Logger.debug('${state}, ${error.rawData}');
  223. });
  224. }
  225. // 系统分享,只能分享图片或者文字,不能组合分享
  226. void _shareBySystem() async {
  227. if (widget.model.image.length >= 1) {
  228. _shareMultipleImages();
  229. return;
  230. } else {
  231. ShareExtend.share(widget.model.content, 'text');
  232. }
  233. }
  234. // 多图分享,调用系统分享
  235. void _shareMultipleImages() async {
  236. List<String> paths = await ImageDownloadUtil.download(widget.model.image);
  237. ShareExtend.shareMultiple(paths, "image", subject: "");
  238. }
  239. }