import 'package:dio/dio.dart'; import 'package:fast/constants.dart'; import 'package:fast/utils/api.dart'; import 'package:fast/utils/http_utils.dart'; import 'package:fast/utils/size_fit.dart'; import 'package:fast/view/index.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'component/fast_btn.dart'; import 'component/header.dart'; import 'component/toast.dart'; class InviteCode extends StatefulWidget { const InviteCode({Key? key}) : super(key: key); @override State createState() => _InviteCodeState(); } class _InviteCodeState extends State { String code = ''; bool showError = false; @override Widget build(BuildContext context) { SizeFit.initialize(context); return Material( child: Container( color: kBgColor, child: Column(children: [ Header( isIndexPage: false, ), SizedBox( height: 50.px, ), Opacity( opacity: showError ? 1 : 0, child: Text( '输入的邀请码错误,请重新输入', style: TextStyle(color: kThemeColor, fontSize: 12.px), ), ), SizedBox( height: 10.px, ), Stack( children: [ Container( width: 311.px, height: 48.px, alignment: Alignment.center, decoration: BoxDecoration( color: const Color(0x26C4CCDA), borderRadius: BorderRadius.all(Radius.circular(16.px))), child: Opacity( opacity: code.isEmpty ? 1 : 0, child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( '请输入邀请码,你和朋友各得', style: TextStyle( color: const Color(0x66FFFFFF), fontSize: 16.px), ), SizedBox( width: 3.px, ), Text( '5', style: TextStyle( color: const Color(0x99FFFFFF), fontSize: 24.px, fontFamily: 'Exo2', fontWeight: FontWeight.w600), ), SizedBox( width: 3.px, ), Image.asset( 'assets/images/stone.png', width: 24.px, height: 24.px, ) ], ), )), SizedBox( width: 311.px, height: 48.px, child: TextField( onChanged: (value) { setState(() { code = value; }); }, style: TextStyle( color: Colors.white, fontSize: 24.px, fontFamily: 'Exo2', fontWeight: FontWeight.w600), cursorColor: Colors.white, maxLength: 4, autofocus: true, textAlign: TextAlign.center, decoration: const InputDecoration( counterText: "", border: InputBorder.none, contentPadding: EdgeInsets.zero, hintStyle: TextStyle(color: Color(0x66FFFFFF))), )) ], ), SizedBox( height: 28.px, ), FastBtn( title: '提交', disable: false, // color: const Color(0x80AAFF00), width: 311.px, height: 48.px, callback: () { invite(); }), SizedBox( height: 30.px, ), GestureDetector( onTap: () { jump(); }, child: Text( '无邀请码,跳过', style: TextStyle( color: const Color(0x99C4CCDA), fontSize: 14.px), )) ]))); } Future invite() async { try { Map data = await HttpUtils.post(Api.inviteCode, data: {'skip': false, 'invite_code': code}); showToast(); Get.offAll(const IndexScreen()); // Navigator.of(context).pop(); } on DioError catch (e) { if (e.response?.data['error_code'] == 'INVITE_CODE_NOT_EXIST') { setState(() { showError = true; }); } } } showToast() { showDialog( context: context, barrierDismissible: false, barrierColor: Colors.transparent, builder: (BuildContext context) { return Toast( title: '成功受邀', content: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( '+3', 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, ) ], ), ); }); } Future jump() async { var data = await HttpUtils.post(Api.inviteCode, data: {'skip': true}); Get.offAll(const IndexScreen()); } }