基础组件库
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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