基础组件库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

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