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

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