import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/data/base_data.dart'; import 'package:huixiang/data/upload_result.dart'; import 'package:huixiang/utils/shared_preference.dart'; import 'package:image_gallery_saver/image_gallery_saver.dart'; import 'package:image_pickers/image_pickers.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../community/release_dynamic.dart'; import '../retrofit/retrofit_api.dart'; import 'font_weight.dart'; class UploadAsync { static void upload(int dynamicType, List mediaPaths, String addressText, String dynamicText) async { SmartDialog.show( builder: (ctx) { return Container( alignment: Alignment.centerRight, margin: EdgeInsets.only( right: 10.w, ), child: Stack( alignment: Alignment.center, children: [ Container( width: 50.h, height: 70.h, decoration: BoxDecoration( color: Colors.grey, borderRadius: BorderRadius.circular(2)), child: dynamicType == 0 ? Expanded( child: Column( children: [ Text( dynamicText, maxLines: 5, overflow: TextOverflow.ellipsis, style: TextStyle( fontWeight: MyFontWeight.semi_bold, fontSize: dynamicText.length > 10 ? (dynamicText.length > 20 ? 8.sp : 10.sp) : 15.sp, color: Color(0xFFCDCCCC), ), ), Expanded( child: Container( height: 20.h, ), ), ], ), ) : ClipRRect( borderRadius: BorderRadius.circular(2), child: Image.file( File(mediaPaths[0].galleryMode == GalleryMode.video ? "${mediaPaths[0].thumbPath}" : "${mediaPaths[0].path}"), fit: BoxFit.cover, width: double.infinity, height: double.infinity, ), ), ), CircularProgressIndicator( strokeWidth: 3.0, backgroundColor: Colors.white, // value: 0.4, valueColor: new AlwaysStoppedAnimation(Colors.grey), ), ], ), ); }, maskWidget: SizedBox(), ); ApiService apiService = ApiService( Dio(), token: SharedInstance.instance.token, showLoading: false, ); fileUpload(apiService, mediaPaths, dynamicType).then((value) async { String subjectType = "text"; if (dynamicType == 0) { subjectType = "text"; } else if (dynamicType == 1) { subjectType = "image"; } else if (dynamicType == 2) { subjectType = "video"; } List remoteImageUrls = []; String remoteVideoUrl = ""; String remoteVideoCoverImg = ""; if (mediaPaths.length > 0) { if (dynamicType == 1) { remoteImageUrls = mediaPaths.map((e) => "${e.remotePath}").toList(); } else if (dynamicType == 2) { remoteVideoUrl = "${mediaPaths[0].remotePath}"; remoteVideoCoverImg = "${mediaPaths[0].thumbPath}"; } } BaseData baseData = await apiService.trend({ "images": remoteImageUrls, "subject": dynamicText, "subjectType": subjectType, "video": remoteVideoUrl, "coverImg": remoteVideoCoverImg, "latitude": "", "location": addressText, "longitude": "", }).catchError((onError) { }); if (baseData?.isSuccess ?? false) { SmartDialog.showToast("发布成功!"); UploadInstance.instance.notifyAllObservers(); } }); } ///文件上传 static Future fileUpload( ApiService apiService, List mediaPaths, int dynamicType) async { if (mediaPaths != null && mediaPaths.length > 0) { await Future.forEach(mediaPaths, (element) async { if ((element.remotePath == null || element.remotePath == "") && (element != null && element.path != null && element.path != "" && await File(element.path!).exists())) { File file = File(element.path!); if (dynamicType == 2) { String thumbnail; if (element.thumbPath != null && element.thumbPath != "" && await File(element.thumbPath!).exists()) { thumbnail = element.thumbPath!; } else { var result = await ImageGallerySaver.saveFile( file.path, isReturnPathOfIOS: true, ); if (result["isSuccess"] == true) { thumbnail = element.thumbPath!; } else { thumbnail = element.thumbPath!; } } if (thumbnail != "" && await File(thumbnail).exists()) { BaseData? baseData = await apiService.upload( File(thumbnail), 123123123, dynamicType == 2); if (baseData?.isSuccess ?? false) { UploadResult? uploadResult = baseData.data; mediaPaths[mediaPaths.indexOf(element)].thumbPath = uploadResult?.url ?? ""; } } } BaseData baseData = await apiService.upload(file, 123123123, dynamicType == 2); if (baseData?.isSuccess ?? false) { UploadResult? uploadResult = baseData.data; mediaPaths[mediaPaths.indexOf(element)].remotePath = uploadResult?.url ?? ""; } } }); } } } abstract class UploadObserver { void onUploadFinish(); } class UploadInstance { factory UploadInstance() => _getInstance(); static UploadInstance get instance => _getInstance(); static UploadInstance? _instance; late List _uploadObserverList; List get uploadObserverList => _uploadObserverList; set uploadObserverList(List value) { _uploadObserverList = value; } UploadInstance._internal() { //单例初始化 _uploadObserverList = []; } static UploadInstance _getInstance() { if (_instance == null) { _instance = UploadInstance._internal(); } return _instance!; } void notifyAllObservers() { _uploadObserverList.forEach((element) { element.onUploadFinish(); }); } }