import 'dart:async'; import 'package:flutter/gestures.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; import 'package:grpc/grpc.dart'; import 'package:trackoffical_app/screen.dart'; import 'package:trackoffical_app/service/api.dart'; import 'common.dart'; import '../../route.dart'; import 'package:trackoffical_app/pb.dart' as pb; class SignUpController extends GetxController { var phone = ''; var code = ''; String name = '' ; final sex = pb.User_Sex.Male.obs; final codeRetryLeft = 0.seconds.obs; Timer? ticker; final isSignUpEnable = true.obs; @override void onInit() { super.onInit(); ticker = Timer.periodic(1.seconds, (timer) { codeRetryLeft.value -= 1.seconds; }); } onGetCode() async{ try{ await ApiService.to.authSendCodeToPhone(phone, pb.SmsType.SignUp); codeRetryLeft.value = await ApiService.to.getSmsSendLeftTime(phone); }on GrpcError catch(e){ if (e.code== StatusCode.alreadyExists){ Get.snackbar('已注册', '请直接登录'); }else{ Get.snackbar('发送失败', e.message??''); } }catch(e){ Get.snackbar('发送失败', e.toString()); } } void onSignUp() async{ if(name.isEmpty){ Get.snackbar('请输入昵称', '不能为空'); return; } if(isSignUpEnable.value){ isSignUpEnable.value = false; try{ await ApiService.to.signUp(phone, code, name, sex.value); await ApiService.to.flushUserInfo(); Get.offAllNamed(RouteName.homeSaveUserInfo); } on GrpcError catch(e){ if (e.code== StatusCode.unauthenticated){ Get.snackbar('验证码错误', '请重新输入'); }else{ Get.snackbar('注册失败', e.message??''); } }catch(e){ Get.snackbar('注册失败', e.toString()); }finally { isSignUpEnable.value = true; } } } } class SignUpView extends GetView { const SignUpView({super.key}); static void show(){ Get.to(const SignUpView(), binding: BindingsBuilder(() { Get.put(SignUpController()); })); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppTopBar(title: '注册账号', hasBackText: true, height: context.height*0.3), body: Padding( padding: EdgeInsets.fromLTRB(13.89.wp, 11.39.wp, 13.89.wp, 11.39.wp), child: Column( children: [ TextFormField( decoration: const TextDecoration(hintText: '请输入手机号'), validator: (String? value) { if (value == null || !value.isPhoneNumber) { return '请输入正确的手机号'; } return null; }, onChanged: (value) { controller.phone = value; }, keyboardType: TextInputType.phone), _columnSpace(), _textFieldCode(), _columnSpace(), TextFormField( decoration: const TextDecoration(hintText: '昵称(必填)'), onChanged: (value) { controller.name = value; }, keyboardType: TextInputType.text), _columnSpace(), button('注 册', controller.onSignUp), const Spacer(), Text.rich(TextSpan( text: '我已有账号,点击返回 ', style: TextStyle(fontSize: 33.33.rpx, color: Colors.black), children: [ TextSpan( text: '登录页面', style: const TextStyle(color: Color(0xffffb40b)), recognizer: TapGestureRecognizer() ..onTap = () { Get.back(); }, ), ])), ], ), ), ); } Widget _columnSpace() { return SizedBox(height: 6.12.wp); } Widget _textFieldCode() { return Stack( alignment: Alignment.centerRight, children: [ TextFormField( decoration: const TextDecoration( hintText: '请输入验证码', ), onChanged: (value) { controller.code = value; }, keyboardType: TextInputType.number, ), Padding( padding: const EdgeInsets.only(right: 4), child: SizedBox( width: 84, height: 37, child: Obx(() => GetCodeButton( codeRetryLeft: controller.codeRetryLeft.value, onPressed: () => controller.onGetCode(), )), ), ), ], ); } } void main() { Get.lazyPut(()=>SignUpController()); runPreview(const SignUpView()); }