| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- import 'dart:async';
- import 'dart:io';
- import 'package:dio/dio.dart';
- import 'package:fast/constants.dart';
- import 'package:fast/model/model.dart';
- import 'package:fast/utils/api.dart';
- import 'package:fast/utils/global.dart';
- import 'package:fast/utils/http_utils.dart';
- import 'package:fast/utils/size_fit.dart';
- import 'package:fast/utils/storage.dart';
- import 'package:fast/view/component/fast_btn.dart';
- import 'package:fast/view/invite_code.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'component/header.dart';
- import 'component/toast.dart';
- import 'index.dart';
- class PhoneCode extends StatefulWidget {
- int type; //1 login 2 bind phone
- String phone;
- PhoneCode({Key? key, required this.phone, required this.type})
- : super(key: key);
- @override
- State<PhoneCode> createState() => _PhoneCodeState();
- }
- class _PhoneCodeState extends State<PhoneCode> {
- String strCode = '';
- String strError = '输入的邀请码错误,请重新输入';
- bool showError = false;
- int seconds = 60;
- FocusNode focusNode = FocusNode();
- bool showCountdown = true;
- Timer? timer;
- @override
- void initState() {
- // TODO: implement initState
- super.initState();
- beginCountDown();
- }
- @override
- void dispose() {
- super.dispose();
- if (timer != null) {
- timer!.cancel();
- }
- }
- 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 {
- await HttpUtils.post(Api.sendCode, data: {'mobile': widget.phone});
- beginCountDown();
- }
- Future verify() async {
- if (widget.type == 2) {
- try {
- var data = await HttpUtils.post(Api.phoneBind, data: {
- "app_version": "1.0",
- "client_type": Platform.isAndroid ? "ANDROID" : "IOS",
- "client_version": "1.0",
- 'mobile': widget.phone,
- 'sms_code': strCode
- });
- Get.close(2);
-
- } on DioError catch (e) {
- setState(() {
- showError = true;
- strError = e.response?.data['error_message'];
- });
- }
-
- return;
- }
- try {
- Map<String, dynamic> data = await HttpUtils.post(Api.smsLogin, data: {
- "app_version": "1.0",
- "client_type": Platform.isAndroid ? "ANDROID" : "IOS",
- "client_version": "1.0",
- 'mobile': widget.phone,
- 'sms_code': strCode
- });
- UserBean user = UserBean.fromJson(data);
- Global().userBean = user;
- Global().token = data['token'];
- Global().balance = user.rjvBalance;
- if (user.inputInviteCode) {
- //没得验证码绑定,可以输入一波
- //首次注册登录,弹获得多少逆龄石
- showRegisterSuccessToast();
- Timer(const Duration(milliseconds: 1300), () {
- Get.to(() => const InviteCode());
- Global().showLogin = false;
- Global().mainPage!.login();
- StorageUtil().prefs!.setString("token", data['token']);
- });
- } else {
- Global().showLogin = false;
- // Global().mainPage!.login();
- StorageUtil().prefs!.setString("token", data['token']);
- // Get.until((route) => false);
- Get.offAll(const IndexScreen());
- }
- } on DioError catch (e) {
- setState(() {
- showError = true;
- strError = e.response?.data['error_message'];
- });
- }
- }
- showRegisterSuccessToast() {
- if (mounted) {
- showDialog(
- context: context,
- barrierDismissible: false,
- barrierColor: Colors.transparent,
- builder: (BuildContext context) {
- return Toast(
- title: '成功注册',
- content: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text(
- '+9',
- style: TextStyle(
- color: Colors.white,
- fontSize: 16.px,
- fontWeight: FontWeight.w800,
- fontFamily: 'Exo2',
- decoration: TextDecoration.none),
- ),
- SizedBox(
- width: 3.px,
- ),
- Image.asset(
- 'assets/images/stone.png',
- width: 24.px,
- height: 24.px,
- )
- ],
- ),
- );
- });
- }
- }
- @override
- Widget build(BuildContext context) {
- SizeFit.initialize(context);
- return Material(
- child: Container(
- color: kBgColor,
- child: Column(
- children: [
- Stack(
- children: [
- Header(
- isIndexPage: false,
- ),
- Positioned(
- left: 16.px,
- top:54.px,
- child: GestureDetector(onTap: () => Navigator.pop(context),child: Container(
- width: 32.px,
- height: 32.px,
- alignment: Alignment.center,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(16.px)),
- color: const Color(0x33000000)
- ),
- child: Image.asset(
- 'assets/images/naviback.png',
- width: 20.px,
- height: 20.px,
- fit: BoxFit.cover,
- )
- ),))
- ],
- ),
- SizedBox(
- height: 50.px,
- ),
- Opacity(
- opacity: showError ? 1 : 0,
- child: Text(
- strError,
- style: TextStyle(color: kThemeColor, fontSize: 12.px),
- ),
- ),
- SizedBox(
- height: 10.px,
- ),
- Stack(
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- item(strCode.isNotEmpty ? strCode.substring(0, 1) : ''),
- SizedBox(
- width: 18.px,
- ),
- item(strCode.length > 1 ? strCode.substring(1, 2) : ''),
- SizedBox(
- width: 18.px,
- ),
- item(strCode.length > 2 ? strCode.substring(2, 3) : ''),
- SizedBox(
- width: 18.px,
- ),
- item(strCode.length > 3 ? strCode.substring(3, 4) : '')
- ],
- ),
- Opacity(
- opacity: 0,
- child: TextField(
- focusNode: focusNode,
- autofocus: true,
- maxLength: 4,
- keyboardType: TextInputType.number,
- onChanged: (value) {
- setState(() {
- strCode = value;
- });
- },
- // keyboardType: TextInputType.number,
- // inputFormatters: [
- // FilteringTextInputFormatter(RegExp("[0-9.]"), allow: true),
- // ],
- ),
- )
- ],
- ),
- SizedBox(
- height: 20.px,
- ),
- Row(
- children: [
- SizedBox(
- width: 32.px,
- ),
- Expanded(
- child: Text(
- '验证码已发到 ${widget.phone}',
- style: TextStyle(color: Colors.white, fontSize: 14.px),
- )),
- if (showCountdown)
- Text(
- '${seconds}s',
- style: TextStyle(
- color: const Color(0x99FFFFFF), fontSize: 14.px),
- ),
- if (!showCountdown)
- GestureDetector(
- onTap: () {
- resend();
- },
- child: Text(
- '重新发送',
- style: TextStyle(
- color: kThemeColor,
- fontSize: 14.px,
- fontWeight: FontWeight.bold),
- ),
- ),
- SizedBox(
- width: 32.px,
- ),
- ],
- ),
- SizedBox(
- height: 28.px,
- ),
- FastBtn(
- title: '验证',
- disable: false,
- // color: const Color(0x80AAFF00),
- width: 311.px,
- height: 48.px,
- callback: () {
- verify();
- }),
- ],
- ),
- ));
- }
- Widget item(String code) {
- return GestureDetector(
- onTap: () {
- FocusScope.of(context).requestFocus(focusNode);
- },
- child: Container(
- width: 64.px,
- height: 64.px,
- alignment: Alignment.center,
- decoration: BoxDecoration(
- color: const Color(0x26C4CCDA),
- borderRadius: BorderRadius.all(Radius.circular(16.px))),
- child: Text(
- code,
- style: TextStyle(
- color: Colors.white,
- fontSize: 40.px,
- fontFamily: 'Exo2',
- fontWeight: FontWeight.w600),
- ),
- ),
- );
- }
- }
|