基础组件库
 
 
 
 
 

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