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.

612 lines
21 KiB

import 'dart:convert';
3 years ago
import 'dart:ui';
3 years ago
import 'package:chewie/chewie.dart';
3 years ago
import 'package:dio/dio.dart';
3 years ago
import 'package:flutter/material.dart';
3 years ago
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/retrofit/data/article.dart';
3 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
3 years ago
import 'package:chewie/src/chewie_progress_colors.dart' as chewie;
3 years ago
import 'package:huixiang/retrofit/retrofit_api.dart';
3 years ago
import 'package:huixiang/utils/font_weight.dart';
3 years ago
import 'package:huixiang/view_widget/border_text.dart';
3 years ago
import 'package:huixiang/view_widget/custom_image.dart';
3 years ago
import 'package:huixiang/view_widget/icon_text.dart';
import 'package:huixiang/view_widget/round_button.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
3 years ago
import 'package:shared_preferences/shared_preferences.dart';
3 years ago
import 'package:video_player/video_player.dart';
3 years ago
import '../photo_view_gallery_screen.dart';
3 years ago
class CommunityDynamic extends StatefulWidget {
final int itemCount;
final Function(double height) heightFun;
3 years ago
final bool isDetails;
3 years ago
final int commentType;
3 years ago
final Function removalDynamic;
3 years ago
final Function exitFull;
3 years ago
final bool isList;
final Article article;
3 years ago
final String userId;
3 years ago
3 years ago
CommunityDynamic(
this.article,
3 years ago
this.commentType, {
3 years ago
Key key,
this.itemCount = 9,
this.heightFun,
this.isDetails = false,
3 years ago
this.removalDynamic,
this.exitFull,
3 years ago
this.userId,
3 years ago
this.isList = false,
3 years ago
}) : super(key: key);
@override
State<StatefulWidget> createState() {
return _CommunityDynamic();
}
}
3 years ago
3 years ago
class _CommunityDynamic extends State<CommunityDynamic> {
GlobalKey globalKey = GlobalKey();
double height = 0;
3 years ago
ApiService apiService;
3 years ago
VideoPlayerController videoPlayerController;
ChewieController chewieAudioController;
Chewie chewies;
3 years ago
@override
void initState() {
super.initState();
3 years ago
SharedPreferences.getInstance().then(
(value) => {
apiService = ApiService(
Dio(),
context: context,
token: value.getString('token'),
),
},
);
3 years ago
initVideo();
}
String filePath;
3 years ago
3 years ago
initVideo() async {
if (widget?.article?.content == null) return;
var cnt = jsonDecode(widget.article.content);
if (cnt["type"] == "video" && cnt["video"] != null) {
3 years ago
if (widget.isList) {
videoPlayerController = VideoPlayerController.network(
cnt["video"],
3 years ago
)..initialize().then((value) {});
} else {
videoPlayerController = VideoPlayerController.network(
cnt["video"],
3 years ago
)..initialize().then((value) {
chewieAudioController = ChewieController(
videoPlayerController: videoPlayerController,
aspectRatio: videoPlayerController.value.aspectRatio,
//宽高比
autoPlay: false,
//自动播放
looping: false,
//循环播放
allowFullScreen: true,
// 拖动条样式颜色
materialProgressColors: chewie.ChewieProgressColors(
playedColor: Colors.white,
handleColor: Colors.white,
backgroundColor: Colors.grey,
bufferedColor: Colors.transparent,
),
autoInitialize: true,
);
chewieAudioController.addListener(_fullScreenListener);
if (mounted) setState(() {});
});
}
3 years ago
}
3 years ago
}
///关注/取关会员
_vipFollow(followId, isFollow) async {
3 years ago
BaseData baseData = await apiService.follow(followId);
if (baseData != null && baseData.isSuccess) {
3 years ago
widget.exitFull();
SmartDialog.showToast(isFollow ? "关注成功" : "取关成功",
alignment: Alignment.center);
3 years ago
setState(() {});
3 years ago
} else {
3 years ago
// SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
3 years ago
}
}
///删除动态
_deleteDynamic(id) async {
BaseData baseData = await apiService.deleteTrend(id);
if (baseData != null && baseData.isSuccess) {
3 years ago
widget.exitFull();
3 years ago
SmartDialog.showToast("删除成功", alignment: Alignment.center);
3 years ago
setState(() {});
3 years ago
} else {
3 years ago
// SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
3 years ago
}
}
3 years ago
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
3 years ago
margin: EdgeInsets.only(top: 2),
3 years ago
key: globalKey,
3 years ago
alignment: Alignment.topCenter,
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Color(0x08000000),
offset: Offset(0, 1),
blurRadius: 8,
spreadRadius: 0,
),
],
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: 44,
child: Row(
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PhotoViewGalleryScreen(
images: [(widget?.article?.authorHeadImg ?? "").isEmpty?
"https://lmg.jj20.com/up/allimg/tx30/09041130358711081.jpg":widget?.article?.authorHeadImg
], //传入图片list
index: 0, //传入当前点击的图片的index
),
));
},
child:
MImage(
3 years ago
(widget?.article?.authorHeadImg ?? "https://lmg.jj20.com/up/allimg/tx30/09041130358711081.jpg"),
width: 44,
height: 44,
isCircle: true,
fit: BoxFit.cover,
errorSrc: "assets/image/default_1.png",
fadeSrc: "assets/image/default_1.png",
),
),
3 years ago
SizedBox(
width: 8,
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget?.article?.authorName ?? "",
3 years ago
style: TextStyle(
3 years ago
fontSize: 15.sp,
fontWeight: MyFontWeight.semi_bold,
3 years ago
color: Color(0xFF1A1A1A),
),
),
Text(
widget?.article?.createTime ?? "",
3 years ago
style: TextStyle(
3 years ago
fontSize: 13.sp,
3 years ago
fontWeight: MyFontWeight.regular,
color: Color(0xFF808080),
),
),
],
),
],
),
),
if (widget?.article?.updateUser != widget.userId ?? "")
3 years ago
GestureDetector(
onTap: () {
setState(() {
if (widget.commentType == 0) {
widget.article.liked =
!(widget.article.liked ?? false);
_vipFollow(widget.article.updateUser,
widget.article.liked ?? false);
3 years ago
} else {
showDeleteDialog();
}
});
},
3 years ago
child: (widget.commentType == 0)
3 years ago
? Container(
3 years ago
width: 56.w,
3 years ago
height: 25.h,
3 years ago
alignment: Alignment.center,
child: RoundButton(
height: 25.h,
backgroup: (widget?.article?.liked ?? false)
3 years ago
? Color(0xFFE6E6E6)
: Color(0xFF32A060),
textColor: (widget?.article?.liked ?? false)
3 years ago
? Color(0xFF808080)
: Colors.white,
text: (widget?.article?.liked ?? false)
3 years ago
? "已关注"
: "关注",
radius: 20,
icons: Icon(
(widget?.article?.liked ?? false)
3 years ago
? Icons.check
: Icons.add,
color: (widget?.article?.liked ?? false)
3 years ago
? Color(0xFF808080)
: Colors.white,
size: 15,
),
))
: Padding(
padding: EdgeInsets.all(20),
child: Icon(
Icons.close,
color: Colors.black,
size: 16,
),
),
3 years ago
),
3 years ago
],
),
SizedBox(
height: 12.h,
),
Text(
widget?.article?.mainTitle ?? "",
3 years ago
maxLines: 5,
3 years ago
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Color(0xFF1A1A1A),
fontWeight: MyFontWeight.regular,
3 years ago
fontSize: 15.sp,
3 years ago
),
),
buildMedia(widget?.article?.content),
3 years ago
if (!widget.isDetails)
SizedBox(
height: 12.h,
),
if (!widget.isDetails)
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
IconText(
(widget.article != null)
? "${widget.article.viewers}"
3 years ago
: "",
3 years ago
space: 4.w,
leftImage: "assets/svg/liulanliang.svg",
iconSize: 16,
3 years ago
textStyle: TextStyle(
fontSize: 14.sp,
),
3 years ago
),
IconText(
"${widget.article.comments ?? 0}",
3 years ago
space: 4.w,
leftImage: "assets/svg/pinglun.svg",
iconSize: 16,
3 years ago
textStyle: TextStyle(
fontSize: 14.sp,
),
3 years ago
),
3 years ago
GestureDetector(
3 years ago
onTap: () {},
3 years ago
child: IconText(
"${widget.article.likes ?? 0}",
3 years ago
space: 4.w,
leftImage: "assets/svg/xihuan.svg",
iconSize: 16,
3 years ago
textStyle: TextStyle(
fontSize: 14.sp,
),
3 years ago
),
3 years ago
),
],
),
],
),
),
Container(
height: 16,
color: Color(0xFFF7F7F7),
3 years ago
),
3 years ago
],
);
}
3 years ago
///动态内容
Widget buildMedia(String subjectInfo) {
if (subjectInfo == null || !subjectInfo.startsWith("{")) {
3 years ago
return Container();
}
var cnt = jsonDecode(subjectInfo);
3 years ago
Widget itemWidget = Container();
if (cnt["type"] == "image" &&
cnt["images"] != null &&
cnt["images"].length > 0) {
if (cnt["images"].length == 1) {
3 years ago
itemWidget = Container(
3 years ago
child: InkWell(
onTap: () {
// ImagePickers.previewImages(subjectInfo.images,0);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PhotoViewGalleryScreen(
images: cnt["images"], //传入图片list
index: 0, //传入当前点击的图片的index
),
),
);
},
child: MImage(
cnt["images"][0],
fit: BoxFit.contain,
radius: BorderRadius.circular(2),
3 years ago
height:MediaQuery.of(context).size.width/1.5,
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
)),
3 years ago
);
} else {
3 years ago
itemWidget = GridView.builder(
3 years ago
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:
(cnt["images"].length == 2 || cnt["images"].length == 4)
? 2
: 3,
3 years ago
crossAxisSpacing: 12.w,
mainAxisSpacing: 12.w,
childAspectRatio: 1,
),
padding: EdgeInsets.zero,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, position) {
return Container(
3 years ago
child: InkWell(
onTap: () {
// ImagePickers.previewImages(subjectInfo.images, position);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PhotoViewGalleryScreen(
images: cnt["images"], //传入图片list
index: position, //传入当前点击的图片的index
),
),
);
3 years ago
},
child: MImage(
cnt["images"][position]
.replaceAll(".jpg", ".jpg?imageView2/0/q/75"),
3 years ago
fit: BoxFit.cover,
aspectRatio: 1,
radius: BorderRadius.circular(1),
3 years ago
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
),
3 years ago
),
);
},
itemCount: cnt["images"].length,
3 years ago
);
}
} else if (cnt["type"] == "video" &&
cnt["video"] != null &&
cnt["video"].isNotEmpty) {
3 years ago
itemWidget = videoWidget(
MediaQuery.of(context).size.width - 32,
videoPlayerController != null
? (MediaQuery.of(context).size.width - 32) /
videoPlayerController.value.aspectRatio
: MediaQuery.of(context).size.width / 2,
cnt["video"].replaceAll(".mp4", "_poster.jpg"),
3 years ago
);
3 years ago
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 16.h,
),
3 years ago
itemWidget,
3 years ago
],
);
}
3 years ago
3 years ago
@override
void dispose() {
super.dispose();
3 years ago
3 years ago
if (chewieAudioController != null) {
chewieAudioController.pause();
chewieAudioController.dispose();
chewieAudioController = null;
}
3 years ago
3 years ago
if (videoPlayerController != null) {
videoPlayerController.pause();
videoPlayerController.dispose();
}
}
3 years ago
3 years ago
Widget videoWidget(double width, double height, src) {
print("src : $src");
3 years ago
return MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaleFactor: 0.9,
),
3 years ago
child: !widget.isList
? (chewieAudioController != null
? Container(
width: width,
height: height,
child: chewies = Chewie(
controller: chewieAudioController,
),
)
: Container(
width: width,
height: height,
))
3 years ago
: Container(
width: width,
height: width / 7 * 5,
3 years ago
color: Colors.black,
3 years ago
child: Stack(
children: [
Container(
width: double.infinity,
height: double.infinity,
3 years ago
child: MImage(
src,
aspectRatio: videoPlayerController != null
? videoPlayerController.value.aspectRatio
: (width / 7 * 5),
3 years ago
fit: BoxFit.cover,
errorSrc: "assets/image/default_2_1.png",
fadeSrc: "assets/image/default_2_1.png",
),
3 years ago
),
Center(
child: Icon(
Icons.play_circle_outline,
color: Colors.white,
size: 60,
),
),
],
),
3 years ago
),
3 years ago
);
}
Future<void> _fullScreenListener() async {
if (!chewieAudioController.isFullScreen) {
Future.delayed(Duration(seconds: 1), () {
widget.exitFull();
});
}
}
3 years ago
///删除动态弹窗
showDeleteDialog() {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Container(
width: MediaQuery.of(context).size.width - 84,
height: 130.h,
3 years ago
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"确定要删除这条动态?",
style: TextStyle(
fontSize: 17.sp,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
SizedBox(
height: 30.h,
),
Row(
children: [
Expanded(
child: InkWell(
child: BorderText(
text: "取消",
textColor: Color(0xFF32A060),
fontSize: 16.sp,
fontWeight: FontWeight.bold,
borderColor: Color(0xFF32A060),
radius: 4,
padding: EdgeInsets.all(12),
borderWidth: 1,
),
onTap: () {
Navigator.of(context).pop();
},
),
flex: 1,
),
SizedBox(
width: 16.w,
),
Expanded(
child: InkWell(
child: RoundButton(
text: "确定",
textColor: Colors.white,
radius: 4,
padding: EdgeInsets.all(12),
backgroup: Color(0xFF32A060),
fontSize: 16.sp,
fontWeight: FontWeight.bold,
),
onTap: () {
_deleteDynamic(widget.article.id);
3 years ago
Navigator.of(context).pop();
},
),
flex: 1,
),
],
)
],
),
),
);
},
);
}
3 years ago
@override
void didChangeDependencies() {
3 years ago
if (widget.heightFun != null)
WidgetsBinding.instance.addPostFrameCallback(_getContainerHeight);
3 years ago
super.didChangeDependencies();
}
_getContainerHeight(_) {
3 years ago
if (globalKey.currentContext != null)
height = globalKey.currentContext.size.height;
if (widget.heightFun != null) widget.heightFun(height);
3 years ago
print("height: $height");
}
}