import 'dart:async'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import '../constants.dart'; import '../utils/api.dart'; import '../utils/http_utils.dart'; import '../utils/size_fit.dart'; import 'component/link_btn.dart'; import 'component/toast.dart'; import 'component/top_container.dart'; class Login extends StatefulWidget { bool _isBind = false; Login({Key? key,bool? isBind}) : super(key: key){ if (isBind!=null){ _isBind = isBind; } } @override State createState() => _LoginState(); } class _LoginState extends State { String strContent = ''; bool isPhoneSignup = true; TextEditingController controller = TextEditingController(); FocusNode focusNode = FocusNode(); FocusNode blankNode = FocusNode(); bool checked = true; @override void initState() { // TODO: implement initState Map data = Get.parameters; if (data.isNotEmpty) { strContent = data['content']??''; isPhoneSignup = data['type'] == 'sms'; controller.text = strContent; } WidgetsBinding.instance.addPostFrameCallback((timeStamp) { focusNode.requestFocus(); }); super.initState(); } @override void dispose() { // TODO: implement dispose focusNode.dispose(); blankNode.dispose(); super.dispose(); } showSignupAlert() { FocusScope.of(context).requestFocus(blankNode); Toast().showCustomHud( Container( alignment: Alignment.center, padding: EdgeInsets.only(top: 32.px, bottom: 32.px), child: Column( children: [ Text( '该${isPhoneSignup ? '手机号' : '邮箱'}尚未注册!\n不可直接登录,请前往注册', textAlign: TextAlign.center, 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('/invite_code'); }), 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'] == true) { Toast().hideHud(); showSignupAlert(); 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'] == true) { Toast().hideHud(); showSignupAlert(); return; } Map data2 = await HttpUtils.post(Api.emailSendCode, data: {'email': strContent}); print(data2.toString()); } } on DioError catch (e) { Toast().hideHud(); Toast().showInfoText(e.response?.data['error_message'], context: context); return; } // ignore: nullable_type_in_catch_clause Toast().hideHud(); Get.toNamed('/verify_code', parameters: { "code": '', "type": isPhoneSignup ? 'sms' : 'email', "content": strContent }); } @override Widget build(BuildContext context) { SizeFit.initialize(context); return Material( color: kBgColor, child: TopContainer( child: Stack(children: [ 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: () { Get.toNamed('/choose_country'); }, 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, focusNode: focusNode, controller: controller, maxLength: isPhoneSignup ? 11 : 50, 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(); // Get.toNamed('/signup_code'); }), 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, ) ])) ]))); } }