import 'dart:io'; import 'dart:ui'; import 'package:chewie/chewie.dart'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:huixiang/retrofit/data/base_data.dart'; import 'package:huixiang/retrofit/data/comunity_comment.dart'; import 'package:chewie/src/chewie_progress_colors.dart' as chewie; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/border_text.dart'; import 'package:huixiang/view_widget/custom_image.dart'; import 'package:huixiang/view_widget/icon_text.dart'; import 'package:huixiang/view_widget/round_button.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:video_player/video_player.dart'; import 'package:thumbnails/thumbnails.dart'; class CommunityDynamic extends StatefulWidget { final int itemCount; final Function(double height) heightFun; final bool isDetails; final int commentType; final Function removalDynamic; final Function exitFull; final bool isList; final ComunityComment comment; CommunityDynamic( this.comment, this.commentType, { Key key, this.itemCount = 9, this.heightFun, this.isDetails = false, this.removalDynamic, this.exitFull, this.isList = false, }) : super(key: key); @override State createState() { return _CommunityDynamic(); } } class _CommunityDynamic extends State { GlobalKey globalKey = GlobalKey(); double height = 0; ApiService apiService; VideoPlayerController videoPlayerController; ChewieController chewieAudioController; Chewie chewies; @override void initState() { super.initState(); SharedPreferences.getInstance().then( (value) => { apiService = ApiService( Dio(), context: context, token: value.getString('token'), ), }, ); initVideo(); } String filePath; initVideo() async { if (widget.comment.subjectInfo.type == "video" && widget.comment.subjectInfo.video.isNotEmpty) { if (widget.isList) { videoPlayerController = VideoPlayerController.network( widget.comment.subjectInfo.video, )..initialize().then((value) {}); } else { videoPlayerController = VideoPlayerController.network( widget.comment.subjectInfo.video, )..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(() {}); }); } } } ///关注/取关会员 _vipFollow(followId) async { BaseData baseData = await apiService.follow(followId); if (baseData != null && baseData.isSuccess) { SmartDialog.showToast("关注成功", alignment: Alignment.center); } else { SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } ///删除动态 _deleteDynamic(id) async { BaseData baseData = await apiService.deleteTrend(id); if (baseData != null && baseData.isSuccess) { widget.exitFull(); SmartDialog.showToast("删除成功", alignment: Alignment.center); } else { SmartDialog.showToast(baseData.msg, alignment: Alignment.center); } } @override Widget build(BuildContext context) { return Column( children: [ Container( margin: EdgeInsets.only(top: 2), key: globalKey, 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: [ MImage( widget?.comment?.memberInfo?.avatar ?? "", width: 44, height: 44, isCircle: true, fit: BoxFit.cover, errorSrc: "assets/image/default_1.png", fadeSrc: "assets/image/default_1.png", ), SizedBox( width: 8, ), Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( widget?.comment?.memberInfo?.nickname ?? "", style: TextStyle( fontSize: 14.sp, fontWeight: MyFontWeight.medium, color: Color(0xFF1A1A1A), ), ), Text( widget?.comment?.createTime ?? "", style: TextStyle( fontSize: 12.sp, fontWeight: MyFontWeight.regular, color: Color(0xFF808080), ), ), ], ), ], ), ), GestureDetector( onTap: () { setState(() { if (widget.commentType == 0) { widget.comment.selfFollow = !(widget.comment.selfFollow ?? false); _vipFollow(widget.comment.memberInfo.mid); } else { showDeleteDialog(); } }); }, child: (widget.commentType == 0) ? RoundButton( padding: EdgeInsets.symmetric( horizontal: 8, vertical: 3, ), backgroup: (widget.comment.selfFollow ?? false) ? Color(0xFFE6E6E6) : Color(0xFF32A060), textColor: (widget.comment.selfFollow ?? false) ? Color(0xFF808080) : Colors.white, text: (widget.comment.selfFollow ?? false) ? "已关注" : "关注", radius: 20, icons: Icon( (widget.comment.selfFollow ?? false) ? Icons.check : Icons.add, color: (widget.comment.selfFollow ?? false) ? Color(0xFF808080) : Colors.white, size: 14, ), ) : Icon( Icons.close, color: Colors.black, size: 16, ), ), ], ), SizedBox( height: 12.h, ), Text( widget.comment.subject ?? "", maxLines: 5, overflow: TextOverflow.ellipsis, style: TextStyle( color: Color(0xFF1A1A1A), fontWeight: MyFontWeight.regular, fontSize: 14.sp, ), ), buildMedia(widget.comment.subjectInfo), if (!widget.isDetails) SizedBox( height: 12.h, ), if (!widget.isDetails) Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ IconText( (widget.comment != null) ? "${widget.comment.viewers}" : "", space: 4.w, leftImage: "assets/svg/liulanliang.svg", iconSize: 16, ), IconText( "${widget.comment.comments ?? 0}", space: 4.w, leftImage: "assets/svg/pinglun.svg", iconSize: 16, ), IconText( "${widget.comment.likes ?? 0}", space: 4.w, leftImage: "assets/svg/xihuan.svg", iconSize: 16, ), ], ), ], ), ), Container( height: 16, color: Color(0xFFF7F7F7), ), ], ); } ///动态内容 Widget buildMedia(SubjectInfo subjectInfo) { if (subjectInfo == null) { return Container(); } Widget itemWidget = Container(); if (subjectInfo.type == "image" && subjectInfo.images.length > 0) { if (subjectInfo.images.length == 1) { itemWidget = Container( child: MImage( subjectInfo.images[0], fit: BoxFit.cover, width: MediaQuery.of(context).size.width / 1.5, height: MediaQuery.of(context).size.width / 1.5, errorSrc: "assets/image/default_2_1.png", fadeSrc: "assets/image/default_2_1.png", ), ); } else { itemWidget = GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: (subjectInfo.images.length == 2 || subjectInfo.images.length == 4) ? 2 : 3, crossAxisSpacing: 12.w, mainAxisSpacing: 12.w, childAspectRatio: 1, ), padding: EdgeInsets.zero, shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemBuilder: (context, position) { return Container( child: MImage( subjectInfo.images[position], fit: BoxFit.cover, aspectRatio: 1, errorSrc: "assets/image/default_2_1.png", fadeSrc: "assets/image/default_2_1.png", ), ); }, itemCount: subjectInfo.images.length, ); } } else if (subjectInfo.type == "video" && subjectInfo.video.isNotEmpty) { 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, !widget.isList ? subjectInfo.video : widget.comment.coverImg, ); } return Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ SizedBox( height: 16.h, ), itemWidget, ], ); } @override void dispose() { super.dispose(); if (chewieAudioController != null) { chewieAudioController.pause(); chewieAudioController.dispose(); chewieAudioController = null; } if (videoPlayerController != null) { videoPlayerController.pause(); videoPlayerController.dispose(); } } Widget videoWidget(double width, double height, src) { print("src : $src"); return MediaQuery( data: MediaQuery.of(context).copyWith( textScaleFactor: 0.9, ), child: !widget.isList ? (chewieAudioController != null ? Container( width: width, height: height, child: chewies = Chewie( controller: chewieAudioController, ), ) : Container( width: width, height: height, )) : Container( width: width, height: width / 7 * 5, color: Colors.black, child: Stack( children: [ Center( child: MImage( src, aspectRatio: videoPlayerController != null ? videoPlayerController.value.aspectRatio : (width / 7 * 5), fit: BoxFit.cover, errorSrc: "assets/image/default_2_1.png", fadeSrc: "assets/image/default_2_1.png", ), ), Center( child: Icon( Icons.play_circle_outline, color: Colors.white, size: 60, ), ), ], ), ), ); } Future _fullScreenListener() async { if (!chewieAudioController.isFullScreen) { Future.delayed(Duration(seconds: 1), () { widget.exitFull(); }); } } ///删除动态弹窗 showDeleteDialog() { showDialog( context: context, builder: (context) { return AlertDialog( content: Container( width: MediaQuery.of(context).size.width - 84, height: 110.h, 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.comment.id); Navigator.of(context).pop(); }, ), flex: 1, ), ], ) ], ), ), ); }, ); } @override void didChangeDependencies() { if (widget.heightFun != null) WidgetsBinding.instance.addPostFrameCallback(_getContainerHeight); super.didChangeDependencies(); } _getContainerHeight(_) { if (globalKey.currentContext != null) height = globalKey.currentContext.size.height; if (widget.heightFun != null) widget.heightFun(height); print("height: $height"); } }