invite_code.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import 'package:dio/dio.dart';
  2. import 'package:fast/constants.dart';
  3. import 'package:fast/utils/api.dart';
  4. import 'package:fast/utils/http_utils.dart';
  5. import 'package:fast/utils/size_fit.dart';
  6. import 'package:fast/view/index.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:get/get.dart';
  9. import 'component/fast_btn.dart';
  10. import 'component/header.dart';
  11. import 'component/toast.dart';
  12. class InviteCode extends StatefulWidget {
  13. const InviteCode({Key? key}) : super(key: key);
  14. @override
  15. State<InviteCode> createState() => _InviteCodeState();
  16. }
  17. class _InviteCodeState extends State<InviteCode> {
  18. String code = '';
  19. bool showError = false;
  20. @override
  21. Widget build(BuildContext context) {
  22. SizeFit.initialize(context);
  23. return Material(
  24. child: Container(
  25. color: kBgColor,
  26. child: Column(children: [
  27. Header(
  28. isIndexPage: false,
  29. ),
  30. SizedBox(
  31. height: 50.px,
  32. ),
  33. Opacity(
  34. opacity: showError ? 1 : 0,
  35. child: Text(
  36. '输入的邀请码错误,请重新输入',
  37. style: TextStyle(color: kThemeColor, fontSize: 12.px),
  38. ),
  39. ),
  40. SizedBox(
  41. height: 10.px,
  42. ),
  43. Stack(
  44. children: [
  45. Container(
  46. width: 311.px,
  47. height: 48.px,
  48. alignment: Alignment.center,
  49. decoration: BoxDecoration(
  50. color: const Color(0x26C4CCDA),
  51. borderRadius:
  52. BorderRadius.all(Radius.circular(16.px))),
  53. child: Opacity(
  54. opacity: code.isEmpty ? 1 : 0,
  55. child: Row(
  56. mainAxisAlignment: MainAxisAlignment.center,
  57. children: [
  58. Text(
  59. '请输入邀请码,你和朋友各得',
  60. style: TextStyle(
  61. color: const Color(0x66FFFFFF),
  62. fontSize: 16.px),
  63. ),
  64. SizedBox(
  65. width: 3.px,
  66. ),
  67. Text(
  68. '5',
  69. style: TextStyle(
  70. color: const Color(0x99FFFFFF),
  71. fontSize: 24.px,
  72. fontFamily: 'Exo2',
  73. fontWeight: FontWeight.w600),
  74. ),
  75. SizedBox(
  76. width: 3.px,
  77. ),
  78. Image.asset(
  79. 'assets/images/stone.png',
  80. width: 24.px,
  81. height: 24.px,
  82. )
  83. ],
  84. ),
  85. )),
  86. SizedBox(
  87. width: 311.px,
  88. height: 48.px,
  89. child: TextField(
  90. onChanged: (value) {
  91. setState(() {
  92. code = value;
  93. });
  94. },
  95. style: TextStyle(
  96. color: Colors.white,
  97. fontSize: 24.px,
  98. fontFamily: 'Exo2',
  99. fontWeight: FontWeight.w600),
  100. cursorColor: Colors.white,
  101. maxLength: 4,
  102. autofocus: true,
  103. textAlign: TextAlign.center,
  104. decoration: const InputDecoration(
  105. counterText: "",
  106. border: InputBorder.none,
  107. contentPadding: EdgeInsets.zero,
  108. hintStyle: TextStyle(color: Color(0x66FFFFFF))),
  109. ))
  110. ],
  111. ),
  112. SizedBox(
  113. height: 28.px,
  114. ),
  115. FastBtn(
  116. title: '提交',
  117. disable: false,
  118. // color: const Color(0x80AAFF00),
  119. width: 311.px,
  120. height: 48.px,
  121. callback: () {
  122. invite();
  123. }),
  124. SizedBox(
  125. height: 30.px,
  126. ),
  127. GestureDetector(
  128. onTap: () {
  129. jump();
  130. },
  131. child: Text(
  132. '无邀请码,跳过',
  133. style: TextStyle(
  134. color: const Color(0x99C4CCDA), fontSize: 14.px),
  135. ))
  136. ])));
  137. }
  138. Future invite() async {
  139. try {
  140. Map<String, dynamic> data = await HttpUtils.post(Api.inviteCode,
  141. data: {'skip': false, 'invite_code': code});
  142. showToast();
  143. Get.offAll(const IndexScreen());
  144. // Navigator.of(context).pop();
  145. } on DioError catch (e) {
  146. if (e.response?.data['error_code'] == 'INVITE_CODE_NOT_EXIST') {
  147. setState(() {
  148. showError = true;
  149. });
  150. }
  151. }
  152. }
  153. showToast() {
  154. showDialog(
  155. context: context,
  156. barrierDismissible: false,
  157. barrierColor: Colors.transparent,
  158. builder: (BuildContext context) {
  159. return Toast(
  160. title: '成功受邀',
  161. content: Row(
  162. mainAxisAlignment: MainAxisAlignment.center,
  163. children: [
  164. Text(
  165. '+3',
  166. style: TextStyle(
  167. color: Colors.white,
  168. fontSize: 16.px,
  169. fontWeight: FontWeight.w800,
  170. fontFamily: 'Exo2',
  171. decoration: TextDecoration.none),
  172. ),
  173. SizedBox(
  174. width: 3.px,
  175. ),
  176. Image.asset(
  177. 'assets/images/stone.png',
  178. width: 24.px,
  179. height: 24.px,
  180. )
  181. ],
  182. ),
  183. );
  184. });
  185. }
  186. Future jump() async {
  187. var data = await HttpUtils.post(Api.inviteCode, data: {'skip': true});
  188. Get.offAll(const IndexScreen());
  189. }
  190. }