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

share_alert_select.dart 19 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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: 1);
  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. //检查是否有存储权限
  350. var status = await Permission.storage.status;
  351. if (!status.isGranted) {
  352. status = await Permission.storage.request();
  353. print(status);
  354. return;
  355. }
  356. int count = 0;
  357. if (_shareDataModel.poster != null) {
  358. count++;
  359. }
  360. count += (_shareDataModel?.image?.length ?? 0);
  361. // 多图分享
  362. if (count > 1) {
  363. _shareMultipleImages(item.type);
  364. return;
  365. }
  366. if (item.type == 'wx') {
  367. _shareByMob(ShareSDKPlatforms.wechatSession);
  368. } else if (item.type == 'pyq') {
  369. _shareByMob(ShareSDKPlatforms.wechatTimeline);
  370. } else if (item.type == 'qq') {
  371. _shareByMob(ShareSDKPlatforms.qq);
  372. } else if (item.type == 'qq_space') {
  373. _shareByMob(ShareSDKPlatforms.qZone);
  374. } else if (item.type == 'weibo') {
  375. _shareByMob(ShareSDKPlatforms.sina);
  376. } else if (item.type == 'more_setting') {
  377. _shareBySystem(item.type);
  378. }
  379. },
  380. );
  381. }
  382. // mob分享,只能单图分享,多图分享调用系统分享
  383. void _shareByMob(ShareSDKPlatform plateform) async {
  384. if (isSelectPic &&
  385. EmptyUtil.isEmpty(_shareDataModel) &&
  386. EmptyUtil.isEmpty(_shareDataModel?.poster) &&
  387. EmptyUtil.isEmpty(_shareDataModel?.image)) {
  388. Fluttertoast.showToast(msg: '请选择分享图片');
  389. return;
  390. }
  391. Loading.show(context);
  392. Timer(Duration(milliseconds: 2000), () {
  393. Loading.dismiss();
  394. });
  395. SSDKMap params;
  396. if (_shareDataModel.poster != null) {
  397. String path = await _savePoster();
  398. if (path != null && path != '') {
  399. params = SSDKMap()
  400. ..setGeneral(
  401. _shareDataModel?.title ?? '',
  402. _shareDataModel?.content ?? '',
  403. Platform.isIOS ? path : null,
  404. null,
  405. Platform.isAndroid ? path : null,
  406. null,
  407. null,
  408. null,
  409. null,
  410. null,
  411. SSDKContentTypes.image,
  412. );
  413. }
  414. } else {
  415. var type = SSDKContentTypes.auto;
  416. if (_shareDataModel?.image?.first != null && _shareDataModel?.url != null) {
  417. type = SSDKContentTypes.webpage;
  418. } else if (_shareDataModel?.image?.first != null) {
  419. type = SSDKContentTypes.image;
  420. } else if (_shareDataModel?.title != null || _shareDataModel?.content != null) {
  421. type = SSDKContentTypes.text;
  422. }
  423. if (plateform == ShareSDKPlatforms.qZone && type == SSDKContentTypes.text) {
  424. _shareDataModel?.title = null;
  425. type = SSDKContentTypes.message;
  426. }
  427. params = SSDKMap()
  428. ..setGeneral(
  429. _shareDataModel?.title ?? '',
  430. _shareDataModel?.content ?? '',
  431. Platform.isIOS ? _shareDataModel.image : null,
  432. Platform.isAndroid ? _shareDataModel?.image?.first : null,
  433. null,
  434. _shareDataModel.url,
  435. null,
  436. null,
  437. null,
  438. null,
  439. type,
  440. );
  441. }
  442. SharesdkPlugin.share(plateform, params, (SSDKResponseState state, Map userdata, Map contentEntity, SSDKError error) {
  443. print(error);
  444. if (state == SSDKResponseState.Fail) {
  445. Fluttertoast.showToast(msg: '分享失败');
  446. } else if (state == SSDKResponseState.Success) {
  447. //Fluttertoast.showToast(msg: '分享成功');
  448. } else if (state == SSDKResponseState.Cancel) {
  449. Fluttertoast.showToast(msg: '取消分享');
  450. }
  451. Logger.debug('${state}, ${error.rawData}');
  452. Loading.dismiss();
  453. });
  454. }
  455. // 系统分享,只能分享图片或者文字,不能组合分享
  456. void _shareBySystem(String type) async {
  457. if (isSelectPic &&
  458. EmptyUtil.isEmpty(_shareDataModel) &&
  459. EmptyUtil.isEmpty(_shareDataModel?.poster) &&
  460. EmptyUtil.isEmpty(_shareDataModel?.image)) {
  461. Fluttertoast.showToast(msg: '请选择分享图片');
  462. return;
  463. }
  464. int count = 0;
  465. if (_shareDataModel.poster != null) {
  466. count++;
  467. }
  468. count += (_shareDataModel?.image?.length ?? 0);
  469. // 多图分享
  470. if (count > 1) {
  471. _shareMultipleImages(type);
  472. return;
  473. }
  474. if (_shareDataModel.poster != null) {
  475. String path = await _savePoster();
  476. if (path != null && path != '') {
  477. ShareExtend.share(path, 'image');
  478. }
  479. } else {
  480. ShareExtend.share(_shareDataModel.content, 'text');
  481. }
  482. }
  483. Future<String> _savePoster() async {
  484. String path;
  485. if (_shareDataModel.poster != null) {
  486. // 检查并请求权限
  487. var status = await Permission.storage.status;
  488. if (status != PermissionStatus.granted) {
  489. status = await Permission.storage.request();
  490. }
  491. if (status == PermissionStatus.denied) {
  492. Fluttertoast.showToast(msg: '暂无权限,分享失败');
  493. return null;
  494. }
  495. try {
  496. // 保存到本地路径
  497. final tempDir = await getTemporaryDirectory();
  498. final file = await File('${tempDir.path}/image.jpg').create();
  499. file.writeAsBytesSync(_shareDataModel.poster);
  500. path = file.path;
  501. Logger.debug(file.path);
  502. } catch (err, s) {
  503. Logger.error(err.toString(), s.toString());
  504. Fluttertoast.showToast(msg: '分享失败');
  505. return null;
  506. }
  507. }
  508. return path;
  509. }
  510. // 多图分享,调用系统分享
  511. void _shareMultipleImages(String type) async {
  512. List<String> paths = List();
  513. String path = await _savePoster();
  514. if (path != null && path != '') {
  515. paths.add(path);
  516. }
  517. Loading.show(context);
  518. List<String> downPaths = await ImageDownloadUtil.download(_shareDataModel.image);
  519. paths.addAll(downPaths);
  520. if (Platform.isAndroid && type == 'wx') {
  521. MorePictureShare.shareWeixinPics(paths);
  522. } else if (Platform.isAndroid && type == 'pyq') {
  523. MorePictureShare.shareWeixinPicsCirlc(paths);
  524. } else {
  525. ShareExtend.shareMultiple(paths, "image", subject: "");
  526. }
  527. Loading.dismiss();
  528. }
  529. }