You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
12 KiB

3 years ago
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/upload_result.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ReleaseDynamic extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ReleaseDynamic();
}
}
class _ReleaseDynamic extends State<ReleaseDynamic> {
int selectCount = 9;
List<Medias> mediaPaths = [];
bool isRelease = false;
int dynamicType = 0;
TextEditingController textEditingController = TextEditingController();
ApiService apiService;
3 years ago
3 years ago
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) {
apiService = ApiService(
Dio(),
context: context,
token: value.getString("token"),
showLoading: false,
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: MyAppBar(
title: "写动态",
titleColor: Colors.black,
leadingColor: Colors.black,
background: Colors.white,
action: Container(
alignment: Alignment.center,
child: Container(
width: 46.w,
height: 24.h,
alignment: Alignment.center,
decoration: BoxDecoration(
color: isRelease ? Color(0xFF32A060) : Color(0xFFD8D8D8),
borderRadius: BorderRadius.circular(4),
),
child: Text(
"发布",
style: TextStyle(
color: isRelease ? Colors.white : Color(0xFFA0A0A0),
fontSize: 14.sp,
fontWeight: MyFontWeight.semi_bold,
),
),
),
),
onTap: () {
if (!isRelease) {
SmartDialog.showToast("请输入您此刻的想法!~");
return;
}
releaseDynamic();
},
),
body: Container(
child: Column(
children: [
buildEdit(),
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 12.w,
mainAxisSpacing: 12.w,
childAspectRatio: 1,
),
padding: EdgeInsets.all(16),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, position) {
if (mediaPaths.length > position) {
return imageItem(mediaPaths[position]);
} else {
return addImageItem();
}
},
itemCount: (mediaPaths.length == 0)
? 1
: ((dynamicType == 2)
? 1
: mediaPaths.length >= 9
? 9
: (mediaPaths.length + 1)),
),
],
),
),
);
}
releaseDynamic() async {
String dynamicText = textEditingController.text;
if (dynamicText == null || dynamicText == "") {
3 years ago
SmartDialog.showToast("请输入您此刻的想法!");
3 years ago
return;
}
EasyLoading.show(status: S.of(context).zhengzaijiazai);
fileUpload().then((value) async {
String subjectType = "text";
if (dynamicType == 0) {
subjectType = "text";
} else if (dynamicType == 1) {
subjectType = "image";
} else if (dynamicType == 2) {
subjectType = "video";
}
List<String> remoteImageUrls = [];
String remoteVideoUrl = "";
if (mediaPaths.length > 0) {
if (dynamicType == 1) {
remoteImageUrls = mediaPaths.map((e) => e.remotePath).toList();
} else if (dynamicType == 2) {
remoteVideoUrl = mediaPaths[0].remotePath;
}
}
3 years ago
BaseData<bool> baseData = await apiService.trend({
3 years ago
"images": remoteImageUrls,
"subject": dynamicText,
"subjectType": subjectType,
"video": remoteVideoUrl,
}).catchError((onError) {
EasyLoading.dismiss();
});
3 years ago
if (baseData.isSuccess) {
3 years ago
SmartDialog.showToast("发布成功!");
3 years ago
Future.delayed(Duration(seconds: 1), () {
Navigator.of(context).pop(true);
});
}
3 years ago
EasyLoading.dismiss();
});
}
///文件上传
Future<void> fileUpload() 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())) {
BaseData<UploadResult> baseData = await apiService.upload(
File(element.path),
123123123,
);
if (baseData != null && baseData.isSuccess) {
UploadResult uploadResult = baseData.data;
mediaPaths[mediaPaths.indexOf(element)].remotePath = uploadResult.url;
}
}
});
}
}
Widget imageItem(Medias media) {
return InkWell(
onTap: () {
showDeletePicker(media);
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
),
alignment: Alignment.center,
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Image.file(
File(media.galleryMode == GalleryMode.video
? media.thumbPath
: media.path),
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
),
),
);
}
///显示图片选择方式
showImagePicker() {
if (dynamicType == 1) {
getImageOrVideo(GalleryMode.image);
return;
}
showCupertinoModalPopup(
context: context,
builder: (context) {
return CupertinoActionSheet(
title: Text("选择媒体文件"),
actions: [
CupertinoActionSheetAction(
child: Text("照片"),
onPressed: () {
getImageOrVideo(GalleryMode.image);
Navigator.of(context).pop();
},
isDefaultAction: true,
isDestructiveAction: false,
),
CupertinoActionSheetAction(
child: Text("视频"),
onPressed: () {
getImageOrVideo(GalleryMode.video);
Navigator.of(context).pop();
},
isDefaultAction: true,
isDestructiveAction: false,
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(S.of(context).quxiao),
isDestructiveAction: true,
),
);
});
}
///显示图片选择方式
showDeletePicker(Medias media) {
showCupertinoModalPopup(
context: context,
builder: (context) {
return CupertinoActionSheet(
title: Text("动态"),
actions: [
CupertinoActionSheetAction(
child: Text("删除"),
onPressed: () {
mediaPaths.remove(media);
if (mediaPaths.length == 0) {
dynamicType = 0;
}
selectCount = 9 - mediaPaths.length;
Navigator.of(context).pop();
setState(() {});
},
isDefaultAction: true,
isDestructiveAction: false,
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(S.of(context).quxiao),
isDestructiveAction: true,
),
);
});
}
Widget addImageItem() {
return InkWell(
onTap: () {
showImagePicker();
},
child: Container(
decoration: BoxDecoration(
color: Color(0xFFF2F2F2),
borderRadius: BorderRadius.circular(4),
),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SvgPicture.asset(
"assets/svg/zhaopianshipin.svg",
width: 48,
height: 48,
fit: BoxFit.contain,
),
Text(
"照片/视频",
style: TextStyle(
fontWeight: MyFontWeight.semi_bold,
fontSize: 15.sp,
color: Color(0xFFCDCCCC),
),
),
],
),
),
);
}
Future getImageOrVideo(GalleryMode galleryMode) async {
if (selectCount == 0) return;
List<Media> medias = await ImagePickers.pickerPaths(
galleryMode: galleryMode,
selectCount: (galleryMode == GalleryMode.video) ? 1 : selectCount,
showGif: true,
showCamera: false,
compressSize: 500,
uiConfig: UIConfig(
uiThemeColor: Color(0xFFFFFFFF),
),
cropConfig: CropConfig(
enableCrop: false,
width: 200,
height: 200,
),
);
mediaPaths.addAll(medias.map((e) => Medias(e)).toList());
selectCount = 9 - mediaPaths.length;
if (mediaPaths.length > 0) {
if (galleryMode == GalleryMode.image) {
dynamicType = 1;
} else {
dynamicType = 2;
}
}
setState(() {});
}
///动态输入框
Widget buildEdit() {
return Container(
height: 174.h,
margin: EdgeInsets.symmetric(horizontal: 16.w),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 1.w,
color: Color(0xFFD8D8D8),
style: BorderStyle.solid,
),
),
),
child: TextField(
controller: textEditingController,
3 years ago
maxLines:5,
3 years ago
style: TextStyle(
fontSize: 14.sp,
fontWeight: MyFontWeight.medium,
color: Color(0xFF4C4C4C),
),
onChanged: (text) {
bool release = text != "" && text != null;
if (release != isRelease) {
isRelease = release;
setState(() {});
}
},
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(
vertical: 18.h,
),
errorBorder: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
hintText: "此时此刻的想法~",
hintStyle: TextStyle(
fontSize: 14.sp,
color: Color(0xFFA29E9E),
),
),
),
);
}
}
class Medias {
// Media media;
Medias(Media media) {
this.thumbPath = media.thumbPath;
this.path = media.path;
this.galleryMode = media.galleryMode;
}
String thumbPath;
String path;
GalleryMode galleryMode;
String remotePath;
}