MyCode.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import 'dart:ui';
  2. import 'dart:typed_data';
  3. import 'dart:async';
  4. import 'dart:ui' as ui;
  5. import 'dart:io';
  6. import 'package:flutter/material.dart';
  7. import 'package:flutter/rendering.dart';
  8. import 'package:permission_handler/permission_handler.dart';
  9. import 'package:image_picker_saver/image_picker_saver.dart';
  10. import 'package:qr_flutter/qr_flutter.dart';
  11. import 'package:redux/redux.dart';
  12. import 'package:flutter_redux/flutter_redux.dart';
  13. import 'package:path_provider/path_provider.dart';
  14. import 'package:wanna_battle/widget/Dialog.dart';
  15. import '../redux/AppState.dart';
  16. import '../model/UserInfo.dart';
  17. import '../widget/LinearButton.dart';
  18. import '../net/HttpManager.dart';
  19. import '../styles/totast.dart';
  20. import '../net/Result.dart';
  21. import '../model/ChannelInfo.dart';
  22. class MyCode extends StatefulWidget {
  23. @override
  24. MyCodeState createState() => MyCodeState();
  25. }
  26. class MyCodeState extends State<MyCode> {
  27. ChannelInfo channelInfo = null;
  28. num race = 10;
  29. GlobalKey globalKey = new GlobalKey();
  30. // 截图boundary,并且返回图片的二进制数据。
  31. Future<Uint8List> _capturePng() async {
  32. RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
  33. ui.Image image = await boundary.toImage();
  34. // 注意:png是压缩后格式,如果需要图片的原始像素数据,请使用rawRgba
  35. ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  36. Uint8List pngBytes = byteData.buffer.asUint8List();
  37. return pngBytes;
  38. }
  39. Future<String> _capturePath(String name) async {
  40. Directory documentsDirectory = await getExternalStorageDirectory();
  41. String path = documentsDirectory.path + name;
  42. return path;
  43. }
  44. Future<void> getChannelInfo() async {
  45. Toast.show(context, '加载中', -1, 'loading');
  46. Result res = await HttpManager.get('channelInfo/getOne', data: {'id': StoreProvider.of<AppState>(context).state.userInfo.channel});
  47. Toast.hide();
  48. if (res.success && res.data != null) {
  49. setState(() {
  50. channelInfo = ChannelInfo.fromJson(res.data);
  51. });
  52. }
  53. Result res2 = await HttpManager.get('systemVariable/get', data: {'name': "ticketPrice"});
  54. if (res2.success) {
  55. setState(() {
  56. race = double.parse( res2.data);
  57. });
  58. }
  59. }
  60. @override
  61. void initState() {
  62. super.initState();
  63. Future.delayed(Duration.zero, () => getChannelInfo());
  64. }
  65. @override
  66. Widget build(BuildContext context) {
  67. AppBar appbar = AppBar(title: Text('我的推广码'), centerTitle: true);
  68. double _height = MediaQuery.of(context).size.height - MediaQueryData.fromWindow(window).padding.top - appbar.preferredSize.height;
  69. return StoreConnector<AppState, UserInfo>(
  70. converter: (Store store) => store.state.userInfo,
  71. builder: (context, userInfo) {
  72. String qrstr = HttpManager.baseUrl + "share?userId=${userInfo.id}&channel=${userInfo.channel}";
  73. // final cryptor = new PlatformStringCryptor();
  74. return WillPopScope(
  75. onWillPop: () {
  76. Navigator.of(context).pop();
  77. Toast.hide();
  78. },
  79. child: Scaffold(
  80. appBar: appbar,
  81. body: Container(
  82. color: Color(0xFF2B2B42),
  83. height: double.infinity,
  84. width: double.infinity,
  85. child: SingleChildScrollView(
  86. child: Container(
  87. height: _height - 60,
  88. constraints: BoxConstraints(minHeight: 543),
  89. margin: EdgeInsets.symmetric(vertical: 30, horizontal: 30),
  90. color: Color(0xFF363759),
  91. child: Column(
  92. mainAxisAlignment: MainAxisAlignment.center,
  93. children: <Widget>[
  94. RepaintBoundary(
  95. key: globalKey,
  96. child: Container(
  97. width: 256,
  98. color: Color(0xFF363759),
  99. child: Column(
  100. children: <Widget>[
  101. Container(height: 36),
  102. Image.asset('images/text_yonghuduan.png'),
  103. Container(
  104. height: 7,
  105. ),
  106. Text('扫描二维码加入 ${userInfo.nickname.length <= 5 ? userInfo.nickname : userInfo.nickname.substring(0, 4) + "..."} 的电竞团队',
  107. style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 14, fontWeight: FontWeight.w600)),
  108. Container(height: 25),
  109. Container(
  110. width: 150,
  111. height: 150,
  112. color: Colors.white,
  113. // padding: EdgeInsets.all(7),
  114. child: QrImage(
  115. data: qrstr,
  116. size: 150.0,
  117. ),
  118. ),
  119. Container(
  120. height: 10,
  121. ),
  122. Text(
  123. '扫一扫下载APP',
  124. style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w500),
  125. ),
  126. Container(
  127. height: 30,
  128. )
  129. ],
  130. ),
  131. ),
  132. ),
  133. // Text(
  134. // channelInfo != null ? '一级队员:每购买一张就会获得${1 * 10 * channelInfo.ratio}' : '',
  135. // style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w500),
  136. // ),
  137. // Text(
  138. // channelInfo != null ? '二级队员:每购买一张就会获得${1 * 10 * channelInfo.ratio2}' : '',
  139. // style: TextStyle(color: Colors.white, fontSize: 14, fontWeight: FontWeight.w500),
  140. // ),
  141. Container(
  142. // margin: EdgeInsets.only(top: 30),
  143. width: 180,
  144. child: LinearButton(
  145. btntext: '立刻邀请',
  146. btnHeight: 44.0,
  147. colorList: [Color(0xFFD4504B), Color(0xFFD4504B)],
  148. onTapHomeMenu: () async {
  149. var permission = PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
  150. await PermissionHandler().requestPermissions(<PermissionGroup>[
  151. PermissionGroup.storage, // 在这里添加需要的权限
  152. ]);
  153. // MyDialog.showDialog(context, '手动截图立即分享二维码给小伙伴吧!');
  154. Uint8List val = await _capturePng();
  155. print(val);
  156. final result = await ImagePickerSaver.saveFile(fileData: val);
  157. print(result);
  158. // Toast.show(context, '保存成功', 1500, 'success');
  159. MyDialog.showDialog(context, '分享二维码保存成功,请在照片图库中找到并分享给其他人吧。');
  160. },
  161. ),
  162. ),
  163. Container(
  164. height: 13,
  165. ),
  166. InkWell(
  167. onTap: () {
  168. if (channelInfo == null) {
  169. MyDialog.showDialog(context, '用户渠道信息为空,暂不能参与活动');
  170. } else {
  171. Navigator.of(context).push(PageRouteBuilder(
  172. opaque: false,
  173. transitionDuration: Duration(milliseconds: 300),
  174. transitionsBuilder:
  175. (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  176. return FadeTransition(
  177. opacity: CurvedAnimation(parent: animation, curve: Curves.linear),
  178. child: child,
  179. );
  180. },
  181. pageBuilder: (BuildContext context, _, __) {
  182. return Rule(race * channelInfo.ratio / 100, race * channelInfo.ratio2 / 100);
  183. }));
  184. }
  185. },
  186. child: Row(
  187. mainAxisAlignment: MainAxisAlignment.center,
  188. children: <Widget>[
  189. Text(
  190. '查看推广奖励规则',
  191. style: TextStyle(color: Theme.of(context).primaryColor),
  192. ),
  193. Image.asset('images/icon_inter_hongse.png')
  194. ],
  195. ),
  196. ),
  197. Expanded(
  198. flex: 1,
  199. child: Container(),
  200. ),
  201. Image.asset('images/img_0000.png'),
  202. ],
  203. ),
  204. ),
  205. ),
  206. )));
  207. });
  208. }
  209. }
  210. class Rule extends Dialog {
  211. Rule(this.ratio, this.ratio2);
  212. double ratio;
  213. double ratio2;
  214. @override
  215. Widget build(BuildContext context) {
  216. return WillPopScope(
  217. child: Scaffold(
  218. backgroundColor: Color(0xCC000000),
  219. body: Container(
  220. child: SizedBox.expand(
  221. child: UnconstrainedBox(
  222. child: Container(
  223. width: 280,
  224. // padding: EdgeInsets.symmetric(horizontal: 20, vertical: 25),
  225. decoration: BoxDecoration(color: Color(0xFF15151D), border: Border.all(width: 1, color: Theme.of(context).primaryColor)),
  226. child: Stack(
  227. children: <Widget>[
  228. Padding(
  229. padding: EdgeInsets.symmetric(horizontal: 16, vertical: 25),
  230. child: Column(
  231. mainAxisAlignment: MainAxisAlignment.center,
  232. children: <Widget>[
  233. Text('推广奖励规则', style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w600)),
  234. Container(
  235. height: 20,
  236. ),
  237. Text('1、扫描该二维码下载APP的用户成为您的一级成员,可以在“我的团队”中查看 ', style: TextStyle(color: Colors.white, fontSize: 14)),
  238. Container(
  239. height: 20,
  240. ),
  241. Text('2、团队一级成员在APP中每购买一张参赛券,您就可以获得${ratio}个积分 ', style: TextStyle(color: Colors.white, fontSize: 14)),
  242. Container(
  243. height: 20,
  244. ),
  245. Text('3、团队二级成员在APP中每购买一张参赛券,您就可以获得${ratio2}个积分 ', style: TextStyle(color: Colors.white, fontSize: 14)),
  246. Container(
  247. height: 35,
  248. ),
  249. Container(
  250. width: double.infinity,
  251. child: LinearButton(
  252. btntext: '确认',
  253. btnHeight: 36.0,
  254. onTapHomeMenu: () {
  255. Navigator.of(context).pop();
  256. },
  257. )),
  258. Container(
  259. height: 5,
  260. )
  261. ],
  262. ),
  263. ),
  264. Positioned(
  265. top: 0,
  266. left: 0,
  267. child: Image.asset(
  268. 'images/tancuang_shang.png',
  269. // width: 131,
  270. ),
  271. ),
  272. Positioned(
  273. bottom: 0,
  274. right: 4,
  275. child: Image.asset(
  276. 'images/tancuang_xia.png',
  277. // width: 148,
  278. ),
  279. ),
  280. ],
  281. ),
  282. ),
  283. ),
  284. ),
  285. ),
  286. ),
  287. onWillPop: () {
  288. return Future.value(false);
  289. },
  290. );
  291. }
  292. }