import 'dart:io'; import 'dart:typed_data'; import 'package:chat/data/UserData.dart'; import 'package:chat/data/constants.dart'; import 'package:chat/generated/i18n.dart'; import 'package:chat/home/rich_title.dart'; import 'package:chat/models/UserInfo.dart'; import 'package:chat/models/money_change.dart'; import 'package:chat/models/ref_name_provider.dart'; import 'package:chat/utils/ChargeMoney.dart'; import 'package:chat/utils/CustomUI.dart'; import 'package:chat/utils/HttpUtil.dart'; import 'package:chat/utils/MessageMgr.dart'; import 'package:chat/utils/PicSwiper.dart'; import 'package:chat/utils/TokenMgr.dart'; import 'package:chat/utils/file_cache_mgr.dart'; import 'package:chat/utils/screen.dart'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:multi_image_picker/multi_image_picker.dart'; import 'package:oktoast/oktoast.dart'; import 'package:provider/provider.dart'; import 'VipPage.dart'; const double LeftSize = 80; const MaxImgSize = 20; class PhotoPage extends StatefulWidget { @required final UserInfo userInfo; PhotoPage({Key key, this.userInfo}) : super(key: key); @override _PhotoPageState createState() => new _PhotoPageState(); } class _PhotoPageState extends State { bool isMyself = false; bool isMan = false; Map imgList = {}; bool isLoadingFinish = false; double imgWidth = (Screen.width - LeftSize - 60) / 3; var resultList = []; bool isBuyPicture = false; @override void initState() { super.initState(); isMyself = widget.userInfo.userId == UserData().basicInfo.userId; isMan = widget.userInfo.sex == 1; isBuyPicture = (isMyself || widget.userInfo.photoAut == 0 || widget.userInfo.payStatus == 1); getImg(); MessageMgr().on('refresh_photo', msgRefreshPhoto); } msgRefreshPhoto(data) { getImg(); } void getImg() async { Map data = { "visitUserId": UserData().basicInfo.userId, "userId": widget.userInfo.userId, }; data['sign'] = TokenMgr().getSign(data); Response res = await HttpUtil().post('user/personal/album', data: data); if (res == null) { return; } isLoadingFinish = true; Map resData = res.data; if (resData['code'] == 0) { imgList = {}; resultList = resData['data'] ?? []; for (int i = 0; i < resultList.length; i++) { var temp = DateTime.parse(resultList[i]['CreatTime']); if (imgList[temp.year] == null) { imgList[temp.year] = { 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [], 11: [], 12: [], }; } imgList[temp.year][temp.month].add(resultList[i]); } if (isMyself) { UserData().picNum = 0; for (int i = 0; i < resultList.length; i++) { if (resultList[i]['Status'] == 1) { UserData().picNum++; } if (resultList[i]['Status'] == 0 || resultList[i]['Status'] == 2) { UserData().haveReview = true; } } } if (mounted) { setState(() {}); } } } @override Widget build(BuildContext context) { var content = Scaffold( appBar: AppBar( title: !isMyself ? Text( (isMyself ? I18n.of(context).my_album : (isMan ? I18n.of(context).his_photo : I18n.of(context).her_photo)), textScaleFactor: 1.0, style: TextStyle( color: AppColors.NewAppbarTextColor, fontSize: 20), ) : Column(children: [ Text( (isMyself ? I18n.of(context).my_album : (isMan ? I18n.of(context).his_photo : I18n.of(context).her_photo)), textScaleFactor: 1.0, style: TextStyle( color: AppColors.NewAppbarTextColor, fontSize: 20), ), Text( '${resultList.length}/$MaxImgSize', style: TextStyle(fontSize: 11), ) ]), leading: CustomUI.buildCustomLeading(context), titleSpacing: -10, centerTitle: true, elevation: 1), backgroundColor: Colors.white, body: SafeArea( child: SingleChildScrollView( child: Column(children: _buildBody()), )), ); return CustomUI.buildPageLoading(context, content, !isLoadingFinish); } buyPhoto() { //女性用户付费,男性用户会员的话免费解锁,非会员付费解锁 becomeVip() { Navigator.of(context).push( new MaterialPageRoute( builder: (context) { return VipPage(); }, ), ); } payCallback() { if (Provider.of(context).money < widget.userInfo.price) { Navigator.of(context).pop(); CustomUI.buildOneConfirm(context, I18n.of(context).balance_insufficien, I18n.of(context).recharge, () { Navigator.of(context).pop(); ChargeMoney.showChargeSheet(context, () { setState(() {}); }); }); return; } Navigator.of(context).pop(); HttpUtil().buyPictures( widget.userInfo.price, widget.userInfo.userId, context, () { setState(() { isBuyPicture = true; widget.userInfo.payStatus = 1; }); }); } freeTime() { HttpUtil().userFreeTime(context, widget.userInfo.userId, 1, () { UserData().basicInfo.usedNum++; Navigator.of(context).pop(); setState(() { isBuyPicture = true; widget.userInfo.payStatus = 1; }); }); } String title = I18n.of(context).unlock_user.replaceFirst( '/s1', Provider.of(context) .getRefName(widget.userInfo.userId, widget.userInfo.nickName), ); if (!UserData().isMan()) { CustomUI.buildOneConfirm( context, title, I18n.of(context) .pay_unlock .replaceFirst('/s1', widget.userInfo.price.toString()), payCallback); } else if (UserData().isVip) { if (UserData().basicInfo.freeNum < UserData().basicInfo.usedNum) { CustomUI.buildOneConfirm( context, title, I18n.of(context) .pay_unlock .replaceFirst('/s1', widget.userInfo.price.toString()), payCallback); } else { CustomUI.buildOneConfirm( context, title, I18n.of(context).unlock_choose, freeTime); } } else { CustomUI.buildTowConfirm( context, title, I18n.of(context).become_member, becomeVip, I18n.of(context) .pay_unlock .replaceFirst('/s1', widget.userInfo.price.toString()), payCallback); } } Widget _buildLockWidget() { TitleItem title; if (!isMan) { title = UserData().isVip ? TitleItem( title: I18n.of(context).unlock_user, name: Provider.of(context) .getRefName(widget.userInfo.userId, widget.userInfo.nickName), ) : TitleItem( title: I18n.of(context).free_unlock, name: widget.userInfo.price.toString() + I18n.of(context).mask_coin); } else { title = TitleItem( title: I18n.of(context).pay_unlock, name: widget.userInfo.price.toString()); } return Container( height: Screen.height * 0.7, alignment: Alignment.center, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset( 'assets/images/lock_photo.png', width: 160, //color: Colors.grey[700], ), Container( margin: EdgeInsets.only(top: 20), alignment: Alignment.center, child: Text( isMan ? I18n.of(context).set_lock : I18n.of(context).set_lock2, textScaleFactor: 1.0, style: TextStyle( color: Colors.grey[700], fontSize: 15, fontWeight: FontWeight.bold), ), ), Container( margin: EdgeInsets.only(top: 8), alignment: Alignment.center, child: RichText( text: TextSpan( children: RichTitle.getRichText(title, titleStyle: TextStyle(color: Colors.grey[700], fontSize: 13), nameStyle: TextStyle(color: Colors.red, fontSize: 13))), ), ), InkWell( onTap: buyPhoto, child: Container( margin: EdgeInsets.only(top: 30), padding: EdgeInsets.only(top: 6, bottom: 6), width: 100, alignment: Alignment.center, decoration: Constants.ConfirmBUttonBoxDecoration, child: Text( I18n.of(context).unlock, textScaleFactor: 1.0, style: TextStyle(color: Colors.white, fontSize: 18), ), )), ], )); } List _buildBody() { List list = [_buildUpload()]; if (imgList.length == 0 && isLoadingFinish && !isMyself) { list.add(CustomUI.buildNoData(context)); return list; } if (!isBuyPicture) { list.add(_buildLockWidget()); return list; } imgList.keys.forEach((k) { list.add(_buildYear(k)); }); return list; } //上传相册 void _uploadPhoto() async { int leftLength = MaxImgSize - imgList.length; if (leftLength <= 0) { showToast(I18n.of(context) .max_upload_size .replaceFirst('/s1', MaxImgSize.toString())); return; } List resultList = List(); resultList = await MultiImagePicker.pickImages( maxImages: leftLength > 9 ? 9 : leftLength, enableCamera: false, selectedAssets: [], cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"), materialOptions: MaterialOptions( actionBarColor: "#50A7F9", actionBarTitle: "Hibok", allViewTitle: "", useDetailsView: true, selectCircleStrokeColor: "#000000", ), ); if (resultList != null && resultList.length > 0) { List fileList = []; for (var i = 0; i < resultList.length; i++) { Asset photoEntity = resultList[i]; print('名字:${photoEntity.name}'); ByteData byteData = await photoEntity.getByteData(); File file = await FileCacheMgr().writeFile( 'temp-photo-${DateTime.now().millisecondsSinceEpoch}.png', byteData.buffer.asInt8List(0)); fileList.add(file); } print('文件列表${fileList.length}'); Map data = {"type": 2, "userId": UserData().basicInfo.userId}; data['sign'] = TokenMgr().getSign(data); data['sex'] = UserData().basicInfo.sex; data['isBurn'] = 0; Response res = await HttpUtil().uploadFiles( fileList, data, 'upload/post/postfiles', 'image', isShowLoading: true); var resData = res.data; if (resData['code'] == 0) { if (resData['data']['msg'] != '' && resData['data']['msg'] != null) { showToast(resData['data']['msg']); } if (resData['msg'].split('|').length != fileList.length) { showToast(I18n.of(context).hava_error_photo); } //MessageMgr().emit('refresh_photo'); getImg(); } else { showToast(resData['msg']); } } } Widget _buildUpload() { return isMyself ? Container( margin: EdgeInsets.only(top: 20), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(right: 15), width: LeftSize, child: Text( I18n.of(context).upload1, style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600), )), InkWell( onTap: _uploadPhoto, child: Container( width: imgWidth, height: imgWidth, margin: EdgeInsets.all(5), decoration: BoxDecoration( color: Color(0xFFEDEDED), borderRadius: BorderRadius.circular(10)), child: Icon( IconData(0xe686, fontFamily: Constants.IconFontFamily), size: 35, color: Colors.white, ), )) ], ), ) : Container(); } Widget _buildYear(int year) { var list = [_buildYearTips(year)]; imgList[year].keys.forEach((k) { if (imgList[year][k].length > 0) { list.add(_buildMonth(year, k)); } }); return Container( width: Screen.width, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: list, )); } Widget _buildYearTips(int year) { return Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(right: 15, top: 20, bottom: 18), width: LeftSize, child: Text( '$year${I18n.of(context).year}', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600), )); } Widget _buildMonth(int year, int month) { if (imgList[year] == null || imgList[year][month] == null) { return Container(); } List pisc = []; imgList[year][month].forEach((f) { pisc.add(PicSwiperItem( f['ImgUrl'], id: f['Id'], type: f['Type'], isWatch: f['IsCheck'] == 1, userId: widget.userInfo.userId, isBuy: f['PayStatus'] == 1, isCheck: f['Status'] == 0, )); }); List list = []; imgList[year][month].forEach((data) { bool isWatch = data['IsCheck'] == 1; double raduis = 10; list.add(CustomUI.buildImgCover(data['Id'], pisc, data['ImgUrl'], imgWidth, raduis, isWatch, context, data['Type'], isMyself: isMyself, payStatus: data['PayStatus'], state: isMan ? 1 : data['Status'])); }); return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(right: 15, top: 1), width: LeftSize, child: Text( '$month${I18n.of(context).month}', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600), )), Container( width: Screen.width - LeftSize - 15, child: Wrap( crossAxisAlignment: WrapCrossAlignment.start, children: list, )) ], ); } @override void dispose() { super.dispose(); MessageMgr().off('refresh_photo', msgRefreshPhoto); } }