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

251 lines
7.1 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. class ShareAlert extends StatelessWidget {
  12. final String skipIdentifier;
  13. final Widget child;
  14. final ShareDataModel model;
  15. const ShareAlert(this.model, this.skipIdentifier, {Key key, this.child})
  16. : super(key: key); // 中间视图
  17. @override
  18. Widget build(BuildContext context) {
  19. return GestureDetector(
  20. child: Scaffold(
  21. backgroundColor: Colors.transparent,
  22. body: BackdropFilter(
  23. filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), //背景
  24. child: Container(
  25. child: Column(
  26. children: <Widget>[
  27. Expanded(
  28. child: Center(child: child ?? Container()),
  29. ),
  30. _ShareAlertContent(this.model, this.skipIdentifier),
  31. ],
  32. ),
  33. ), // 模糊化
  34. ),
  35. ),
  36. onTap: () {
  37. Navigator.of(context).pop();
  38. },
  39. );
  40. }
  41. }
  42. class _ShareAlertContent extends StatefulWidget {
  43. final ShareDataModel model;
  44. final String skipIdentifier;
  45. const _ShareAlertContent(this.model, this.skipIdentifier, {Key key})
  46. : super(key: key);
  47. @override
  48. _ShareAlertContentState createState() => _ShareAlertContentState();
  49. }
  50. class _ShareAlertContentState extends State<_ShareAlertContent> {
  51. List<ShareIconModel> _icons = [];
  52. @override
  53. void initState() {
  54. NetUtil.request('/api/v1/mod/${widget.skipIdentifier}',
  55. method: NetMethod.GET, onCache: (data) {
  56. _parseData(data);
  57. }, onSuccess: (data) {
  58. _parseData(data);
  59. }, onError: (err) {});
  60. super.initState();
  61. }
  62. void _parseData(Map<String, dynamic> data) {
  63. List modList = data['mod_list'];
  64. Map d = modList.first;
  65. if (d != null) {
  66. String dString = d['data'];
  67. List list = jsonDecode(dString);
  68. _icons = list.map((item) {
  69. return ShareIconModel.fromJson(Map<String, dynamic>.from(item));
  70. }).toList();
  71. setState(() {});
  72. }
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. return GestureDetector(
  77. onTap: () {},
  78. child: Container(
  79. width: double.infinity,
  80. decoration: BoxDecoration(
  81. color: Colors.white,
  82. borderRadius: BorderRadius.only(
  83. topLeft: Radius.circular(12),
  84. topRight: Radius.circular(12),
  85. ),
  86. ),
  87. child: SafeArea(
  88. top: false,
  89. child: Column(
  90. children: <Widget>[
  91. Container(
  92. margin: EdgeInsets.only(top: 8, bottom: 8),
  93. width: 62,
  94. height: 4,
  95. decoration: BoxDecoration(
  96. color: Color(0xffd8d8d8),
  97. borderRadius: BorderRadius.circular(2)),
  98. ),
  99. Text(
  100. '分享至',
  101. style: TextStyle(
  102. fontSize: 15,
  103. color: Color(0xff333333),
  104. fontWeight: FontWeight.bold),
  105. ),
  106. Container(
  107. margin:
  108. EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
  109. child: _createIcons(),
  110. ),
  111. GestureDetector(
  112. child: Container(
  113. margin: EdgeInsets.only(left: 12, right: 12, bottom: 10),
  114. padding: EdgeInsets.all(12),
  115. decoration: BoxDecoration(
  116. color: Color(0xfff3f3f3),
  117. borderRadius: BorderRadius.circular(8)),
  118. child: Center(
  119. child: Text(
  120. '取消',
  121. style: TextStyle(
  122. fontSize: 12,
  123. fontWeight: FontWeight.bold,
  124. color: Color(0xff999999)),
  125. ),
  126. ),
  127. ),
  128. onTap: () {
  129. Navigator.of(context).pop();
  130. },
  131. )
  132. ],
  133. ),
  134. ),
  135. ),
  136. );
  137. }
  138. Widget _createIcons() {
  139. return Wrap(
  140. spacing: 10,
  141. runSpacing: 10,
  142. children: _icons.map((item) {
  143. return _createIcon(item);
  144. }).toList(),
  145. );
  146. }
  147. Widget _createIcon(ShareIconModel item) {
  148. return GestureDetector(
  149. child: Container(
  150. width: 60,
  151. child: Column(
  152. children: <Widget>[
  153. Container(
  154. width: 40,
  155. height: 40,
  156. child: CachedNetworkImage(
  157. imageUrl: item.icon,
  158. fit: BoxFit.contain,
  159. ),
  160. ),
  161. Padding(
  162. padding: const EdgeInsets.only(top: 2, bottom: 2),
  163. child: Text(
  164. item.name,
  165. style: TextStyle(
  166. fontSize: 12,
  167. color: Color(0xff333333),
  168. fontWeight: FontWeight.bold),
  169. ),
  170. ),
  171. ],
  172. ),
  173. ),
  174. onTap: () {
  175. if (item.type == 'wx') {
  176. _shareByMob(ShareSDKPlatforms.wechatSession);
  177. } else if (item.type == 'pyq') {
  178. _shareByMob(ShareSDKPlatforms.wechatTimeline);
  179. } else if (item.type == 'qq') {
  180. _shareByMob(ShareSDKPlatforms.qq);
  181. } else if (item.type == 'qq_space') {
  182. _shareByMob(ShareSDKPlatforms.qZone);
  183. } else if (item.type == 'weibo') {
  184. _shareByMob(ShareSDKPlatforms.sina);
  185. } else if (item.type == 'more_setting') {
  186. _shareBySystem();
  187. }
  188. },
  189. );
  190. }
  191. void _shareByMob(ShareSDKPlatform plateform) async {
  192. // dynamic image;
  193. if (widget.model.image != null && widget.model.image.length > 1) {
  194. _shareMultipleImages();
  195. return;
  196. }
  197. //单独公共分享
  198. SSDKMap params = SSDKMap()
  199. ..setGeneral(
  200. widget.model.title,
  201. widget.model.content,
  202. widget.model.image,
  203. null,
  204. null,
  205. widget.model.url,
  206. null,
  207. null,
  208. null,
  209. null,
  210. SSDKContentTypes.auto,
  211. );
  212. SharesdkPlugin.share(plateform, params, (SSDKResponseState state,
  213. Map userdata, Map contentEntity, SSDKError error) {
  214. Logger.debug('${state}, ${error.rawData}');
  215. });
  216. }
  217. // 系统分享,只能分享图片或者文字,不能组合分享
  218. void _shareBySystem() async {
  219. if (widget.model.image.length >= 1) {
  220. _shareMultipleImages();
  221. return;
  222. } else {
  223. ShareExtend.share(widget.model.content, 'text');
  224. }
  225. }
  226. // 多图分享,调用系统分享
  227. void _shareMultipleImages() async {
  228. List<String> paths = await ImageDownloadUtil.download(widget.model.image);
  229. ShareExtend.shareMultiple(paths, "image", subject: "");
  230. }
  231. }