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

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