sign_up2_view.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import 'dart:async';
  2. import 'package:flutter/gestures.dart';
  3. import 'package:get/get.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:grpc/grpc.dart';
  6. import 'package:trackoffical_app/screen.dart';
  7. import 'package:trackoffical_app/service/api.dart';
  8. import 'common.dart';
  9. import '../../route.dart';
  10. import 'package:trackoffical_app/pb.dart' as pb;
  11. class SignUpController extends GetxController {
  12. var phone = '';
  13. var code = '';
  14. String name = '' ;
  15. final sex = pb.User_Sex.Male.obs;
  16. final codeRetryLeft = 0.seconds.obs;
  17. Timer? ticker;
  18. final isSignUpEnable = true.obs;
  19. @override
  20. void onInit() {
  21. super.onInit();
  22. ticker = Timer.periodic(1.seconds, (timer) {
  23. codeRetryLeft.value -= 1.seconds;
  24. });
  25. }
  26. onGetCode() async{
  27. try{
  28. await ApiService.to.authSendCodeToPhone(phone, pb.SmsType.SignUp);
  29. codeRetryLeft.value = await ApiService.to.getSmsSendLeftTime(phone);
  30. }on GrpcError catch(e){
  31. if (e.code== StatusCode.alreadyExists){
  32. Get.snackbar('已注册', '请直接登录');
  33. }else{
  34. Get.snackbar('发送失败', e.message??'');
  35. }
  36. }catch(e){
  37. Get.snackbar('发送失败', e.toString());
  38. }
  39. }
  40. void onSignUp() async{
  41. if(name.isEmpty){
  42. Get.snackbar('请输入昵称', '不能为空');
  43. return;
  44. }
  45. if(isSignUpEnable.value){
  46. isSignUpEnable.value = false;
  47. try{
  48. await ApiService.to.signUp(phone, code, name, sex.value);
  49. await ApiService.to.flushUserInfo();
  50. Get.offAllNamed(RouteName.homeSaveUserInfo);
  51. } on GrpcError catch(e){
  52. if (e.code== StatusCode.unauthenticated){
  53. Get.snackbar('验证码错误', '请重新输入');
  54. }else{
  55. Get.snackbar('注册失败', e.message??'');
  56. }
  57. }catch(e){
  58. Get.snackbar('注册失败', e.toString());
  59. }finally {
  60. isSignUpEnable.value = true;
  61. }
  62. }
  63. }
  64. }
  65. class SignUpView extends GetView<SignUpController> {
  66. const SignUpView({super.key});
  67. static void show(){
  68. Get.to(const SignUpView(), binding: BindingsBuilder(() {
  69. Get.put(SignUpController());
  70. }));
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. return Scaffold(
  75. appBar: AppTopBar(title: '注册账号', hasBackText: true, height: context.height*0.3),
  76. body: Padding(
  77. padding: EdgeInsets.fromLTRB(13.89.wp, 11.39.wp, 13.89.wp, 11.39.wp),
  78. child: Column(
  79. children: [
  80. TextFormField(
  81. decoration:
  82. const TextDecoration(hintText: '请输入手机号'),
  83. validator: (String? value) {
  84. if (value == null || !value.isPhoneNumber) {
  85. return '请输入正确的手机号';
  86. }
  87. return null;
  88. },
  89. onChanged: (value) {
  90. controller.phone = value;
  91. },
  92. keyboardType: TextInputType.phone),
  93. _columnSpace(),
  94. _textFieldCode(),
  95. _columnSpace(),
  96. TextFormField(
  97. decoration:
  98. const TextDecoration(hintText: '昵称(必填)'),
  99. onChanged: (value) {
  100. controller.name = value;
  101. },
  102. keyboardType: TextInputType.text),
  103. _columnSpace(),
  104. button('注 册', controller.onSignUp),
  105. const Spacer(),
  106. Text.rich(TextSpan(
  107. text: '我已有账号,点击返回 ',
  108. style: TextStyle(fontSize: 33.33.rpx, color: Colors.black),
  109. children: [
  110. TextSpan(
  111. text: '登录页面',
  112. style: const TextStyle(color: Color(0xffffb40b)),
  113. recognizer: TapGestureRecognizer()
  114. ..onTap = () {
  115. Get.back();
  116. },
  117. ),
  118. ])),
  119. ],
  120. ),
  121. ),
  122. );
  123. }
  124. Widget _columnSpace() {
  125. return SizedBox(height: 6.12.wp);
  126. }
  127. Widget _textFieldCode() {
  128. return Stack(
  129. alignment: Alignment.centerRight,
  130. children: [
  131. TextFormField(
  132. decoration: const TextDecoration(
  133. hintText: '请输入验证码',
  134. ),
  135. onChanged: (value) {
  136. controller.code = value;
  137. },
  138. keyboardType: TextInputType.number,
  139. ),
  140. Padding(
  141. padding: const EdgeInsets.only(right: 4),
  142. child: SizedBox(
  143. width: 84,
  144. height: 37,
  145. child: Obx(() => GetCodeButton(
  146. codeRetryLeft: controller.codeRetryLeft.value,
  147. onPressed: () => controller.onGetCode(),
  148. )),
  149. ),
  150. ),
  151. ],
  152. );
  153. }
  154. }
  155. void main() {
  156. Get.lazyPut(()=>SignUpController());
  157. runPreview(const SignUpView());
  158. }