123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- 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 'package:sport/widgets/image.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({required this.userInfo, this.ok, this.addClose = false, this.newFriendCode, this.isWaitAdd = false, this.goNext});
- @override
- State<StatefulWidget> createState() {
- return _ScanAddFriendCustomAlertDialog();
- }
- }
- class _ScanAddFriendCustomAlertDialog extends State<ScanAddFriendCustomAlertDialog> with InjectApi {
- String okText = "申请中";
- int dialogIndex = 0;
- List<Function>? dialogs;
- Timer? timerText;
- Timer? timerIsFriend;
- ValueNotifier<int> _addState = ValueNotifier(0);
- @override
- initState() {
- super.initState();
- _addState.addListener(() {
- Timer(Duration(seconds: 3), () {
- Navigator.of(context).maybePop(false);
- });
- });
- }
- initOkText() {
- int timeout = 0;
- List<String> okTexts = ["申请中", "申请中.", "申请中..", "申请中..."];
- int max = 20;
- timerText?.cancel();
- timerText = Timer.periodic(const Duration(seconds: 1), (timer) {
- //到时回调
- setState(() {
- okText = "${okTexts[timeout % 3]} (${max - timeout})";
- });
- if (timeout > max) {
- timer.cancel();
- _addState.value = 99;
- }
- timeout += 1;
- });
- }
- @override
- void 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.userRelation(uid: uid)).data;
- if (data?.relation == "friends") {
- // ToastUtil.show("添加成功");
- timer.cancel();
- _addState.value = 1;
- // Navigator.of(context).pop(false);
- return;
- }
- if (data?.followDetail?.isIgnore == 0) {
- // ToastUtil.show("添加成功");
- timer.cancel();
- _addState.value = 2;
- // Navigator.of(context).pop(false);
- return;
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- var _width = MediaQuery.of(context).size.width * 0.86;
- Widget userInfoContent(bool isShowUserInfo) {
- return Column(
- children: <Widget>[
- CircleAvatar(backgroundColor: Colors.black26,
- backgroundImage: userAvatarProvider(
- widget.userInfo.avatar,
- ),
- radius: 35),
- Space(
- height: 12.0,
- ),
- Center(
- child: isShowUserInfo
- ? ValueListenableBuilder(
- valueListenable: _addState,
- builder: (BuildContext context, int value, Widget? child) {
- if (value == 0) {
- return RichText(
- text: TextSpan(children: <InlineSpan>[
- TextSpan(text: "是否添加 ", style: Theme.of(context).textTheme.subtitle1!),
- TextSpan(
- text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
- TextSpan(text: " 为好友", style: Theme.of(context).textTheme.subtitle1!)
- ]),
- );
- }else if (value == 1) {
- return RichText(
- text: TextSpan(children: <InlineSpan>[
- TextSpan(text: "已添加 ", style: Theme.of(context).textTheme.subtitle1!),
- TextSpan(
- text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
- TextSpan(text: " 为好友", style: Theme.of(context).textTheme.subtitle1!)
- ]),
- );
- } else if (value == 2) {
- return Text("对方拒绝了您的好友申请", style: Theme.of(context).textTheme.subtitle1!);
- } else {
- return RichText(
- text: TextSpan(children: <InlineSpan>[
- TextSpan(text: "对方未回应,已", style: Theme.of(context).textTheme.subtitle1!),
- TextSpan(text: " 关注 ", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
- TextSpan(text: "对方", style: Theme.of(context).textTheme.subtitle1!)
- ]),
- );
- }
- },
- )
- : ValueListenableBuilder(
- valueListenable: _addState,
- builder: (BuildContext context, int value, Widget? child) {
- if (value == 1) {
- return RichText(
- text: TextSpan(children: <InlineSpan>[
- TextSpan(text: "已添加", style: Theme.of(context).textTheme.subtitle1!),
- TextSpan(
- text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
- TextSpan(text: " 为好友", style: Theme.of(context).textTheme.subtitle1!)
- ]),
- );
- } else if (value == 2) {
- return RichText(
- text: TextSpan(children: <InlineSpan>[
- TextSpan(text: "您已拒绝了 ", style: Theme.of(context).textTheme.subtitle1!),
- TextSpan(
- text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
- TextSpan(text: " 的好友申请", style: Theme.of(context).textTheme.subtitle1!)
- ]),
- );
- } else {
- return RichText(
- text: TextSpan(children: <InlineSpan>[
- TextSpan(
- text: "${widget.userInfo.name}", style: Theme.of(context).textTheme.subtitle1!.copyWith(color: Theme.of(context).accentColor)),
- TextSpan(text: " 请求添加您为好友", style: Theme.of(context).textTheme.subtitle1!)
- ]),
- );
- }
- }),
- ),
- // Space(
- // height: 8.0,
- // ),
- // isShowUserInfo
- // ? Row(
- // mainAxisAlignment: MainAxisAlignment.center,
- // children: <Widget>[
- // Row(
- // children: <Widget>[
- // 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,
- padding: const EdgeInsets.all(24.0),
- decoration: BoxDecoration(
- borderRadius: BorderRadius.all(Radius.circular(10.0)),
- color: Colors.white,
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: <Widget>[
- ConstrainedBox(
- constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height - 100),
- child: SingleChildScrollView(child: userInfoContent(isShowUserInfo))),
- ValueListenableBuilder(
- valueListenable: _addState,
- builder: (BuildContext context, int value, Widget? child) {
- if (_addState.value == 0)
- return Padding(
- padding: const EdgeInsets.only(top: 30),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Expanded(
- child: FeelCancelButton(
- height: 35,
- callback: () async {
- // 这里cancel好像不需要?
- // 代表是第三个框 可以 取消...
- if (isShowUserInfo) {
- Navigator.of(context).pop(false);
- }else{
- await api.postRejectFriend(widget.userInfo.id!);
- _addState.value = 2;
- }
- },
- content: isShowUserInfo ? "取消" : "拒绝"),
- ),
- SizedBox(
- width: 16,
- ),
- Expanded(child: button)
- ],
- ),
- );
- return Container();
- },
- ),
- ],
- ),
- ),
- );
- }
- Widget scanDialog(double width) {
- return baseDialog(
- width,
- PrimaryButton(
- height: 35,
- callback: () async {
- await api.getNewFriend(widget.newFriendCode ?? "");
- setState(() {
- dialogIndex = 1;
- pollGetIsFriend(widget.userInfo.id!); // 是否是好友的
- });
- initOkText();
- },
- 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 resp = (await api.postAcceptFriend(widget.userInfo.id!));
- // 这里我不想去 争论什么 这里就是返回null的。 从来都没有用result 去 判断...
- if (resp.code == 0) {
- // ToastUtil.show("添加成功");
- _addState.value = 1;
- }
- // Navigator.of(context).pop(false);
- },
- content: "添加"),
- false);
- }
- dialogs = [scanDialog, confirmDialog, confirmDialogOther];
- return Material(
- type: MaterialType.transparency,
- child: widget.isWaitAdd == true ? dialogs![2](_width) : dialogs![dialogIndex](_width),
- );
- }
- }
|