share.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_svg/svg.dart';
  5. import 'package:link/utils/global.dart';
  6. import 'package:link/view/component/web_image.dart';
  7. import 'package:screenshot/screenshot.dart';
  8. import 'dart:js' as js;
  9. import '../utils/size_fit.dart';
  10. class Share extends StatefulWidget {
  11. Uint8List byteData;
  12. Share({Key? key, required this.byteData}) : super(key: key);
  13. @override
  14. State<Share> createState() => _ShareState();
  15. }
  16. class _ShareState extends State<Share> {
  17. bool showShareGuide = false;
  18. bool isWechatBrowser = false; //false;
  19. ScreenshotController screenshotController = ScreenshotController();
  20. Uint8List? imgData;
  21. @override
  22. initState() {
  23. // var ua = html.window.navigator.userAgent.toLowerCase();
  24. getImage();
  25. getPlatform();
  26. super.initState();
  27. }
  28. getImage() async {
  29. screenshotController
  30. .captureFromWidget(SizedBox(
  31. width: MediaQuery.of(context).size.width,
  32. height: MediaQuery.of(context).size.width,
  33. child: SingleChildScrollView(
  34. physics: const NeverScrollableScrollPhysics(),
  35. child: Container(
  36. transform: Matrix4.translationValues(0, -72.px, 0),
  37. child: Image.memory(widget.byteData),
  38. ),
  39. ),
  40. ),pixelRatio: 0.6)
  41. .then((value) {
  42. imgData = value;
  43. });
  44. }
  45. getPlatform() async {
  46. bool result = js.context
  47. .callMethod('isWechatBrowser'); //await DdJsUtil.isWeChatBrowser;
  48. setState(() {
  49. isWechatBrowser = result;
  50. });
  51. // js.context.callMethod('shareSys');
  52. }
  53. share() {
  54. setState(() {
  55. showShareGuide = true;
  56. });
  57. String url = "data:image/png;base64," +
  58. base64Encode(imgData != null ? imgData! : widget.byteData);
  59. js.context.callMethod('shareSys', [url, '?u=' + Global().uid]);
  60. }
  61. copy() {}
  62. @override
  63. Widget build(BuildContext context) {
  64. SizeFit.initialize(context);
  65. String url = "data:image/png;base64," + base64Encode(widget.byteData);
  66. // js.context.callMethod('shareSys');
  67. return Material(
  68. color: Colors.transparent,
  69. child: Stack(
  70. children: [
  71. Positioned(
  72. top: 0,
  73. right: 0,
  74. bottom: 0,
  75. left: 0,
  76. child: GestureDetector(
  77. onTap: () {
  78. Navigator.of(context).pop();
  79. },
  80. child: Container(
  81. width: MediaQuery.of(context).size.width,
  82. height: MediaQuery.of(context).size.height,
  83. color: Colors.transparent,
  84. ),
  85. )),
  86. if (showShareGuide && isWechatBrowser)
  87. Positioned(
  88. right: 21.px,
  89. top: 12.px,
  90. child: Image.asset(
  91. 'assets/images/share_arrow_up.png',
  92. width: 40.px,
  93. height: 46.px,
  94. )),
  95. if (showShareGuide && isWechatBrowser)
  96. Positioned(
  97. right: 81.px,
  98. top: 40.px,
  99. child: Text(
  100. '点击…打开分享菜\n单,选择你要分享\n的好友',
  101. style: TextStyle(
  102. color: Colors.white,
  103. fontSize: 20.px,
  104. fontWeight: FontWeight.bold,
  105. height: 1.8),
  106. )),
  107. if (showShareGuide && !isWechatBrowser)
  108. Positioned(
  109. left: 156.px,
  110. bottom: 46.px,
  111. child: Image.asset(
  112. 'assets/images/share_arrow_down.png',
  113. width: 40.px,
  114. height: 46.px,
  115. )),
  116. if (showShareGuide && !isWechatBrowser)
  117. Positioned(
  118. left: 0,
  119. right: 0,
  120. bottom: 112.px,
  121. child: Text(
  122. '点击分享菜单,选\n择你要分享的平台',
  123. textAlign: TextAlign.center,
  124. style: TextStyle(
  125. color: Colors.white,
  126. fontSize: 20.px,
  127. fontWeight: FontWeight.bold,
  128. height: 1.8),
  129. )),
  130. if (!showShareGuide)
  131. Positioned(
  132. right: 24.px,
  133. top: 24.px,
  134. child: GestureDetector(
  135. onTap: () {
  136. Navigator.of(context).pop();
  137. },
  138. child: SvgPicture.asset(
  139. 'assets/icons/close.svg',
  140. width: 24.px,
  141. height: 24.px,
  142. ),
  143. )),
  144. if (!showShareGuide)
  145. Column(
  146. mainAxisAlignment: MainAxisAlignment.center,
  147. crossAxisAlignment: CrossAxisAlignment.center,
  148. children: [
  149. Container(
  150. width: 200.px,
  151. height: MediaQuery.of(context).size.height / (375 / 200.0),
  152. child: WebImage(
  153. url: url,
  154. width: 200.px,
  155. height:
  156. MediaQuery.of(context).size.height / (375 / 200.0),
  157. )
  158. // Image.memory(
  159. // widget.byteData,
  160. // width: 200.px,
  161. // height: MediaQuery.of(context).size.height / (375 / 200.0),
  162. // fit: BoxFit.cover,
  163. // ),
  164. ),
  165. SizedBox(
  166. height: 53.px,
  167. ),
  168. Row(
  169. mainAxisAlignment: MainAxisAlignment.center,
  170. crossAxisAlignment: CrossAxisAlignment.center,
  171. children: [
  172. GestureDetector(
  173. onTap: () {
  174. share();
  175. },
  176. child: Column(
  177. children: [
  178. Image.asset(
  179. 'assets/images/share_weibo.png',
  180. width: 44.px,
  181. height: 44.px,
  182. ),
  183. SizedBox(
  184. height: 11.px,
  185. ),
  186. Text(
  187. '微博',
  188. style:
  189. TextStyle(color: Colors.white, fontSize: 11.px),
  190. )
  191. ],
  192. ),
  193. ),
  194. SizedBox(
  195. width: 24.px,
  196. ),
  197. GestureDetector(
  198. onTap: () {
  199. share();
  200. },
  201. child: Column(
  202. children: [
  203. Image.asset(
  204. 'assets/images/share_wechat.png',
  205. width: 44.px,
  206. height: 44.px,
  207. ),
  208. SizedBox(
  209. height: 11.px,
  210. ),
  211. Text(
  212. '微信',
  213. style:
  214. TextStyle(color: Colors.white, fontSize: 11.px),
  215. )
  216. ],
  217. ),
  218. ),
  219. SizedBox(
  220. width: 24.px,
  221. ),
  222. GestureDetector(
  223. onTap: () {
  224. share();
  225. },
  226. child: Column(
  227. children: [
  228. Image.asset(
  229. 'assets/images/share_qq.png',
  230. width: 44.px,
  231. height: 44.px,
  232. ),
  233. SizedBox(
  234. height: 11.px,
  235. ),
  236. Text(
  237. 'QQ',
  238. style:
  239. TextStyle(color: Colors.white, fontSize: 11.px),
  240. )
  241. ],
  242. ),
  243. ),
  244. SizedBox(
  245. width: 24.px,
  246. ),
  247. GestureDetector(
  248. onTap: () {
  249. copy();
  250. },
  251. child: Column(
  252. children: [
  253. Image.asset(
  254. 'assets/images/share_link.png',
  255. width: 44.px,
  256. height: 44.px,
  257. ),
  258. SizedBox(
  259. height: 11.px,
  260. ),
  261. Text(
  262. '复制链接',
  263. style:
  264. TextStyle(color: Colors.white, fontSize: 11.px),
  265. )
  266. ],
  267. ),
  268. ),
  269. ],
  270. )
  271. ],
  272. ),
  273. ],
  274. ),
  275. );
  276. }
  277. }