import 'dart:async'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:get/get.dart'; import 'package:flutter/material.dart'; import 'package:get_storage/get_storage.dart'; import 'package:grpc/grpc.dart'; import 'package:trackoffical_app/service/api.dart'; import 'package:trackoffical_app/view/login/logo_widget.dart'; import '../../route.dart'; import '../../styles/theme.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); 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 Stack( children: [ const SvgPicture( SvgAssetLoader('assets/images/bk_app.svg'), fit: BoxFit.fitHeight, height: double.infinity, width: double.infinity, ), Scaffold( backgroundColor: Colors.transparent, appBar: AppBar( backgroundColor: Colors.transparent, automaticallyImplyLeading: false, title: GestureDetector( onTap: () => Get.back(), child: Row( children: [ const Icon(Icons.arrow_back_ios_new, color: Colors.white), Text( '返回', style: context.textTheme.titleMedium ?.copyWith(color: Colors.white), ) ], )), ), body: SingleChildScrollView( child: SizedBox( // height: MediaQuery.of(context).size.height, child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ const LogoWidget(), Container( margin: const EdgeInsets.fromLTRB(40, 4, 40, 0), padding: const EdgeInsets.all(12), width: double.infinity, decoration: BoxDecoration( color: const Color(0x8fffffff), borderRadius: BorderRadius.circular(7)), 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(), SignInButton( width: double.infinity, height: 46, onPressed: controller.onSignUp, color1: const Color(0xffe6ff00), color2: const Color(0xffffd500), child: Text( '注 册', style: context.textTheme.titleMedium ?.copyWith(color: const Color(0xff840000)), )) ], ), ), Padding(padding: MediaQuery.of(context).viewInsets), ], ), ), )) ], ); } Widget _columnSpace() { return const SizedBox(height: 12); } 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() { runApp(GetMaterialApp( theme: appThemeData(), initialBinding: BindingsBuilder(() { Get.put(SignUpController()); }), home: const SignUpView())); }