import 'dart:async'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:sport/bean/user_info.dart'; import 'package:sport/services/api/inject_api.dart'; import 'package:sport/utils/toast.dart'; import '../button_cancel.dart'; import '../button_primary.dart'; import '../space.dart'; /// 业务组件 只针对 扫码加好友情况下的弹窗... class ScanAddFriendCustomAlertDialog extends StatefulWidget { final Function ok; final bool addClose; final UserInfo userInfo; final String newFriendCode; final bool isWaitAdd; final Function goNext; ScanAddFriendCustomAlertDialog( {this.userInfo, this.ok, this.addClose = false, this.newFriendCode, this.isWaitAdd, this.goNext}); @override State createState() { // TODO: implement createState return _ScanAddFriendCustomAlertDialog(); } } class _ScanAddFriendCustomAlertDialog extends State with InjectApi { String okText = "申请中"; int dialogIndex = 0; List dialogs; Timer timerText; Timer timerIsFriend; @override initState() { super.initState(); } initOkText() { int cout = 0; List okTexts = ["申请中", "申请中.", "申请中..", "申请中..."]; timerText = Timer.periodic(const Duration(seconds: 1), (timer) { //到时回调 setState(() { okText = okTexts[cout % 3]; }); cout += 1; if (cout >= 30) { timer.cancel(); timer = null; } }); } @override void dispose() { // TODO: implement dispose super.dispose(); timerText?.cancel(); // timer = null; timerIsFriend?.cancel(); } void pollGetIsFriend(int uid) { timerIsFriend = Timer.periodic(const Duration(seconds: 2), (timer) async { //到时回调 var data = (await api.postIsFriend(uid)).data; if (data) { ToastUtil.show("添加成功"); timer.cancel(); timer = null; Navigator.of(context).pop(false); } }); } @override Widget build(BuildContext context) { var _width = MediaQuery.of(context).size.width * 0.86; Widget userInfoContent(bool isShowUserInfo) { return Column( children: [ CircleAvatar( backgroundImage: CachedNetworkImageProvider( widget.userInfo?.avatar, ), radius: 35), Space( height: 8.0, ), Center( child: Text( "${widget.userInfo.name}", style: Theme.of(context).textTheme.headline3, ), ), Space( height: 8.0, ), isShowUserInfo ? Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Row( children: [ Image.asset( "lib/assets/img/mine_icon_${widget.userInfo.gender == 1 ? "manl" : "girl"}_gray.png"), Space( width: 4, ), Text( widget.userInfo.gender == 1 ? "男" : "女", style: TextStyle(color: Color(0xff999999), fontSize: 12), ), ], ), if ((widget.userInfo.age ?? 0) != 0) Padding( padding: const EdgeInsets.only(left: 10.0), child: Text( "${widget.userInfo.age}岁", style: TextStyle(color: Color(0xff999999), fontSize: 12), ), ), if (widget.userInfo.city != null) Padding( padding: const EdgeInsets.only(left: 10.0), child: Text( "${widget.userInfo.province}${widget.userInfo.province == widget.userInfo.city ? '' : widget.userInfo.city}", style: TextStyle(color: Color(0xff999999), fontSize: 12), ), ), ], ) : Text( "请求添加您为好友", style: Theme.of(context).textTheme.subtitle1, ), ], ); } Widget baseDialog(double width, Widget button, bool isShowUserInfo) { return Center( child: Container( width: width, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(10.0)), color: Colors.white, ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Align( alignment: Alignment.topRight, child: IconButton( icon: Image.asset("lib/assets/img/btn_close_big.png"), onPressed: () async { await api.postRejectFriend(widget.userInfo.id); Navigator.pop(context, false); }, )), ConstrainedBox( constraints: BoxConstraints( maxHeight: MediaQuery.of(context).size.height - 100), child: SingleChildScrollView( child: userInfoContent(isShowUserInfo))), Padding( padding: const EdgeInsets.all(16.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: FeelCancelButton( height: 35, callback: () async { // 这里cancel好像不需要? // 代表是第三个框 可以 取消... if (!isShowUserInfo) { await api.postRejectFriend(widget.userInfo.id); } Navigator.of(context).pop(false); }, content: "取消"), ), SizedBox( width: 16, ), Expanded(child: button) ], ), ) ], ), ), ); } Widget scanDialog(double width) { return baseDialog( width, PrimaryButton( height: 35, callback: () async { await api.getNewFriend(widget.newFriendCode); setState(() { dialogIndex = 1; initOkText(); // 申请中的 setState pollGetIsFriend(widget.userInfo.id); // 是否是好友的 }); }, content: "申请好友"), true, ); } Widget confirmDialog(double width) { return baseDialog(width, FeelCancelButton(height: 35, content: okText, callback: () {}), true); } Widget confirmDialogOther(double width) { return baseDialog( width, PrimaryButton( height: 35, callback: () async { var data = (await api.postAcceptFriend(widget.userInfo.id)).data; // 这里我不想去 争论什么 这里就是返回null的。 从来都没有用result 去 判断... if (data == null) { ToastUtil.show("添加成功"); } Navigator.of(context).pop(false); }, content: "添加"), false); } dialogs = [scanDialog, confirmDialog, confirmDialogOther]; return Material( type: MaterialType.transparency, child: widget.isWaitAdd != null ? dialogs[2](_width) : dialogs[dialogIndex](_width), ); } }