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

582 lines
20 KiB

  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:typed_data';
  5. import 'dart:ui';
  6. import 'dart:ui' as ui;
  7. import 'package:cached_network_image/cached_network_image.dart';
  8. import 'package:flutter/cupertino.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter/rendering.dart';
  11. import 'package:flutter/services.dart';
  12. import 'package:flutter_swiper/flutter_swiper.dart';
  13. import 'package:fluttertoast/fluttertoast.dart';
  14. import 'package:more_picture_share/more_picture_share.dart';
  15. import 'package:path_provider/path_provider.dart';
  16. import 'package:permission_handler/permission_handler.dart';
  17. import 'package:share_extend/share_extend.dart';
  18. import 'package:sharesdk_plugin/sharesdk_plugin.dart';
  19. import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_create.dart';
  20. import 'package:zhiying_base_widget/dialog/loading/loading.dart';
  21. import 'package:zhiying_base_widget/utils/image_download_util/image_download_util.dart';
  22. import 'package:zhiying_base_widget/widgets/share/models/share_alert_model.dart';
  23. import 'package:zhiying_base_widget/widgets/share/models/share_data_model.dart';
  24. import 'package:zhiying_base_widget/widgets/share/models/share_icon_model.dart';
  25. import 'package:zhiying_base_widget/widgets/share/share_alert_content.dart';
  26. import 'package:zhiying_comm/zhiying_comm.dart';
  27. import 'package:zhiying_base_widget/widgets/share/models/share_select_pic_model.dart';
  28. import 'package:zhiying_base_widget/template/goods_share_template/goods_share_template.dart';
  29. class ShareAlertSelect extends StatefulWidget {
  30. final String skipIdentifier;
  31. final bool isContentShow;
  32. final ShareDataModel model;
  33. final bool isPicShow;
  34. final ShareSelectPicModel selectPicModel;
  35. const ShareAlertSelect(this.model, this.skipIdentifier, {Key key, this.isContentShow = false, this.selectPicModel, this.isPicShow}) : super(key: key); // 中间视图
  36. @override
  37. _ShareAlertSelectState createState() => _ShareAlertSelectState();
  38. }
  39. class _ShareAlertSelectState extends State<ShareAlertSelect> {
  40. ShareAlertModel _iconModel;
  41. bool isPicShow;
  42. ShareSelectPicModel selectPicModel;
  43. GlobalKey _globalKey = GlobalKey();
  44. List<int> tempPoster;
  45. @override
  46. void initState() {
  47. Logger.log('设置分享页面不弹');
  48. /// 设置分享页面不弹
  49. IntellectCreate.setCheck(false);
  50. NetUtil.request('/api/v1/mod/${widget.skipIdentifier}', method: NetMethod.GET, onCache: (data) {
  51. // try{
  52. // _parseData(data);
  53. // }catch(e){
  54. // print(e);
  55. // }
  56. }, onSuccess: (data) {
  57. print(data);
  58. _parseData(data);
  59. }, onError: (err) {});
  60. if (!EmptyUtil.isEmpty(widget.isPicShow)) {
  61. isPicShow = widget.isPicShow;
  62. } else {
  63. isPicShow = false;
  64. }
  65. selectPicModel = widget.selectPicModel;
  66. super.initState();
  67. }
  68. @override
  69. void dispose() {
  70. Logger.log('设置智能弹窗可以弹');
  71. /// 之后可以弹
  72. IntellectCreate.setCheck(true);
  73. super.dispose();
  74. }
  75. void _parseData(Map<String, dynamic> data) {
  76. List modList = data['mod_list'];
  77. Map d = modList.first;
  78. if (d != null) {
  79. String dString = d['data'];
  80. _iconModel = ShareAlertModel.fromJson(Map<String, dynamic>.from(jsonDecode(dString)));
  81. setState(() {});
  82. }
  83. }
  84. _selectPic(List<SharePicItem> picList, int position) async {
  85. ShareDataModel model = ShareDataModel();
  86. model.image = [];
  87. for (int i = 0; i < picList.length; i++) {
  88. if (picList[i].select) {
  89. if (i == 0) {
  90. if (tempPoster == null) {
  91. Loading.show(context);
  92. ShareDataModel shareDataModel = await _updateModel(_globalKey);
  93. tempPoster = shareDataModel.poster;
  94. model.poster = shareDataModel.poster;
  95. Loading.dismiss();
  96. } else {
  97. model.poster = tempPoster;
  98. }
  99. } else {
  100. model.image.add(picList[i].pic);
  101. }
  102. }
  103. }
  104. EventUtil.instance.fire(model);
  105. }
  106. ///截图保存
  107. Future<ShareDataModel> _updateModel(GlobalKey _globalKey) async {
  108. ShareDataModel _shareModel = ShareDataModel();
  109. BuildContext buildContext = _globalKey.currentContext;
  110. if (null != buildContext) {
  111. RenderRepaintBoundary boundary = buildContext.findRenderObject();
  112. ///pixelRatio是放大倍数
  113. ui.Image image = await boundary.toImage(pixelRatio: 5);
  114. // 注意:png是压缩后格式,如果需要图片的原始像素数据,请使用rawRgba
  115. ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  116. Uint8List pngBytes = byteData.buffer.asUint8List();
  117. _shareModel.poster = pngBytes;
  118. } else {
  119. _shareModel.poster = null;
  120. }
  121. // _shareModel.content = _isContentSelected ? _content : '';
  122. return _shareModel;
  123. }
  124. @override
  125. Widget build(BuildContext context) {
  126. return WillPopScope(
  127. onWillPop: () async {
  128. Loading.dismiss();
  129. Navigator.canPop(context);
  130. return true;
  131. },
  132. child: GestureDetector(
  133. child: Scaffold(
  134. backgroundColor: Colors.transparent,
  135. body: BackdropFilter(
  136. filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), //背景
  137. child: Container(
  138. child: Column(
  139. children: <Widget>[
  140. widget.isContentShow
  141. ? Expanded(
  142. child: Center(child: ShareAlertContent(_iconModel)),
  143. )
  144. : Container(),
  145. isPicShow
  146. ? Expanded(
  147. child: Swiper(
  148. loop: false,
  149. itemBuilder: (BuildContext context, int index) {
  150. Widget picWidget;
  151. if (index == 0) {
  152. picWidget = Column(
  153. children: [
  154. Expanded(
  155. child: Transform.scale(
  156. alignment: Alignment.center,
  157. scale: 0.75,
  158. child: GoodsShareTemplate(
  159. selectPicModel.posterModel,
  160. contentKey: _globalKey,
  161. ),
  162. ),
  163. ),
  164. ],
  165. );
  166. } else {
  167. picWidget = CachedNetworkImage(
  168. imageUrl: selectPicModel.picList[index].pic,
  169. fit: BoxFit.fitWidth,
  170. );
  171. }
  172. return Container(
  173. alignment: Alignment.bottomCenter,
  174. margin: EdgeInsets.only(top: 50, bottom: 10),
  175. child: Stack(
  176. children: <Widget>[
  177. picWidget,
  178. // CachedNetworkImage(
  179. // imageUrl: selectPicModel.picList[index].pic,
  180. // fit: BoxFit.fitWidth,
  181. // ),
  182. Positioned(
  183. bottom: 10,
  184. right: 15,
  185. child: GestureDetector(
  186. onTap: () {
  187. selectPicModel.picList[index].select = !selectPicModel.picList[index].select;
  188. setState(() {});
  189. _selectPic(selectPicModel.picList, index);
  190. },
  191. child: Container(
  192. alignment: Alignment.center,
  193. decoration: BoxDecoration(
  194. borderRadius: BorderRadius.circular(50),
  195. color: selectPicModel.picList[index].select ? Colors.red : Colors.grey,
  196. ),
  197. child: Icon(
  198. CupertinoIcons.check_mark,
  199. color: Colors.white,
  200. ),
  201. ),
  202. ),
  203. )
  204. ],
  205. ),
  206. );
  207. },
  208. itemCount: selectPicModel.picList.length,
  209. viewportFraction: 0.8,
  210. scale: 0.8,
  211. ),
  212. )
  213. : Container(),
  214. _ShareAlertContent(widget.model, widget.skipIdentifier, _iconModel, isPicShow),
  215. ],
  216. ),
  217. ), // 模糊化
  218. ),
  219. ),
  220. onTap: () {
  221. // Navigator.of(context).pop();
  222. },
  223. ),
  224. );
  225. }
  226. }
  227. class _ShareAlertContent extends StatefulWidget {
  228. final ShareDataModel model;
  229. final String skipIdentifier;
  230. final ShareAlertModel iconModel;
  231. final bool isSelectPic;
  232. const _ShareAlertContent(this.model, this.skipIdentifier, this.iconModel, this.isSelectPic, {Key key}) : super(key: key);
  233. @override
  234. _ShareAlertContentState createState() => _ShareAlertContentState();
  235. }
  236. class _ShareAlertContentState extends State<_ShareAlertContent> {
  237. StreamSubscription listen;
  238. bool isSelectPic;
  239. ShareDataModel _shareDataModel;
  240. @override
  241. void dispose() {
  242. // TODO: implement dispose
  243. listen?.cancel();
  244. super.dispose();
  245. }
  246. @override
  247. void initState() {
  248. // TODO: implement initState
  249. isSelectPic = widget.isSelectPic;
  250. if (isSelectPic) {
  251. _shareDataModel = null;
  252. } else {
  253. _shareDataModel = widget?.model;
  254. }
  255. listen = EventUtil.instance.on<ShareDataModel>().listen((ShareDataModel event) {
  256. if (isSelectPic) {
  257. _shareDataModel = event;
  258. }
  259. });
  260. super.initState();
  261. }
  262. @override
  263. Widget build(BuildContext context) {
  264. return GestureDetector(
  265. onTap: () {},
  266. child: Container(
  267. width: double.infinity,
  268. decoration: BoxDecoration(
  269. color: Colors.white,
  270. borderRadius: BorderRadius.only(
  271. topLeft: Radius.circular(12),
  272. topRight: Radius.circular(12),
  273. ),
  274. ),
  275. child: SafeArea(
  276. top: false,
  277. child: Column(
  278. children: <Widget>[
  279. Container(
  280. margin: EdgeInsets.only(top: 8, bottom: 8),
  281. width: 62,
  282. height: 4,
  283. decoration: BoxDecoration(color: Color(0xffd8d8d8), borderRadius: BorderRadius.circular(2)),
  284. ),
  285. Text(
  286. '分享至',
  287. style: TextStyle(fontSize: 15, color: Color(0xff333333), fontWeight: FontWeight.bold),
  288. ),
  289. Container(
  290. margin: EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
  291. child: _createIcons(),
  292. ),
  293. GestureDetector(
  294. child: Container(
  295. margin: EdgeInsets.only(left: 12, right: 12, bottom: 10),
  296. padding: EdgeInsets.all(12),
  297. decoration: BoxDecoration(color: Color(0xfff3f3f3), borderRadius: BorderRadius.circular(8)),
  298. child: Center(
  299. child: Text(
  300. '取消',
  301. style: TextStyle(fontSize: 12, fontWeight: FontWeight.bold, color: Color(0xff999999)),
  302. ),
  303. ),
  304. ),
  305. onTap: () {
  306. Navigator.of(context).pop();
  307. },
  308. )
  309. ],
  310. ),
  311. ),
  312. ),
  313. );
  314. }
  315. Widget _createIcons() {
  316. return Wrap(
  317. spacing: 10,
  318. runSpacing: 10,
  319. children: widget.iconModel?.icons?.map((item) {
  320. return _createIcon(item);
  321. })?.toList() ??
  322. [],
  323. );
  324. }
  325. Widget _createIcon(ShareIconModel item) {
  326. return GestureDetector(
  327. child: Container(
  328. width: 60,
  329. child: Column(
  330. children: <Widget>[
  331. Container(
  332. width: 40,
  333. height: 40,
  334. child: CachedNetworkImage(
  335. imageUrl: item.icon,
  336. fit: BoxFit.contain,
  337. ),
  338. ),
  339. Padding(
  340. padding: const EdgeInsets.only(top: 2, bottom: 2),
  341. child: Text(
  342. item.name,
  343. style: TextStyle(fontSize: 12, color: Color(0xff333333), fontWeight: FontWeight.bold),
  344. ),
  345. ),
  346. ],
  347. ),
  348. ),
  349. onTap: () async {
  350. //检查是否有存储权限
  351. var status = await Permission.storage.status;
  352. if (!status.isGranted) {
  353. status = await Permission.storage.request();
  354. print(status);
  355. return;
  356. }
  357. Loading.show(context);
  358. ///检查是否选中图片
  359. if (_shareDataModel?.poster == null && (_shareDataModel?.image == null || _shareDataModel?.image?.length == 0)) {
  360. Fluttertoast.showToast(msg: "请选择需要分享的图片");
  361. Loading.dismiss();
  362. return;
  363. }
  364. if (_shareDataModel?.poster != null) {
  365. File file = await EncodeUtil.compressImage(context, images: _shareDataModel?.poster, size: 500);
  366. _shareDataModel.poster = file.readAsBytesSync();
  367. Loading.dismiss();
  368. }
  369. int count = 0;
  370. if (_shareDataModel.poster != null) {
  371. count++;
  372. }
  373. count += (_shareDataModel?.image?.length ?? 0);
  374. // 多图分享
  375. if (count > 1) {
  376. _shareMultipleImages(item.type);
  377. return;
  378. }
  379. if (item.type == 'wx') {
  380. _shareByMob(ShareSDKPlatforms.wechatSession);
  381. } else if (item.type == 'pyq') {
  382. _shareByMob(ShareSDKPlatforms.wechatTimeline);
  383. } else if (item.type == 'qq') {
  384. _shareByMob(ShareSDKPlatforms.qq);
  385. } else if (item.type == 'qq_space') {
  386. _shareByMob(ShareSDKPlatforms.qZone);
  387. } else if (item.type == 'weibo') {
  388. _shareByMob(ShareSDKPlatforms.sina);
  389. } else if (item.type == 'more_setting') {
  390. _shareBySystem(item.type);
  391. }
  392. });
  393. }
  394. // mob分享,只能单图分享,多图分享调用系统分享
  395. void _shareByMob(ShareSDKPlatform plateform) async {
  396. if (isSelectPic && EmptyUtil.isEmpty(_shareDataModel) && EmptyUtil.isEmpty(_shareDataModel?.poster) && EmptyUtil.isEmpty(_shareDataModel?.image)) {
  397. Fluttertoast.showToast(msg: '请选择分享图片');
  398. return;
  399. }
  400. Loading.show(context);
  401. Timer(Duration(milliseconds: 2000), () {
  402. Loading.dismiss();
  403. });
  404. SSDKMap params;
  405. if (_shareDataModel.poster != null) {
  406. String path = await _savePoster();
  407. if (path != null && path != '') {
  408. params = SSDKMap()
  409. ..setGeneral(
  410. _shareDataModel?.title ?? '',
  411. _shareDataModel?.content ?? '',
  412. Platform.isIOS ? path : null,
  413. null,
  414. Platform.isAndroid ? path : null,
  415. null,
  416. null,
  417. null,
  418. null,
  419. null,
  420. SSDKContentTypes.image,
  421. );
  422. }
  423. } else {
  424. var type = SSDKContentTypes.auto;
  425. if (_shareDataModel?.image?.first != null && _shareDataModel?.url != null) {
  426. type = SSDKContentTypes.webpage;
  427. } else if (_shareDataModel?.image?.first != null) {
  428. type = SSDKContentTypes.image;
  429. } else if (_shareDataModel?.title != null || _shareDataModel?.content != null) {
  430. type = SSDKContentTypes.text;
  431. }
  432. if (plateform == ShareSDKPlatforms.qZone && type == SSDKContentTypes.text) {
  433. _shareDataModel?.title = null;
  434. type = SSDKContentTypes.message;
  435. }
  436. params = SSDKMap()
  437. ..setGeneral(
  438. _shareDataModel?.title ?? '',
  439. _shareDataModel?.content ?? '',
  440. Platform.isIOS ? _shareDataModel.image : null,
  441. Platform.isAndroid ? _shareDataModel?.image?.first : null,
  442. null,
  443. _shareDataModel.url,
  444. null,
  445. null,
  446. null,
  447. null,
  448. type,
  449. );
  450. }
  451. SharesdkPlugin.share(plateform, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {
  452. print(error);
  453. if (state == SSDKResponseState.Fail) {
  454. Fluttertoast.showToast(msg: '分享失败');
  455. } else if (state == SSDKResponseState.Success) {
  456. //Fluttertoast.showToast(msg: '分享成功');
  457. } else if (state == SSDKResponseState.Cancel) {
  458. Fluttertoast.showToast(msg: '取消分享');
  459. }
  460. Logger.debug('${state}, ${error.rawData}');
  461. Loading.dismiss();
  462. });
  463. }
  464. // 系统分享,只能分享图片或者文字,不能组合分享
  465. void _shareBySystem(String type) async {
  466. if (isSelectPic && EmptyUtil.isEmpty(_shareDataModel) && EmptyUtil.isEmpty(_shareDataModel?.poster) && EmptyUtil.isEmpty(_shareDataModel?.image)) {
  467. Fluttertoast.showToast(msg: '请选择分享图片');
  468. return;
  469. }
  470. int count = 0;
  471. if (_shareDataModel.poster != null) {
  472. count++;
  473. }
  474. count += (_shareDataModel?.image?.length ?? 0);
  475. // 多图分享
  476. if (count > 1) {
  477. _shareMultipleImages(type);
  478. return;
  479. }
  480. if (_shareDataModel.poster != null) {
  481. String path = await _savePoster();
  482. if (path != null && path != '') {
  483. ShareExtend.share(path, 'image');
  484. }
  485. } else {
  486. ShareExtend.share(_shareDataModel.content, 'text');
  487. }
  488. }
  489. Future<String> _savePoster() async {
  490. String path;
  491. if (_shareDataModel.poster != null) {
  492. // 检查并请求权限
  493. var status = await Permission.storage.status;
  494. if (status != PermissionStatus.granted) {
  495. status = await Permission.storage.request();
  496. }
  497. if (status == PermissionStatus.denied) {
  498. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  499. return null;
  500. }
  501. try {
  502. // 保存到本地路径await getTemporaryDirectory();
  503. // final tempDir =
  504. final tempDir = await getApplicationSupportDirectory();
  505. final file = await File('${tempDir.path}/image.jpg').create();
  506. file.writeAsBytesSync(_shareDataModel.poster);
  507. path = file.path;
  508. Logger.debug(file.path);
  509. } catch (err, s) {
  510. Logger.error(err.toString(), s.toString());
  511. Fluttertoast.showToast(msg: '分享失败');
  512. return null;
  513. }
  514. }
  515. return path;
  516. }
  517. // 多图分享,调用系统分享
  518. void _shareMultipleImages(String type) async {
  519. List<String> paths = List();
  520. String path = await _savePoster();
  521. if (path != null && path != '') {
  522. paths.add(path);
  523. }
  524. Loading.show(context);
  525. List<String> downPaths = await ImageDownloadUtil.download(_shareDataModel.image);
  526. paths.addAll(downPaths);
  527. if (Platform.isAndroid && type == 'wx') {
  528. MorePictureShare.shareWeixinPics(paths);
  529. } else if (Platform.isAndroid && type == 'pyq') {
  530. MorePictureShare.shareWeixinPicsCirlc(paths);
  531. } else {
  532. ShareExtend.shareMultiple(paths, "image", subject: "");
  533. }
  534. Loading.dismiss();
  535. }
  536. }