import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:link/view/choose_country.dart'; import 'package:link/view/component/link_btn.dart'; import 'package:link/view/component/toast.dart'; import 'package:dio/dio.dart'; import '../constants.dart'; import '../utils/api.dart'; import '../utils/http_utils.dart'; import '../utils/size_fit.dart'; import 'component/top_container.dart'; class SignUp extends StatefulWidget { bool _isBind = false; SignUp({Key? key,bool? isBind}) : super(key: key){ if (isBind!=null){ _isBind = isBind; } } @override State createState() => _SignUpState(); } class _SignUpState extends State { String code = ''; String strContent = ''; bool isPhoneSignup = true; TextEditingController controller = TextEditingController(); FocusNode focusNode = FocusNode(); FocusNode blankNode = FocusNode(); bool checked = true; @override void initState() { // Map data = Get.parameters; // code = data['code']; Map data = Get.parameters; if (data.isNotEmpty) { code = data['code']??''; isPhoneSignup = data['type'] == 'sms'; } WidgetsBinding.instance.addPostFrameCallback((timeStamp) { focusNode.requestFocus(); }); super.initState(); } @override void dispose() { // TODO: implement dispose focusNode.dispose(); blankNode.dispose(); super.dispose(); } showLoginAlert(bool isPhone) { FocusScope.of(context).requestFocus(blankNode); if (widget._isBind){ Toast().showInfoText('${isPhone ? '手机号' : '邮箱'}已绑定其他账号\n请重新输入', context: context); return; } Toast().showCustomHud( Container( alignment: Alignment.center, padding: EdgeInsets.only(top: 32.px, bottom: 32.px), child: Column( children: [ Text( '该${isPhone ? '手机号' : '邮箱'}已注册!\n可直接前往登录', style: TextStyle( color: Colors.white, fontSize: 16.px, fontWeight: FontWeight.bold), ), SizedBox( height: 16.px, ), AlertButton( title: '前往登录', isCancel: false, width: 220.px, height: 40.px, callback: () { Toast().hideHud(); Get.toNamed('/login', parameters: { "content": strContent, "type": isPhone ? 'sms' : 'email' }); }), SizedBox( height: 24.px, ), AlertButton( title: '返回重新输入', isCancel: true, width: 220.px, height: 40.px, callback: () { Toast().hideHud(); }), ], ), ), context: context); } Future sendCode() async { if (!checked){ Toast().showInfoText('请阅读并同意协议', context: context); return; } Toast().showHud(context: context); try { if (isPhoneSignup) { Map data = await HttpUtils.get(Api.phoneCanRegister, params: {'mobile': strContent}); if (data['success'] == false) { Toast().hideHud(); showLoginAlert(true); return; } Map data2 = await HttpUtils.post(Api.phoneSendCode, data: {'mobile': strContent}); print(data2.toString()); } else { Map data = await HttpUtils.get(Api.emailCanRegister, params: {'email': strContent}); if (data['success'] == false) { Toast().hideHud(); showLoginAlert(false); return; } Map data2 = await HttpUtils.post(Api.emailSendCode, data: {'email': strContent}); print(data2.toString()); } } on DioError catch (e) { print(e.toString()); } // ignore: nullable_type_in_catch_clause Toast().hideHud(); Get.toNamed('/verify_code', parameters: { "code": code, "type": isPhoneSignup ? 'sms' : 'email', "content": strContent, "bind":widget._isBind?'1':'0' }); } choose(){ showDialog( context: context, barrierDismissible: false, barrierColor: Colors.transparent, useSafeArea: false, builder: (BuildContext context) { return ChooseCountry(); }); } @override Widget build(BuildContext context) { SizeFit.initialize(context); return Material( color: kBgColor, child: TopContainer( child: Stack(children: [ // Positioned( // left: 0, // top: 0, // right: 0, // child: Container( // padding: EdgeInsets.only( // left: 12.px, right: 12.px, top: 14.px, bottom: 14.px), // alignment: Alignment.topLeft, // child: Image.asset( // 'assets/images/navi_back.png', // width: 20.px, // height: 20.px, // ), // )), Container( padding: EdgeInsets.only(top: 71.px), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Image.asset( 'assets/images/logo.png', width: 167.px, height: 64.px, ), SizedBox( height: 36.px, ), Container( width: 315.px, height: 68.px, padding: EdgeInsets.only(left: 16.px, right: 16.px), decoration: BoxDecoration( borderRadius: BorderRadius.circular(16.px), color: const Color(0xFF2C2C2E)), child: Row( children: [ if (isPhoneSignup) GestureDetector( onTap: (){ choose(); }, child: Row( children: [ Text( '+86', style: TextStyle( fontFamily: 'Link1', color: Colors.white, fontSize: 28.px), ), SizedBox( width: 4.px, ), Image.asset( 'assets/images/arrow.png', width: 24.px, height: 24.px, ) ], ), ), if (isPhoneSignup) SizedBox( width: 8.px, ), Expanded( child: TextField( autofocus: true, controller: controller, focusNode: focusNode, // maxLength: 11, cursorColor: kBtnColor, keyboardType: isPhoneSignup ? TextInputType.number : TextInputType.emailAddress, onChanged: (value) { setState(() { strContent = value; }); }, onEditingComplete: (() { FocusScope.of(context).requestFocus(focusNode); }), style: TextStyle( color: Colors.white, fontSize: 28.px, fontFamily: 'Link1'), decoration: InputDecoration( border: InputBorder.none, hintText: isPhoneSignup ? '输入我的手机号' : '输入我的邮箱', hintStyle: TextStyle( fontSize: 20.px, color: const Color(0xFF74747A)), counterText: "", ), // keyboardType: TextInputType.number, inputFormatters: isPhoneSignup ? [ FilteringTextInputFormatter( RegExp("[0-9.]"), allow: true), ] : [ // FilteringTextInputFormatter(RegExp("[0-9.]"), // allow: true), ], )), Opacity( opacity: strContent.isNotEmpty ? 1 : 0, child: GestureDetector( onTap: () { setState(() { strContent = ''; controller.clear(); }); }, child: Image.asset( 'assets/images/clear.png', width: 24.px, height: 24.px, ), ), ) ], ), ), SizedBox( height: 36.px, ), LinkButton( title: isPhoneSignup ? '发送短信验证码' : '发送邮箱验证码', disable: false, isBlack: false, callback: () { sendCode(); }), SizedBox( height: 37.px, ), if (!widget._isBind) Row( mainAxisAlignment: MainAxisAlignment.center, children: [ GestureDetector( onTap: (){ setState(() { checked = !checked; }); }, child: Image.asset( checked?'assets/images/checked.png':'assets/images/check.png', width: 18.px, height: 18.px, ), ), SizedBox( width: 7.px, ), Text( '已阅读并同意', style: TextStyle( color: const Color(0xFF74747A), fontSize: 12.px), ), GestureDetector( child: Text( '《用户服务协议》', style: TextStyle(color: kBtnColor, fontSize: 12.px), ), ), GestureDetector( child: Text( '《隐私权政策》', style: TextStyle(color: kBtnColor, fontSize: 12.px), ), ) ], ), Expanded( child: Container(), ), if (!widget._isBind) GestureDetector( onTap: () { setState(() { strContent = ''; controller.clear(); isPhoneSignup = !isPhoneSignup; }); }, child: Text( isPhoneSignup ? '使用邮箱注册' : '使用手机注册', style: TextStyle( color: const Color(0xFF808080), fontSize: 14.px), ), ), SizedBox( height: 50.px, ) ])) ]))); } }