import 'dart:async'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:link/utils/global.dart'; import 'package:link/utils/storage.dart'; import 'package:link/view/component/toast.dart'; import '../constants.dart'; import '../utils/api.dart'; import '../utils/http_utils.dart'; import '../utils/size_fit.dart'; import 'component/code.dart'; import 'component/link_btn.dart'; import 'component/top_container.dart'; class VerifyCode extends StatefulWidget { const VerifyCode({Key? key}) : super(key: key); @override State createState() => _VerifyCodeState(); } class _VerifyCodeState extends State { bool showCountdown = false; bool isBind = false; int seconds = 60; Timer? timer; String code = ''; late String content; late String invite_code; late String type; @override void initState() { // TODO: implement initState super.initState(); Map data = Get.parameters; //Get.arguments; content = data['content']; invite_code = data['code']; type = data['type']; if (data['bind'] != null) { isBind = data['bind'] == '1'; } beginCountDown(); } @override dispose(){ if (timer != null) { timer!.cancel(); } super.dispose(); } beginCountDown() { setState(() { seconds = 60; showCountdown = true; }); timer = Timer.periodic(const Duration(seconds: 1), (e) { setState(() { seconds = seconds - 1; }); if (seconds == 0) { showCountdown = false; timer!.cancel(); } }); } Future resend() async { try { if (type == 'sms') { Map data2 = await HttpUtils.post(Api.phoneSendCode, data: {'mobile': content}); print(data2.toString()); } else { Map data2 = await HttpUtils.post(Api.emailSendCode, data: {'email': content}); print(data2.toString()); } } on DioError catch (e) { print(e.toString()); return; } beginCountDown(); } Future verify() async { Toast().showHud(context: context); Map data2; if (isBind) { try { if (type == 'sms') { data2 = await HttpUtils.post(Api.phoneBind, data: { 'mobile': content, 'verify_code': code }); print(data2.toString()); } else { data2 = await HttpUtils.post(Api.emailBind, data: { 'email': content, 'verify_code': code }); print(data2.toString()); } } on DioError catch (e) { // print(e.response?.data.toString()); Toast().hideHud(); Toast() .showInfoText(e.response?.data['error_message'], context: context); return; } Toast().hideHud(); if (isBind) { Get.close(2); return; } return; } try { if (type == 'sms') { data2 = await HttpUtils.post(Api.phoneLogin, data: { 'mobile': content, 'verify_code': code, 'invite_code': invite_code }); print(data2.toString()); } else { data2 = await HttpUtils.post(Api.emailLogin, data: { 'email': content, 'verify_code': code, 'invite_code': invite_code }); print(data2.toString()); } } on DioError catch (e) { // print(e.response?.data.toString()); Toast().hideHud(); Toast().showInfoText(e.response?.data['error_message'], context: context); return; } Toast().hideHud(); Global().token = data2['token']; Global().hasLogin = true; StorageUtil().setJSON('userInfo', data2); StorageUtil().setBool('hasLogin', true); StorageUtil().setString('token', data2['token']); if (data2['need_improve'] == true) { Get.toNamed('/edit_nick_info'); } else { Get.offAllNamed('/', parameters: {'u': data2['id']}); } } @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, // ), // )), Positioned( child: SingleChildScrollView( child: Container( padding: EdgeInsets.only(top: 47.px), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Image.asset( 'assets/images/logo.png', width: 167.px, height: 64.px, ), SizedBox( height: 36.px, ), Text( '验证码已发送至 $content', textAlign: TextAlign.center, style: TextStyle( color: const Color(0xFF74747A), fontSize: 14.px, height: 1.71), ), if (showCountdown) Text( seconds.toString() + 's', style: TextStyle( color: const Color(0xFF74747A), fontSize: 14.px, height: 1.71), ), if (!showCountdown) GestureDetector( onTap: () { resend(); }, child: Text( '重新发送', style: TextStyle( color: kBtnColor, fontSize: 14.px, height: 1.71), ), ), SizedBox( height: 12.px, ), Code( type: 2, changed: (e) { setState(() { code = e; if (e.length == 4) { verify(); } }); }, ), SizedBox( height: 24.px, ), LinkButton( title: '验证', disable: false, isBlack: false, callback: () { verify(); // }), SizedBox( height: 52.px, ), ])), )) ]))); } }